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

232 lines
6.8 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 InteractionPlayer : ADPlayer , ATInterstitialAdListener
{
private ATInterstitialAd _atInterstitialAd;
2026-04-22 17:44:49 +08:00
private ATInterstitialAutoAd _atInterstitialAutoAd;
2023-01-29 12:44:57 +08:00
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._atInterstitialAd = ATInterstitialAd.Instance;
2026-04-22 17:44:49 +08:00
this._atInterstitialAutoAd = ATInterstitialAutoAd.Instance;
2023-01-29 12:44:57 +08:00
this._aggregator = new ADListenerAggregator();
this._aggregator.BindInterstitialAdListener(this._atInterstitialAd.client,this);
}
public override void ShowAD(Action onClose, Action<bool> onVideoComplete)
{
2026-04-22 17:44:49 +08:00
if (this.IsReadly())
2023-01-29 12:44:57 +08:00
{
2026-04-22 17:44:49 +08:00
this.adListener.onClose = onClose;
this.adListener.onVideoComplete = onVideoComplete;
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
this._atInterstitialAutoAd.showAutoAd(this.Key, BuildInterstitialExtra());
}
else
{
ATInterstitialAd.Instance.showInterstitialAd(this.Key);
}
2023-01-29 12:44:57 +08:00
curState = 0;
}
}
2026-04-22 17:44:49 +08:00
public override bool IsReadly()
{
if (this.curState == 2)
{
return true;
}
if (UseAutoLoad())
{
if (this._atInterstitialAutoAd != null &&
this._atInterstitialAutoAd.autoLoadInterstitialAdReadyForPlacementID(this.Key))
{
this.curState = 2;
return true;
}
return false;
}
if (this._atInterstitialAd != null && this._atInterstitialAd.hasInterstitialAdReady(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._atInterstitialAutoAd.setAutoLocalExtra(this.Key, BuildInterstitialExtra());
this.curState = this._atInterstitialAutoAd.autoLoadInterstitialAdReadyForPlacementID(this.Key) ? 2 : 1;
return;
}
if (this._atInterstitialAd != null && this._atInterstitialAd.hasInterstitialAdReady(this.Key))
{
this.curState = 2;
return;
}
2023-01-29 12:44:57 +08:00
{
//加载广告
Dictionary<string, object> jsonmap = new Dictionary<string, object>();
//只针对SigmobSigmob的激励视频广告源当做插屏使用
// jsonmap.Add(AnyThinkAds.Api.ATConst.USE_REWARDED_VIDEO_AS_INTERSTITIAL, AnyThinkAds.Api.ATConst.USE_REWARDED_VIDEO_AS_INTERSTITIAL_NO);
var width = (int) (Screen.width * 0.7f);
ATSize atSize = new ATSize(width, (int) (width * 1.5f));
jsonmap.Add(ATInterstitialAdLoadingExtra.kATInterstitialAdLoadingExtraInterstitialAdSizeStruct , atSize);
curState = 1;
this._atInterstitialAd.loadInterstitialAd(this.Key, jsonmap);
}
}
}
#region SDK
public void onInterstitialAdLoad(string placementId)
{
this.curState = 2;
}
public void onInterstitialAdLoadFail(string placementId, string code, string message)
{
this.curState = 0;
}
public void onInterstitialAdShow(string placementId, ATCallbackInfo callbackInfo)
{
}
public void onInterstitialAdFailedToShow(string placementId)
{
2026-04-22 17:44:49 +08:00
this.curState = 0;
this.adListener.OnShowError();
2023-01-29 12:44:57 +08:00
}
public void onInterstitialAdClose(string placementId, ATCallbackInfo callbackInfo)
{
this.adListener.OnAdClose();
this.adListener.OnShowComplete();
}
public void onInterstitialAdClick(string placementId, ATCallbackInfo callbackInfo)
{
}
public void onInterstitialAdStartPlayingVideo(string placementId, ATCallbackInfo callbackInfo)
{
}
public void onInterstitialAdEndPlayingVideo(string placementId, ATCallbackInfo callbackInfo)
{
}
public void onInterstitialAdFailedToPlayVideo(string placementId, string code, string message)
{
2026-04-22 17:44:49 +08:00
this.curState = 0;
this.adListener.OnShowError();
2023-01-29 12:44:57 +08:00
}
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)
{
this.OnError(code , message);
}
#endregion
2026-04-22 17:44:49 +08:00
public override void OnPlayRequestStarted()
{
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
this._atInterstitialAutoAd?.setAutoLocalExtra(this.Key, BuildInterstitialExtra());
}
}
public override int MaxLoadAttempts => ToponAdController.CurrentOptions?.InterstitialMaxLoadAttempts ?? base.MaxLoadAttempts;
public override float LoadRetryDelaySeconds =>
Math.Max(0f, (ToponAdController.CurrentOptions?.InterstitialLoadRetryDelayMs ?? 500) / 1000f);
public override bool AutoPreloadOnInit => ToponAdController.CurrentOptions?.InterstitialPrewarmOnInit ?? true;
private bool UseAutoLoad()
{
return ToponAdController.CurrentOptions?.InterstitialAutoLoad ?? true;
}
private void EnsureAutoLoadRegistered()
{
if (_autoLoadRegistered)
{
return;
}
this._atInterstitialAutoAd?.addAutoLoadAdPlacementID(new[] { this.Key });
_autoLoadRegistered = true;
}
private Dictionary<string, string> BuildInterstitialExtra()
{
return new Dictionary<string, string>
{
{ ATConst.SCENARIO, this.AdScene ?? string.Empty }
};
}
public override void EnterAdScenario(string scenario)
{
if (string.IsNullOrWhiteSpace(scenario) || string.Equals(scenario, "__default__", StringComparison.Ordinal))
{
return;
}
if (UseAutoLoad())
{
EnsureAutoLoadRegistered();
this._atInterstitialAutoAd?.entryAutoAdScenarioWithPlacementID(this.Key, scenario);
return;
}
this._atInterstitialAd?.entryScenarioWithPlacementID(this.Key, scenario);
}
}