411 lines
16 KiB
C#
411 lines
16 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 : D_MonoSingleton<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;
|
||
public string _topon_banner_units;
|
||
private string _rvPos;
|
||
private string _ivPos;
|
||
private Action<bool, double> _rvCloseCallback = null;
|
||
private Action<double> _ivCloseCallback = null;
|
||
private Action _rvShowFailedCallback = null;
|
||
public bool _initialized { get; private set; } = false;
|
||
private bool _receivedReward = false;
|
||
private bool bannerReady = false;
|
||
private bool bannerHide = false;
|
||
|
||
public void ParseTpnConfig()
|
||
{
|
||
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();
|
||
_topon_banner_units = config.topon_banner_units.FirstOrDefault();
|
||
}
|
||
|
||
public void Initialize()
|
||
{
|
||
LoggerUtils.Debug("[Tpn] 初始化 Tpn 广告 SDK appid:" + _topon_app_id + " appkey:" + _topon_app_key + " ivId:" + _topon_interstitial_units + " rvId:" + _topon_rewarded_units);
|
||
if (_initialized) return;
|
||
if (!string.IsNullOrEmpty(_topon_app_id) && !string.IsNullOrEmpty(_topon_app_key))
|
||
{
|
||
ATSDKAPI.initSDK(_topon_app_id, _topon_app_key);
|
||
_initialized = true;
|
||
ATSDKAPI.setLogDebug(false);
|
||
InitRewarded();
|
||
InitInterstitial();
|
||
SetBanner();
|
||
}
|
||
}
|
||
|
||
public void RefreshAdsData()
|
||
{
|
||
|
||
}
|
||
#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.client.onRewardEvent += OnAdRewardEvent;
|
||
ATRewardedAutoVideo.Instance.addAutoLoadAdPlacementID(new string[] { _topon_rewarded_units });
|
||
|
||
AdsActionEvents.TrackAdStartLoad(Platfrom, AdsType.Rewarded);
|
||
|
||
}
|
||
public void LoadRewarded() { }
|
||
public void DisplayRewarded(string adPos, Action<bool, double> 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<double> 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 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()
|
||
{
|
||
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
|
||
|
||
#region 代理
|
||
private void OnAdLoadedEvent(object sender, ATAdEventArgs erg)
|
||
{
|
||
|
||
if (erg.placementId.Equals(_topon_interstitial_units))
|
||
{
|
||
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,
|
||
AdsType.Interstitial,
|
||
0);
|
||
AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Interstitial);
|
||
}
|
||
else if (erg.placementId.Equals(_topon_rewarded_units))
|
||
{
|
||
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,
|
||
AdsType.Rewarded,
|
||
0);
|
||
AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Rewarded);
|
||
}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);
|
||
}
|
||
}
|
||
|
||
private void OnAdLoadedFailEvent(object sender, ATAdErrorEventArgs erg)
|
||
{
|
||
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()));
|
||
AdsActionEvents.TrackAdFailToLoad(Platfrom,
|
||
ClientName + "_" + erg.callbackInfo.network_firm_id,
|
||
erg.callbackInfo.adunit_id,
|
||
adsType,
|
||
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()));
|
||
ThreadUtils.QueueOnMainThread(pObj =>
|
||
{
|
||
if (erg.placementId.Equals(_topon_rewarded_units))
|
||
{
|
||
_rvShowFailedCallback?.Invoke();
|
||
_rvShowFailedCallback = null;
|
||
}
|
||
else if (erg.placementId.Equals(_topon_interstitial_units))
|
||
{
|
||
_ivCloseCallback?.Invoke(0);
|
||
_ivCloseCallback = null;
|
||
}
|
||
}, "");
|
||
}
|
||
|
||
private void OnAdRewardEvent(object sender, ATAdEventArgs erg)
|
||
{
|
||
LoggerUtils.Debug("[Tpn] 广告奖励");
|
||
_receivedReward = true;
|
||
}
|
||
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 : _ivPos,
|
||
erg.callbackInfo.publisher_revenue);
|
||
|
||
ThreadUtils.QueueOnMainThread(pObj =>
|
||
{
|
||
if (erg.placementId.Equals(_topon_rewarded_units))
|
||
{
|
||
_rvCloseCallback?.Invoke(_receivedReward, erg.callbackInfo.publisher_revenue);
|
||
_rvCloseCallback = null;
|
||
_receivedReward = false;
|
||
}
|
||
else if (erg.placementId.Equals(_topon_interstitial_units))
|
||
{
|
||
_ivCloseCallback?.Invoke(erg.callbackInfo.publisher_revenue);
|
||
_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 : _ivPos,
|
||
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 bool IsNativeAvailable(string adUnitId)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
public void DisplayNative(string _adPos, string adUnitId, NativeAdPosition position)
|
||
{
|
||
|
||
}
|
||
|
||
public void RemoveNative(string adUnitId)
|
||
{
|
||
|
||
}
|
||
|
||
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() { }
|
||
|
||
#endregion
|
||
|
||
#region 横幅广告功能
|
||
public void LoadBanner() { }
|
||
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) { }
|
||
#endregion
|
||
|
||
#region 广告收益上报
|
||
public void TrackAdImpression(ATAdEventArgs erg)
|
||
{
|
||
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,
|
||
adsType,
|
||
erg.callbackInfo.publisher_revenue,
|
||
erg.placementId.Equals(_topon_rewarded_units) ? _rvPos : _ivPos,
|
||
AdPlayCountManager.GetAdsActionCount(adsType,AdPlayCountManager.PLAY_COUNT_SUFFIX));
|
||
|
||
ShuShuEvent.Instance.OnAdRevenueEvent(ClientName,
|
||
ClientName + "_" + erg.callbackInfo.network_firm_id,
|
||
erg.callbackInfo.adunit_id,
|
||
adsType.ToString(),
|
||
erg.callbackInfo.publisher_revenue,
|
||
_pos,
|
||
AdPlayCountManager.GetAdsActionCount(adsType,AdPlayCountManager.PLAY_COUNT_SUFFIX));
|
||
}
|
||
#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;
|
||
|
||
}
|
||
}
|
||
} |