2024-12-20 18:19:39 +08:00

82 lines
2.0 KiB
C#

using TTSDK;
using TTSDK.UNBridgeLib.LitJson;
using UnityEngine;
/// <summary>
/// 广告管理器
/// </summary>
public class AdMgr : MonoBehaviour
{
private static AdMgr instance;
public static AdMgr Instance => instance;
private void Awake()
{
if (instance != null)
{
Destroy(gameObject);
return;
}
instance = this;
}
private TTRewardedVideoAd adInstance; //全局唯一的激励视频实例
private AdCallback callback; //广告播放完成回调
private object args; //回调参数
private void Start()
{
DontDestroyOnLoad(gameObject);
Init();
}
/// <summary>
/// 初始化激励视频广告
/// </summary>
private void Init()
{
string videoAdId = "8k6krp0rjl87k356al"; //激励视频广告id
adInstance = TT.CreateRewardedVideoAd(
videoAdId,
(isClose, closeCode) => {
Debug.Log("激励视频关闭 state: " + isClose + ", code: " + closeCode);
if (isClose)
{
//广告数据ROI
TT.SendToTAQ(new JsonData
{
["event_type"] = "lt_roi"
});
if (callback != null)
{
callback(args);
}
}
},
(errorCode, errorMsg) => {
Debug.Log("激励视频错误 " + errorCode);
}
);
}
/// <summary>
/// 显示激励视频广告
/// </summary>
/// <param name="onShowFinish">广告显示完成回调</param>
/// <param name="args">回调参数</param>
public void ShowAd(AdCallback callback, object args = null)
{
this.callback = callback;
this.args = args;
adInstance?.Show();
}
}
/// <summary>
/// 广告显示完成回调
/// </summary>
/// <param name="args"></param>
public delegate void AdCallback(object args);