292 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			292 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | |
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| using System.Threading;
 | |
| using AnyThinkAds.Api;
 | |
| using AnyThinkAds.ThirdParty.LitJson;
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace WZ
 | |
| {
 | |
|     public class TpnAdsManager : NormalSingleton<TpnAdsManager>, IAdService
 | |
|     {
 | |
|         public string ClientName => "Topon";
 | |
|         public PlatformType Platfrom => PlatformType.Topon;
 | |
|         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<bool> _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).FirstOrDefault();
 | |
|             topon_rewarded_units = AdConfigParser.GetTopOnAdUnits(AdsType.Rewarded).FirstOrDefault();
 | |
|             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 });
 | |
| 
 | |
|             AdsActionEvents.TrackAdStartLoad(Platfrom,"","",AdsType.Rewarded);
 | |
| 
 | |
|         }
 | |
|         public void LoadRewarded() { }
 | |
|         public void DisplayRewarded(string adPos, Action<bool> 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 });
 | |
|             AdsActionEvents.TrackAdStartLoad(Platfrom,"","",AdsType.Interstitial);
 | |
|         }
 | |
| 
 | |
|         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)
 | |
|         {
 | |
|             AdsActionEvents.TrackAdLoaded(Platfrom,
 | |
|                 ClientName + "_" + erg.callbackInfo.network_firm_id,
 | |
|                 erg.callbackInfo.adunit_id,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial,
 | |
|                 0);
 | |
| 
 | |
|             if (erg.placementId.Equals(topon_interstitial_units))
 | |
|             {
 | |
|                 LoggerUtils.Debug("[Tpn] ads tpn topon interstitial loaded");
 | |
|                 AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Interstitial);
 | |
|             }
 | |
|             else if (erg.placementId.Equals(topon_rewarded_units))
 | |
|             {
 | |
|                 LoggerUtils.Debug("[Tpn] ads tpn topon rewarded loaded");
 | |
|                 AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Rewarded);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         private void OnAdLoadedFailEvent(object sender, ATAdErrorEventArgs erg)
 | |
|         {
 | |
|             LoggerUtils.Debug("[Tpn] 广告加载失败:" + JsonMapper.ToJson(erg.callbackInfo.toDictionary()));
 | |
|             AdsActionEvents.TrackAdFailToLoad(Platfrom,
 | |
|                 ClientName + "_" + erg.callbackInfo.network_firm_id,
 | |
|                 erg.callbackInfo.adunit_id,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial,
 | |
|                 0,
 | |
|                 erg.errorMessage);
 | |
|         }
 | |
| 
 | |
|         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()));
 | |
| 
 | |
|             AdsActionEvents.TrackAdFailToShow(Platfrom,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial,
 | |
|                 erg.errorMessage,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? _rvPos : "");
 | |
| 
 | |
|             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()));
 | |
| 
 | |
|             AdsActionEvents.TrackAdClosed(Platfrom,
 | |
|                 ClientName + "_" + erg.callbackInfo.network_firm_id,
 | |
|                 erg.callbackInfo.adunit_id,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? _rvPos : "",
 | |
|                 erg.callbackInfo.publisher_revenue);
 | |
| 
 | |
|             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()));
 | |
| 
 | |
|             AdsActionEvents.TrackAdClicked(Platfrom,
 | |
|                 ClientName + "_" + erg.callbackInfo.network_firm_id,
 | |
|                 erg.callbackInfo.adunit_id,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? _rvPos : "",
 | |
|                 erg.callbackInfo.publisher_revenue);
 | |
| 
 | |
|             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 : AdsType.Interstitial,
 | |
|                 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));
 | |
| 
 | |
|             AdsActionEvents.TrackAdImpression(Platfrom,
 | |
|                 ClientName + "_" + erg.callbackInfo.network_firm_id,
 | |
|                 erg.callbackInfo.adunit_id,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial,
 | |
|                 erg.placementId.Equals(topon_rewarded_units) ? _rvPos : "",
 | |
|                 erg.callbackInfo.publisher_revenue);
 | |
|         }
 | |
|         #endregion
 | |
|     }
 | |
| } |