Files
Commercialization.topon/Assets/Topon_Adapter/Runtime/Scripts/AwardVideoPlayer.cs

240 lines
7.2 KiB
C#
Raw Normal View History

2023-01-29 12:44:57 +08:00
using System;
using System.Collections.Generic;
using AnyThinkAds.Api;
using Runtime.ADAggregator;
using UnityEngine;
public class AwardVideoPlayer : ADPlayer , ATRewardedVideoListener
{
private ATRewardedVideo _atRewardedVideo;
2026-04-22 17:44:49 +08:00
private ATRewardedAutoVideo _atRewardedAutoVideo;
2023-01-29 12:44:57 +08:00
private Action<bool> _onVideoComplete;
private ADListenerAggregator _aggregator;
2026-04-22 17:44:49 +08:00
private bool _autoLoadRegistered;
2023-01-29 12:44:57 +08:00
public override void OnInit()
{
this._atRewardedVideo = ATRewardedVideo.Instance;
2026-04-22 17:44:49 +08:00
this._atRewardedAutoVideo = ATRewardedAutoVideo.Instance;
2023-01-29 12:44:57 +08:00
// this._atRewardedVideo.client.setListener(this); //由于新版本广告sdk弃用该方式通过以下方式重新桥接监听事件
this._aggregator = new ADListenerAggregator();
this._aggregator.BindAwardVideoListener(this._atRewardedVideo.client , this);
// var adClient = this._atRewardedVideo.client;
// adClient.on
}
public override void ShowAD(Action onClose, Action<bool> onVideoComplete)
{
if (this.IsReadly())
{
2023-04-04 18:33:51 +08:00
this.curState = 0;
2023-01-29 12:44:57 +08:00
this._onVideoComplete = onVideoComplete;
this.adListener.onClose = onClose;
this.adListener.onVideoComplete = this.OnVideoComplete;
2023-09-14 14:20:26 +08:00
var json = new Dictionary<string, string> { { AnyThinkAds.Api.ATConst.SCENARIO, this.AdScene } };
2026-04-22 17:44:49 +08:00
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
this._atRewardedAutoVideo.showAutoAd(this.Key, json);
}
else
{
this._atRewardedVideo.showAd(this.Key , json);
}
2023-01-29 12:44:57 +08:00
}
}
private void OnVideoComplete(bool obj)
{
this._onVideoComplete?.Invoke(obj);
this._onVideoComplete = null;
}
public override bool IsReadly()
{
2026-04-22 17:44:49 +08:00
if (UseAutoLoad())
{
if (this._atRewardedAutoVideo != null &&
this._atRewardedAutoVideo.autoLoadRewardedVideoReadyForPlacementID(this.Key))
{
this.curState = 2;
return true;
}
return this.curState == 2;
}
if (this.curState == 2)
{
return true;
}
if (this._atRewardedVideo != null && this._atRewardedVideo.hasAdReady(this.Key))
{
this.curState = 2;
return true;
}
return false;
2023-01-29 12:44:57 +08:00
}
public override void LoadAD()
{
if (curState == 0)
{
2026-04-22 17:44:49 +08:00
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
this._atRewardedAutoVideo.setAutoLocalExtra(this.Key, BuildRewardedExtra());
this.curState = this._atRewardedAutoVideo.autoLoadRewardedVideoReadyForPlacementID(this.Key) ? 2 : 1;
return;
}
if (this._atRewardedVideo != null && this._atRewardedVideo.hasAdReady(this.Key))
{
this.curState = 2;
return;
}
2023-01-29 12:44:57 +08:00
{
Dictionary<string,string> jsonmap = new Dictionary<string,string>();
//ATConst.USERID_KEY必传用于标识每个用户;ATConst.USER_EXTRA_DATA为可选参数传入后将透传到开发者的服务器
2026-04-22 17:44:49 +08:00
jsonmap = BuildRewardedExtra();
2023-01-29 12:44:57 +08:00
curState = 1;
this._atRewardedVideo.loadVideoAd(this.Key, jsonmap);
}
}
}
#region SDK
public void onRewardedVideoAdLoaded(string placementId)
{
this.curState = 2;
}
public void onRewardedVideoAdLoadFail(string placementId, string code, string message)
{
Debug.LogError($"激励视频加载失败: {message} , code:{code} , placementId: {placementId}");
this.curState = 0;
}
public void onRewardedVideoAdPlayStart(string placementId, ATCallbackInfo callbackInfo)
{
}
public void onRewardedVideoAdPlayEnd(string placementId, ATCallbackInfo callbackInfo)
{
}
public void onRewardedVideoAdPlayFail(string placementId, string code, string message)
{
Debug.LogError($"激励视频播放失败: {message} , code:{code} , placementId: {placementId}");
curState = 0;
this.adListener.OnShowError();
}
public void onRewardedVideoAdPlayClosed(string placementId, bool isReward, ATCallbackInfo callbackInfo)
{
this.adListener.OnRewardVerify(isReward , 1 , "");
this.adListener.OnAdClose();
}
public void onRewardedVideoAdPlayClicked(string placementId, ATCallbackInfo callbackInfo)
{
}
public void onReward(string placementId, ATCallbackInfo callbackInfo)
{
this.adListener.OnRewardVerify(true , 1 , "");
this.adListener.OnAdClose();
}
public void startLoadingADSource(string placementId, ATCallbackInfo callbackInfo)
{
}
public void finishLoadingADSource(string placementId, ATCallbackInfo callbackInfo)
{
}
public void failToLoadADSource(string placementId, ATCallbackInfo callbackInfo, string code, string message)
{
this.OnError(code , message);
}
public void startBiddingADSource(string placementId, ATCallbackInfo callbackInfo)
{
}
public void finishBiddingADSource(string placementId, ATCallbackInfo callbackInfo)
{
}
public void failBiddingADSource(string placementId, ATCallbackInfo callbackInfo, string code, string message)
{
}
#endregion
2026-04-22 17:44:49 +08:00
public override void OnPlayRequestStarted()
{
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
this._atRewardedAutoVideo?.setAutoLocalExtra(this.Key, BuildRewardedExtra());
}
}
public override int MaxLoadAttempts => ToponAdController.CurrentOptions?.RewardedMaxLoadAttempts ?? base.MaxLoadAttempts;
public override float LoadRetryDelaySeconds =>
Math.Max(0f, (ToponAdController.CurrentOptions?.RewardedLoadRetryDelayMs ?? 750) / 1000f);
public override bool AutoPreloadOnInit => ToponAdController.CurrentOptions?.RewardedPrewarmOnInit ?? true;
private bool UseAutoLoad()
{
return ToponAdController.CurrentOptions?.RewardedAutoLoad ?? true;
}
private void EnsureAutoLoadRegistered()
{
if (_autoLoadRegistered)
{
return;
}
this._atRewardedAutoVideo?.addAutoLoadAdPlacementID(new[] { this.Key });
_autoLoadRegistered = true;
}
private Dictionary<string, string> BuildRewardedExtra()
{
return new Dictionary<string, string>
{
{ ATConst.USERID_KEY, ADManager.Instance.UserId },
{ ATConst.USER_EXTRA_DATA, "user_extra_data" }
};
}
public override void EnterAdScenario(string scenario)
{
if (string.IsNullOrWhiteSpace(scenario) || string.Equals(scenario, "__default__", StringComparison.Ordinal))
{
return;
}
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
this._atRewardedAutoVideo?.entryAutoAdScenarioWithPlacementID(this.Key, scenario);
return;
}
this._atRewardedVideo?.entryScenarioWithPlacementID(this.Key, scenario);
}
2023-01-29 12:44:57 +08:00
2026-04-22 17:44:49 +08:00
}