SDK_UnityMoney/Assets/Script/SDKManager/AdsSDKManager/TpnAdsManager/TpnAdsManager.cs

414 lines
17 KiB
C#
Raw Normal View History

2025-08-31 05:15:14 +00:00
using System;
2025-08-31 01:11:05 +00:00
using System.Collections;
using System.Collections.Generic;
2025-09-01 03:39:21 +00:00
using System.Linq;
2025-08-31 01:11:05 +00:00
using System.Threading;
using AnyThinkAds.Api;
using AnyThinkAds.ThirdParty.LitJson;
using UnityEngine;
2025-09-01 10:32:50 +00:00
namespace WZ
2025-08-31 01:11:05 +00:00
{
2025-09-02 09:14:49 +00:00
public class TpnAdsManager : D_MonoSingleton<TpnAdsManager>, IAdService
2025-08-31 01:11:05 +00:00
{
2025-09-01 07:14:26 +00:00
public string ClientName => "Topon";
public PlatformType Platfrom => PlatformType.Topon;
2025-09-01 12:53:08 +00:00
public string _topon_app_id;
public string _topon_app_key;
public string _topon_interstitial_units;
public string _topon_rewarded_units;
2026-01-06 02:31:49 +00:00
public string _topon_banner_units;
2025-08-31 05:15:14 +00:00
private string _rvPos;
private string _ivPos;
private Action<bool, double> _rvCloseCallback = null;
2025-09-01 14:14:33 +00:00
private Action<double> _ivCloseCallback = null;
2025-08-31 05:15:14 +00:00
private Action _rvShowFailedCallback = null;
public bool _initialized { get; private set; } = false;
private bool _receivedReward = false;
2026-01-06 02:31:49 +00:00
private bool bannerReady = false;
private bool bannerHide = false;
2026-01-04 10:20:54 +00:00
public void ParseTpnConfig()
2025-08-31 01:11:05 +00:00
{
2026-01-04 10:20:54 +00:00
var config = AdConfigParser.Parse("adUnits_tp");
_topon_app_id = config.topon_app_id;
_topon_app_key = config.topon_app_key;
_topon_interstitial_units = config.topon_interstitial_units.FirstOrDefault();
_topon_rewarded_units = config.topon_rewarded_units.FirstOrDefault();
2026-01-06 02:31:49 +00:00
_topon_banner_units = config.topon_banner_units.FirstOrDefault();
2026-01-04 10:20:54 +00:00
}
2026-01-04 10:20:54 +00:00
public void Initialize()
{
2025-09-01 12:53:08 +00:00
LoggerUtils.Debug("[Tpn] 初始化 Tpn 广告 SDK appid:" + _topon_app_id + " appkey:" + _topon_app_key + " ivId:" + _topon_interstitial_units + " rvId:" + _topon_rewarded_units);
if (_initialized) return;
2025-09-01 12:53:08 +00:00
if (!string.IsNullOrEmpty(_topon_app_id) && !string.IsNullOrEmpty(_topon_app_key))
2025-08-31 01:11:05 +00:00
{
2025-09-01 12:53:08 +00:00
ATSDKAPI.initSDK(_topon_app_id, _topon_app_key);
_initialized = true;
2025-08-31 01:11:05 +00:00
ATSDKAPI.setLogDebug(false);
InitRewarded();
InitInterstitial();
2026-01-06 02:31:49 +00:00
SetBanner();
2025-08-31 01:11:05 +00:00
}
}
2025-09-01 12:53:08 +00:00
public void RefreshAdsData()
{
2026-01-04 10:20:54 +00:00
2025-09-01 12:53:08 +00:00
}
2025-08-31 01:11:05 +00:00
#region 广
private void InitRewarded()
{
2025-09-01 12:53:08 +00:00
if (string.IsNullOrEmpty(_topon_rewarded_units)) return;
2025-08-31 01:11:05 +00:00
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.client.onRewardEvent += OnAdRewardEvent;
2025-09-01 12:53:08 +00:00
ATRewardedAutoVideo.Instance.addAutoLoadAdPlacementID(new string[] { _topon_rewarded_units });
2025-08-31 01:11:05 +00:00
2025-09-15 05:59:03 +00:00
AdsActionEvents.TrackAdStartLoad(Platfrom, AdsType.Rewarded);
2025-09-01 07:14:26 +00:00
2025-08-31 01:11:05 +00:00
}
public void LoadRewarded() { }
public void DisplayRewarded(string adPos, Action<bool, double> rewardCallback = null, Action showFailedCallback = null)
2025-08-31 01:11:05 +00:00
{
2025-08-31 05:15:14 +00:00
_rvPos = adPos;
_rvCloseCallback = rewardCallback;
_rvShowFailedCallback = showFailedCallback;
2025-09-01 12:53:08 +00:00
ATRewardedAutoVideo.Instance.showAutoAd(_topon_rewarded_units);
2025-08-31 01:11:05 +00:00
}
public double GetRewardedRevenue() { return 0; }
public bool IsRewardedAvailable()
{
2026-01-06 03:04:34 +00:00
if(!_initialized && AdmobAdsManager.Instance._initialized) Initialize();
2025-09-01 12:53:08 +00:00
return string.IsNullOrEmpty(_topon_rewarded_units) ? false : ATRewardedAutoVideo.Instance.autoLoadRewardedVideoReadyForPlacementID(_topon_rewarded_units);
2025-08-31 01:11:05 +00:00
}
#endregion
#region 广
private void InitInterstitial()
{
2025-09-01 12:53:08 +00:00
if (string.IsNullOrEmpty(_topon_interstitial_units)) return;
2025-08-31 01:11:05 +00:00
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;
2025-09-01 12:53:08 +00:00
ATInterstitialAutoAd.Instance.addAutoLoadAdPlacementID(new string[] { _topon_interstitial_units });
2025-09-15 05:59:03 +00:00
AdsActionEvents.TrackAdStartLoad(Platfrom, AdsType.Interstitial);
2025-08-31 01:11:05 +00:00
}
public void LoadInterstitial() { }
public double GetInterstitialRevenue() { return 0; }
2025-08-31 01:11:05 +00:00
2025-09-01 14:14:33 +00:00
public void DisplayInterstitial(string ivPos, IvType _IvType = IvType.IV1, Action<double> closeCallback = null)
2025-08-31 01:11:05 +00:00
{
2025-08-31 05:15:14 +00:00
_ivPos = ivPos;
_ivCloseCallback = closeCallback;
2025-09-01 12:53:08 +00:00
ATInterstitialAutoAd.Instance.showAutoAd(_topon_interstitial_units);
2025-08-31 01:11:05 +00:00
}
public bool IsInterstitialAvailable()
{
2026-01-06 03:04:34 +00:00
if(!_initialized && AdmobAdsManager.Instance._initialized) Initialize();
2025-09-01 12:53:08 +00:00
return string.IsNullOrEmpty(_topon_interstitial_units) ? false : ATInterstitialAutoAd.Instance.autoLoadInterstitialAdReadyForPlacementID(_topon_interstitial_units);
2025-08-31 01:11:05 +00:00
}
#endregion
2026-01-06 02:31:49 +00:00
#region banner
private void SetBanner()
{
if (string.IsNullOrEmpty(_topon_banner_units)) return;
ATBannerAd.Instance.client.onAdLoadEvent += OnAdLoadedEvent;
ATBannerAd.Instance.client.onAdLoadFailureEvent += OnAdLoadedFailEvent;
ATBannerAd.Instance.client.onAdImpressEvent += OnAdVideoStartEvent;
ATBannerAd.Instance.client.onAdAutoRefreshEvent += OnAdVideoStartEvent;
LoadBannerAd();
}
public void LoadBannerAd()
{
Dictionary<string, object> jsonmap = new Dictionary<string,object>();
//配置Banner要展示的宽度高度是否使用pixel为单位只针对iOS有效Android 使用pixel为单位注意不同平台的横幅广告有一定限制例如配置的穿山甲横幅广告640*100为了能填充完屏幕宽计算高度H = (屏幕宽 *100)/640那么在load的extra的size为屏幕宽H
ATSize bannerSize = new ATSize(Screen.width - 100, 150, true);
jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraBannerAdSizeStruct, bannerSize);
ATBannerAd.Instance.loadBannerAd(_topon_banner_units, jsonmap);
}
public bool IsBannerAvailable()
{
2026-01-06 03:04:34 +00:00
if(!_initialized && AdmobAdsManager.Instance._initialized) Initialize();
2026-01-06 02:31:49 +00:00
return string.IsNullOrEmpty(_topon_banner_units) ? false : bannerReady;
}
public void ShowBanner()
{
if (bannerHide)
{
ATBannerAd.Instance.showBannerAd(_topon_banner_units);
}
else
{
ATBannerAd.Instance.showBannerAd(_topon_banner_units, ATBannerAdLoadingExtra.kATBannerAdShowingPisitionBottom);
}
}
public void HideBanner()
{
bannerHide = true;
ATBannerAd.Instance.hideBannerAd(_topon_banner_units);
}
#endregion
2025-08-31 01:11:05 +00:00
#region
private void OnAdLoadedEvent(object sender, ATAdEventArgs erg)
{
2025-09-01 07:14:26 +00:00
2025-09-01 12:53:08 +00:00
if (erg.placementId.Equals(_topon_interstitial_units))
2025-08-31 01:11:05 +00:00
{
LoggerUtils.Debug("[Tpn] ads tpn topon interstitial loaded");
var info = JsonUtility.FromJson<TopOnCallback>(ATInterstitialAutoAd.Instance.checkAutoAdStatus(erg.placementId));
AdsActionEvents.TrackAdLoaded(Platfrom,
ClientName + "_" + info.adInfo.network_firm_id,
info.adInfo.adunit_id,
2026-01-06 02:31:49 +00:00
AdsType.Interstitial,
0);
AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Interstitial);
2025-08-31 01:11:05 +00:00
}
2025-09-01 12:53:08 +00:00
else if (erg.placementId.Equals(_topon_rewarded_units))
2025-08-31 01:11:05 +00:00
{
LoggerUtils.Debug("[Tpn] ads tpn topon rewarded loaded");
var info = JsonUtility.FromJson<TopOnCallback>(ATRewardedAutoVideo.Instance.checkAutoAdStatus(erg.placementId));
AdsActionEvents.TrackAdLoaded(Platfrom,
ClientName + "_" + info.adInfo.network_firm_id,
info.adInfo.adunit_id,
2026-01-06 02:31:49 +00:00
AdsType.Rewarded,
0);
AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Rewarded);
2026-01-06 02:31:49 +00:00
}else if(erg.placementId.Equals(_topon_banner_units))
{
LoggerUtils.Debug("[Tpn] ads tpn topon banner loaded");
var info = JsonUtility.FromJson<TopOnCallback>(ATRewardedAutoVideo.Instance.checkAutoAdStatus(erg.placementId));
AdsActionEvents.TrackAdLoaded(Platfrom,
ClientName + "_" + info.adInfo.network_firm_id,
info.adInfo.adunit_id,
AdsType.Banner,
0);
AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Banner);
2025-08-31 01:11:05 +00:00
}
}
private void OnAdLoadedFailEvent(object sender, ATAdErrorEventArgs erg)
{
2026-01-06 02:31:49 +00:00
var adsType = AdsType.Rewarded;
if(erg.placementId.Equals(_topon_interstitial_units)) adsType = AdsType.Interstitial;
if(erg.placementId.Equals(_topon_banner_units)) adsType = AdsType.Banner;
LoggerUtils.Debug("[Tpn] 广告加载失败:" + JsonMapper.ToJson(erg.callbackInfo.toDictionary()));
2025-09-01 07:14:26 +00:00
AdsActionEvents.TrackAdFailToLoad(Platfrom,
ClientName + "_" + erg.callbackInfo.network_firm_id,
erg.callbackInfo.adunit_id,
2026-01-06 02:31:49 +00:00
adsType,
2025-09-01 07:14:26 +00:00
0,
erg.errorMessage);
2025-08-31 01:11:05 +00:00
}
private void OnAdVideoStartEvent(object sender, ATAdEventArgs erg)
{
LoggerUtils.Debug("[Tpn] 广告开始播放:" + JsonMapper.ToJson(erg.callbackInfo.toDictionary()));
TrackAdImpression(erg);
2025-08-31 01:11:05 +00:00
ThreadUtils.QueueOnMainThread(pObj =>
{
2025-09-01 12:53:08 +00:00
if (erg.placementId.Equals(_topon_rewarded_units))
2025-08-31 01:11:05 +00:00
{
}
2025-09-01 12:53:08 +00:00
else if (erg.placementId.Equals(_topon_interstitial_units))
2025-08-31 01:11:05 +00:00
{
}
}, "");
}
private void OnAdVideoFailureEvent(object sender, ATAdErrorEventArgs erg)
{
LoggerUtils.Debug("[Tpn] 广告播放失败" + JsonMapper.ToJson(erg.callbackInfo.toDictionary()));
ThreadUtils.QueueOnMainThread(pObj =>
{
2025-09-01 12:53:08 +00:00
if (erg.placementId.Equals(_topon_rewarded_units))
2025-08-31 01:11:05 +00:00
{
2025-08-31 05:15:14 +00:00
_rvShowFailedCallback?.Invoke();
_rvShowFailedCallback = null;
2025-08-31 01:11:05 +00:00
}
2025-09-01 12:53:08 +00:00
else if (erg.placementId.Equals(_topon_interstitial_units))
2025-08-31 01:11:05 +00:00
{
2025-09-01 14:14:33 +00:00
_ivCloseCallback?.Invoke(0);
2025-08-31 05:15:14 +00:00
_ivCloseCallback = null;
2025-08-31 01:11:05 +00:00
}
}, "");
}
private void OnAdRewardEvent(object sender, ATAdEventArgs erg)
{
LoggerUtils.Debug("[Tpn] 广告奖励");
_receivedReward = true;
}
2025-08-31 01:11:05 +00:00
private void OnAdVideoClosedEvent(object sender, ATAdEventArgs erg)
{
LoggerUtils.Debug("[Tpn] 广告关闭了:" + JsonMapper.ToJson(erg.callbackInfo.toDictionary()));
2025-09-01 07:14:26 +00:00
AdsActionEvents.TrackAdClosed(Platfrom,
ClientName + "_" + erg.callbackInfo.network_firm_id,
erg.callbackInfo.adunit_id,
2025-09-01 12:53:08 +00:00
erg.placementId.Equals(_topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial,
2025-09-09 06:19:16 +00:00
erg.placementId.Equals(_topon_rewarded_units) ? _rvPos : _ivPos,
2025-09-01 07:14:26 +00:00
erg.callbackInfo.publisher_revenue);
2025-08-31 01:11:05 +00:00
ThreadUtils.QueueOnMainThread(pObj =>
{
2025-09-01 12:53:08 +00:00
if (erg.placementId.Equals(_topon_rewarded_units))
2025-08-31 01:11:05 +00:00
{
_rvCloseCallback?.Invoke(_receivedReward, erg.callbackInfo.publisher_revenue);
2025-08-31 05:15:14 +00:00
_rvCloseCallback = null;
_receivedReward = false;
2025-08-31 01:11:05 +00:00
}
2025-09-01 12:53:08 +00:00
else if (erg.placementId.Equals(_topon_interstitial_units))
2025-08-31 01:11:05 +00:00
{
2025-09-01 14:14:33 +00:00
_ivCloseCallback?.Invoke(erg.callbackInfo.publisher_revenue);
2025-08-31 05:15:14 +00:00
_ivCloseCallback = null;
2025-08-31 01:11:05 +00:00
}
}, "");
}
private void OnAdVideoClickedEvent(object sender, ATAdEventArgs erg)
{
LoggerUtils.Debug("[Tpn] 点击广告了:" + JsonMapper.ToJson(erg.callbackInfo.toDictionary()));
2025-09-01 07:14:26 +00:00
AdsActionEvents.TrackAdClicked(Platfrom,
ClientName + "_" + erg.callbackInfo.network_firm_id,
erg.callbackInfo.adunit_id,
2025-09-01 12:53:08 +00:00
erg.placementId.Equals(_topon_rewarded_units) ? AdsType.Rewarded : AdsType.Interstitial,
2025-09-09 06:19:16 +00:00
erg.placementId.Equals(_topon_rewarded_units) ? _rvPos : _ivPos,
2025-09-01 07:14:26 +00:00
erg.callbackInfo.publisher_revenue);
2025-08-31 01:11:05 +00:00
ThreadUtils.QueueOnMainThread(pObj =>
{
2025-09-01 12:53:08 +00:00
if (erg.placementId.Equals(_topon_rewarded_units))
2025-08-31 01:11:05 +00:00
{
}
2025-09-01 12:53:08 +00:00
else if (erg.placementId.Equals(_topon_interstitial_units))
2025-08-31 01:11:05 +00:00
{
}
}, "");
}
#endregion
#region 广
public void LoadSplash() { }
public bool IsSplashAvailable() { return false; }
public void DisplaySplash() { }
public double GetSplashRevenue() { return 0; }
2025-08-31 01:11:05 +00:00
#endregion
#region 广
public void LoadNative() { }
2025-09-02 02:07:10 +00:00
public bool IsNativeAvailable(string adUnitId)
{
return false;
}
public void DisplayNative(string _adPos, string adUnitId, NativeAdPosition position)
{
2025-09-02 02:07:10 +00:00
}
public void RemoveNative(string adUnitId)
{
2025-09-02 02:07:10 +00:00
}
public double GetNativeRevenue(string adUnitId)
{
return 0;
}
public double GetNativeRevenue() { return 0; }
public void DisplayNative(NativeAdPosition position) { }
public bool IsNativeAvailable() { return false; }
public void RemoveNative() { }
2025-08-31 01:11:05 +00:00
#endregion
#region 广
public void LoadBanner() { }
2025-09-16 13:29:51 +00:00
public bool IsBannerAvailable(BannerType bannerType) { return false; }
public void HideBanner(BannerType bannerType) { }
public double GetBannerRevenue(BannerType bannerType) { return 0; }
public void DisplayBanner(BannerType bannerType,BannerAlignType bannerAlignType) { }
2025-08-31 01:11:05 +00:00
#endregion
2025-08-31 13:19:46 +00:00
#region 广
public void TrackAdImpression(ATAdEventArgs erg)
{
2026-01-06 02:31:49 +00:00
var adsType = AdsType.Rewarded;
if(erg.placementId.Equals(_topon_interstitial_units)) adsType = AdsType.Interstitial;
if(erg.placementId.Equals(_topon_banner_units)) adsType = AdsType.Banner;
var _pos = "";
if(erg.placementId.Equals(_topon_interstitial_units)) _pos = _ivPos;
if(erg.placementId.Equals(_topon_banner_units)) _pos = _rvPos;
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,
2026-01-06 02:31:49 +00:00
adsType,
erg.callbackInfo.publisher_revenue,
2025-09-09 06:19:16 +00:00
erg.placementId.Equals(_topon_rewarded_units) ? _rvPos : _ivPos,
2026-01-06 02:31:49 +00:00
AdPlayCountManager.GetAdsActionCount(adsType,AdPlayCountManager.PLAY_COUNT_SUFFIX));
ShuShuEvent.Instance.OnAdRevenueEvent(ClientName,
ClientName + "_" + erg.callbackInfo.network_firm_id,
erg.callbackInfo.adunit_id,
2026-01-06 02:31:49 +00:00
adsType.ToString(),
erg.callbackInfo.publisher_revenue,
2026-01-06 02:31:49 +00:00
_pos,
AdPlayCountManager.GetAdsActionCount(adsType,AdPlayCountManager.PLAY_COUNT_SUFFIX));
}
2025-08-31 13:19:46 +00:00
#endregion
[Serializable]
public class TopOnCallback
{
public bool isLoading;
public bool isReady;
public TopOnCallbackInfo adInfo;
}
[Serializable]
public class TopOnCallbackInfo
{
public double publisher_revenue;
public string adunit_id;
public int network_firm_id;
}
2025-08-31 01:11:05 +00:00
}
}