using System; using System.Collections; using System.Collections.Generic; using System.Threading; using AnyThinkAds.Api; using AnyThinkAds.ThirdParty.LitJson; using Script.Common; using Script.SDKManager.AdsSDKManager.Constant; using Script.SDKManager.AdsSDKManager.Utils; using Script.Utils; using SDKManager.AdsSDKManager.Utils; using UnityEngine; namespace Script.SDKManager.AdsSDKManager.TpnAdsManager { public class TpnAdsManager : NormalSingleton, IAdService { public string ClientName => "Tpn"; public string topon_app_id; public string topon_app_key; public string topon_interstitial_units; public string topon_rewarded_units; private string _rvPos; private string _ivPos; private Action _rvCloseCallback = null; private Action _ivCloseCallback = null; private Action _rvShowFailedCallback = null; public void Initialize() { topon_app_id = AdConfigParser.GetTopOnAppId(); topon_app_key = AdConfigParser.GetTopOnAppKey(); topon_interstitial_units = AdConfigParser.GetTopOnAdUnits(AdsType.Interstitial).ToArray()[0]; topon_rewarded_units = AdConfigParser.GetTopOnAdUnits(AdsType.Rewarded).ToArray()[0]; LoggerUtils.Debug("[Tpn] 初始化 Tpn 广告 SDK appid:" + topon_app_id + " appkey:" + topon_app_key + " ivId:" + topon_interstitial_units + " rvId:" + topon_rewarded_units); if (!string.IsNullOrEmpty(topon_app_id) && !string.IsNullOrEmpty(topon_app_key)) { ATSDKAPI.initSDK(topon_app_id, topon_app_key); ATSDKAPI.setLogDebug(false); // ATSDKAPI.showDebuggerUI(); InitRewarded(); InitInterstitial(); } } #region 激励广告功能 private void InitRewarded() { if (string.IsNullOrEmpty(topon_rewarded_units)) return; ATRewardedAutoVideo.Instance.client.onAdLoadEvent += OnAdLoadedEvent; ATRewardedAutoVideo.Instance.client.onAdLoadFailureEvent += OnAdLoadedFailEvent; ATRewardedAutoVideo.Instance.client.onAdVideoCloseEvent += OnAdVideoClosedEvent; ATRewardedAutoVideo.Instance.client.onAdVideoStartEvent += OnAdVideoStartEvent; ATRewardedAutoVideo.Instance.client.onAdVideoFailureEvent += OnAdVideoFailureEvent; ATRewardedAutoVideo.Instance.client.onAdClickEvent += OnAdVideoClickedEvent; ATRewardedAutoVideo.Instance.addAutoLoadAdPlacementID(new string[] { topon_rewarded_units }); } public void LoadRewarded() { } public void DisplayRewarded(string adPos, Action rewardCallback = null, Action showFailedCallback = null) { _rvPos = adPos; _rvCloseCallback = rewardCallback; _rvShowFailedCallback = showFailedCallback; ATRewardedAutoVideo.Instance.showAutoAd(topon_rewarded_units); } public double GetRewardedRevenue() { return 0; } public bool IsRewardedAvailable() { return string.IsNullOrEmpty(topon_rewarded_units) ? false : ATRewardedAutoVideo.Instance.autoLoadRewardedVideoReadyForPlacementID(topon_rewarded_units); } #endregion #region 插页广告功能 private void InitInterstitial() { if (string.IsNullOrEmpty(topon_interstitial_units)) return; ATInterstitialAutoAd.Instance.client.onAdLoadEvent += OnAdLoadedEvent; ATInterstitialAutoAd.Instance.client.onAdLoadFailureEvent += OnAdLoadedFailEvent; ATInterstitialAutoAd.Instance.client.onAdCloseEvent += OnAdVideoClosedEvent; ATInterstitialAutoAd.Instance.client.onAdShowEvent += OnAdVideoStartEvent; ATInterstitialAutoAd.Instance.client.onAdShowFailureEvent += OnAdVideoFailureEvent; ATInterstitialAutoAd.Instance.client.onAdClickEvent += OnAdVideoClickedEvent; ATInterstitialAutoAd.Instance.addAutoLoadAdPlacementID(new string[] { topon_interstitial_units }); } public void LoadInterstitial() { } public double GetInterstitialRevenue() { return 0; } public void DisplayInterstitial(string ivPos, IvType _IvType = IvType.IV1, Action closeCallback = null) { _ivPos = ivPos; _ivCloseCallback = closeCallback; ATInterstitialAutoAd.Instance.showAutoAd(topon_interstitial_units); } public bool IsInterstitialAvailable() { return string.IsNullOrEmpty(topon_interstitial_units) ? false : ATInterstitialAutoAd.Instance.autoLoadInterstitialAdReadyForPlacementID(topon_interstitial_units); } #endregion #region 代理 private void OnAdLoadedEvent(object sender, ATAdEventArgs erg) { if (erg.placementId.Equals(topon_interstitial_units)) { LoggerUtils.Debug("[Tpn] ads tpn topon interstitial loaded"); } else if (erg.placementId.Equals(topon_rewarded_units)) { LoggerUtils.Debug("[Tpn] ads tpn topon rewarded loaded"); } } private void OnAdLoadedFailEvent(object sender, ATAdErrorEventArgs erg) { LoggerUtils.Debug("[Tpn] 广告加载失败:" + JsonMapper.ToJson(erg.callbackInfo.toDictionary())); } private void OnAdVideoStartEvent(object sender, ATAdEventArgs erg) { LoggerUtils.Debug("[Tpn] 广告开始播放:" + JsonMapper.ToJson(erg.callbackInfo.toDictionary())); TrackAdImpression(erg); ThreadUtils.QueueOnMainThread(pObj => { if (erg.placementId.Equals(topon_rewarded_units)) { } else if (erg.placementId.Equals(topon_interstitial_units)) { } }, ""); } private void OnAdVideoFailureEvent(object sender, ATAdErrorEventArgs erg) { LoggerUtils.Debug("[Tpn] 广告播放失败" + JsonMapper.ToJson(erg.callbackInfo.toDictionary())); ThreadUtils.QueueOnMainThread(pObj => { if (erg.placementId.Equals(topon_rewarded_units)) { _rvShowFailedCallback?.Invoke(); _rvShowFailedCallback = null; } else if (erg.placementId.Equals(topon_interstitial_units)) { _ivCloseCallback?.Invoke(); _ivCloseCallback = null; } }, ""); } private void OnAdVideoClosedEvent(object sender, ATAdEventArgs erg) { LoggerUtils.Debug("[Tpn] 广告关闭了:" + JsonMapper.ToJson(erg.callbackInfo.toDictionary())); ThreadUtils.QueueOnMainThread(pObj => { if (erg.placementId.Equals(topon_rewarded_units)) { _rvCloseCallback?.Invoke(true); _rvCloseCallback = null; } else if (erg.placementId.Equals(topon_interstitial_units)) { _ivCloseCallback?.Invoke(); _ivCloseCallback = null; } }, ""); } private void OnAdVideoClickedEvent(object sender, ATAdEventArgs erg) { LoggerUtils.Debug("[Tpn] 点击广告了:" + JsonMapper.ToJson(erg.callbackInfo.toDictionary())); ThreadUtils.QueueOnMainThread(pObj => { if (erg.placementId.Equals(topon_rewarded_units)) { } else if (erg.placementId.Equals(topon_interstitial_units)) { } }, ""); } #endregion #region 开屏广告功能 public void LoadSplash() { } public bool IsSplashAvailable() { return false; } public void DisplaySplash() { } public double GetSplashRevenue() { return 0; } #endregion #region 原生广告功能 public void LoadNative() { } public double GetNativeRevenue() { return 0; } public void DisplayNative(NativeAdPosition position) { } public bool IsNativeAvailable() { return false; } public void RemoveNative() { } #endregion #region 横幅广告功能 public void LoadBanner() { } public bool IsBannerAvailable() { return false; } public void HideBanner() { } public double GetBannerRevenue() { return 0; } public void DisplayBanner() { } #endregion #region 广告收益上报 public void TrackAdImpression(ATAdEventArgs erg) { AdjustTrackEvent.Instance.TrackAdEvent(erg.callbackInfo.publisher_revenue, ClientName + "_" + erg.callbackInfo.network_firm_id, erg.callbackInfo.adunit_id, erg.callbackInfo.network_placement_id); FireBaseAnalyticsManager.Instance.OnAdRevenueEvent(ClientName, ClientName + "_" + erg.callbackInfo.network_firm_id, erg.callbackInfo.adunit_id, erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded.ToString() : AdsType.Interstitial.ToString(), erg.callbackInfo.publisher_revenue, erg.placementId.Equals(topon_rewarded_units) ? _rvPos : "", AdPlayCountManager.GetAdPlayCount(erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial)); ShuShuEvent.Instance.OnAdRevenueEvent(ClientName, ClientName + "_" + erg.callbackInfo.network_firm_id, erg.callbackInfo.adunit_id, erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded.ToString() : AdsType.Interstitial.ToString(), erg.callbackInfo.publisher_revenue, erg.placementId.Equals(topon_rewarded_units) ? _rvPos : "", AdPlayCountManager.GetAdPlayCount(erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial)); } #endregion } }