using System; using System.Collections.Generic; using AnyThinkAds.Api; using Runtime.ADAggregator; using UnityEngine; public class InteractionPlayer : ADPlayer , ATInterstitialAdListener { private ATInterstitialAd _atInterstitialAd; private ATInterstitialAutoAd _atInterstitialAutoAd; private ADListenerAggregator _aggregator; private bool _autoLoadRegistered; public override void OnInit() { this._atInterstitialAd = ATInterstitialAd.Instance; this._atInterstitialAutoAd = ATInterstitialAutoAd.Instance; this._aggregator = new ADListenerAggregator(); this._aggregator.BindInterstitialAdListener(this._atInterstitialAd.client,this); } public override void ShowAD(Action onClose, Action onVideoComplete) { if (this.IsReadly()) { this.adListener.onClose = onClose; this.adListener.onVideoComplete = onVideoComplete; if (UseAutoLoad()) { EnsureAutoLoadRegistered(); this._atInterstitialAutoAd.showAutoAd(this.Key, BuildInterstitialExtra()); } else { ATInterstitialAd.Instance.showInterstitialAd(this.Key); } curState = 0; } } 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; } public override void LoadAD() { if (curState == 0) { 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; } { //加载广告 Dictionary jsonmap = new Dictionary(); //只针对Sigmob,Sigmob的激励视频广告源当做插屏使用 // 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) { this.curState = 0; this.adListener.OnShowError(); } 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) { this.curState = 0; this.adListener.OnShowError(); } 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 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 BuildInterstitialExtra() { return new Dictionary { { 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); } }