Unity开发抖音小游戏广告部分接入

Unity开发抖音小游戏广告部分接入

介绍

最近在使用Unity做抖音小游戏这块的内容,因为要接入广告,所以这里我把我接入广告的部分代码和经验分享一下。

环境确保

根据抖音官方的文档我们是先需要安装BGDT这个支持插件

安装好之后如下打开开发者工具

需要再安装对应版本的Start SDK这个是广告sdk(其他我下载的大家也可以参考一下)

开通流量主获取广告位

打开抖音开放平台的控制台找到商业化中的流量主,这里通常是未开放的需要自己打开流量主(这里很快)

创建广告位,这里可以根据自己的需要去创建(激励视频、插屏广告、Banner广告)。创建完成之后会提供广告位ID。

广告部分代码

管理脚本

csharp 复制代码
using UnityEngine;
using StarkSDKSpace;
using static StarkSDKSpace.StarkAdManager;

public delegate void OnVideoAdCloseCallBack();
public delegate void OnVideoAdErrorCallBack();

public class AdClass : VideoAdCallback
{
    public void OnError(int errCode, string errorMessage)
    {
        Debug.LogError("播放错误");
    }

    public void OnVideoClose(int watchedTime, int effectiveTime, int duration)
    {
        Debug.LogError("视频关闭 看了多久 = " + watchedTime);
    }

    public void OnVideoLoaded()
    {
        Debug.LogError("视频加载完毕");
    }

    public void OnVideoShow(long timestamp)
    {
        Debug.LogError("开始展示视频");
    }
}

public class ByteGameAdManager : MonoBehaviour
{
    private static ByteGameAdManager _instance;

    private AdClass adc;

    public static ByteGameAdManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new GameObject("ByteGameAdManager").AddComponent<ByteGameAdManager>();
                DontDestroyOnLoad(_instance.gameObject);
            }

            return _instance;
        }
    }

    private StarkAdManager starkAdManager;


    //public const string LevelComplete_Video_Id = "mi5ag76n0bg4506577";
    //public const string Back_Interstitial_Video_Id = "2efdh3aelb10i0q2ie";
    //public const string Resume_Interstitial_Video_Id = "9e953k528dj5ljkcef";
    //public const string Game_Banner_Id = "4f08ml2f1a81vsqvbg";

    private void Awake()
    {
        _instance = this;
        DontDestroyOnLoad(_instance.gameObject);

		//这个是为了在开发工具中方便测试所以需要打开这里
        if (Application.isEditor)
            MockSetting.OpenAllMockModule();
        starkAdManager = StarkSDK.API.GetStarkAdManager();
        adc = new AdClass();
    }

    /// <summary>
    /// 播放激励视频广告
    /// </summary>
    /// <param name="adId"></param>
    /// <param name="closeCallBack"></param>
    /// <param name="errorCallBack"></param>
    public void ShowVideoAd(string adId, System.Action<bool> closeCallBack, System.Action<int, string> errorCallBack)
    {
        if (starkAdManager != null)
        {
            starkAdManager.ShowVideoAdWithId(adId, closeCallBack, errorCallBack);
        }
    }

    /// <summary>
    /// 播放插屏广告
    /// </summary>
    /// <param name="adId"></param>
    /// <param name="errorCallBack"></param>
    /// <param name="closeCallBack"></param>
    public void ShowInterstitialAd(string adId, System.Action<int, string> errorCallBack, System.Action closeCallBack)
    {
        if (starkAdManager != null)
            starkAdManager.CreateInterstitialAd(adId, errorCallBack, closeCallBack);
    }
}

测试脚本

csharp 复制代码
 if (ByteGameAdManager.Instance != null) 
        {
            //这里需要开通流量主获取广告位ID
            ByteGameAdManager.Instance.ShowVideoAd("5egc7tjx9774ad0il0",
            (bol) => {
                Debug.LogError("关闭广告 ,是否已经看完" + bol);
            },
            (it,str) => {
                Debug.LogError("错误警告Error");
            });
        }

测试如下

编辑器下如下图所示(根据自己的点击获得反馈)

正常发布版本之后抖音上是正常看到的广告。

总结

上述是我对抖音小游戏广告部分的整理和经验分享,感谢大家的支持。

相关推荐
LF男男10 小时前
GameManager.cs
unity
郝学胜-神的一滴1 天前
[简化版 GAMES 101] 计算机图形学 07:图形学投影完全推导
c++·unity·图形渲染·three.js·unreal engine
Avalon7122 天前
Unity3D响应式渲染UI框架UniVue
游戏·ui·unity·c#·游戏引擎
ellis19702 天前
Unity UI性能优化一之插件【Unity UI Optimization Tool】
unity·性能优化
Zik----2 天前
Unity基础学习笔记(B站视频课整理)
unity·vr
郝学胜-神的一滴3 天前
罗德里格斯旋转公式(Rodrigues‘ Rotation Formula)完整推导
c++·unity·godot·图形渲染·three.js·unreal
田鸡_3 天前
Unity新输入系统(Input System)教学篇
unity·游戏引擎·游戏程序
EQ-雪梨蛋花汤3 天前
【Unity笔记】Unity 音游模板与免费资源:高效构建节奏游戏开发全指南
笔记·unity·游戏引擎
星辰徐哥3 天前
Unity基础:游戏对象的激活与隐藏:SetActive方法详解
游戏·unity·lucene
微莱羽墨3 天前
零、0基础入门Unity 安装详细教程(2026最新版教程,安装Unity看这一篇就够了!)
unity·游戏引擎·unity安装