using AdsPlatform.Runtime; using AnyThinkAds.Api; using AnyThinkAds.ThirdParty.LitJson; using System.Collections.Generic; using UnityEngine; namespace AnyThinkAds { public class AnyThinkPlatform : IAdsPlatform { /// /// 初始化SDK /// /// 初始化ID /// APP密钥 /// 初始化事件 /// 参数 public void Init(string id, string appKey, InitEvent initEvent, Param param = null) { atRewardedVideoListener = new ATRewardedVideoListener(); ATRewardedVideo.Instance.client.setListener(atRewardedVideoListener); atInterstitialListener = new ATInterstitialListener(); ATInterstitialAd.Instance.client.setListener(atInterstitialListener); atBannerListener = new ATBannerListener(); ATBannerAd.Instance.client.setListener(atBannerListener); if (param != null) { //设置开启Debug日志(强烈建议测试阶段开启,方便排查问题) ATSDKAPI.setLogDebug(param.GetPara("setLogDebug")); //设置询问用户是否同意收集隐私数据 if (param.GetPara("didGetUserLocation")) { //判断是否在欧盟地区 ATSDKAPI.getUserLocation(new GetLocationListener()); } } //(必须配置)SDK的初始化 ATSDKAPI.initSDK(id, appKey, new ATSDKInit(initEvent)); //Use your own app_id & app_key here } private class ATSDKInit : ATSDKInitListener { /// /// 初始化事件 /// private InitEvent m_InitEvent; public ATSDKInit(InitEvent mInitEvent) { m_InitEvent = mInitEvent; } public void initSuccess() { m_InitEvent?.OnInitResult(true); } public void initFail(string message) { m_InitEvent?.OnInitResult(false); } } //发布欧盟地区的开发者需使用以下授权代码,询问用户是否同意收集隐私数据 private class GetLocationListener : ATGetUserLocationListener { public void didGetUserLocation(int location) { Debug.Log("Developer callback didGetUserLocation(): " + location); if (location == ATSDKAPI.kATUserLocationInEU && ATSDKAPI.getGDPRLevel() == ATSDKAPI.UNKNOWN) { ATSDKAPI.showGDPRAuth(); } } } /// /// 插屏回调 /// public static ATInterstitialListener atInterstitialListener; /// /// 加载插屏广告 /// /// 广告位 /// 插屏加载事件 /// 参数 public void LoadInterstitialAd(string placeId, InterstitialLoadEvent interstitialLoadEvent, Param param = null) { atInterstitialListener.SetLoadEvent(interstitialLoadEvent); Dictionary jsonmap = new Dictionary(); jsonmap.Add(AnyThinkAds.Api.ATConst.USE_REWARDED_VIDEO_AS_INTERSTITIAL, AnyThinkAds.Api.ATConst.USE_REWARDED_VIDEO_AS_INTERSTITIAL_NO); //jsonmap.Add(AnyThinkAds.Api.ATConst.USE_REWARDED_VIDEO_AS_INTERSTITIAL, AnyThinkAds.Api.ATConst.USE_REWARDED_VIDEO_AS_INTERSTITIAL_YES); ATInterstitialAd.Instance.loadInterstitialAd(placeId, jsonmap); } /// /// 展示开屏广告 /// /// 广告位 /// 插屏播放事件 /// 参数 public void ShowInterstitialAd(string placeId, InterstitialPlayEvent interstitialPlayEvent, Param param = null) { atInterstitialListener.SetPlayEvent(interstitialPlayEvent); ATInterstitialAd.Instance.showInterstitialAd(placeId); } public class ATInterstitialListener : ATInterstitialAdListener { /// /// 加载事件 /// private InterstitialLoadEvent m_InterstitialLoadEvent; /// /// 播放事件 /// private InterstitialPlayEvent m_InterstitialPlayEvent; /// /// 设置加载事件 /// /// 加载事件 public void SetLoadEvent(InterstitialLoadEvent loadEvent) { m_InterstitialLoadEvent = loadEvent; } /// /// 设置播放事件 /// /// 播放事件 public void SetPlayEvent(InterstitialPlayEvent playEvent) { m_InterstitialPlayEvent = playEvent; } public void onInterstitialAdLoad(string placementId) { m_InterstitialLoadEvent?.OnLoadResult(placementId, true); } public void onInterstitialAdLoadFail(string placementId, string code, string message) { m_InterstitialLoadEvent?.OnLoadResult(placementId, false); } public void onInterstitialAdShow(string placementId, ATCallbackInfo callbackInfo) { m_InterstitialPlayEvent?.OnAdShow(placementId); } public void onInterstitialAdFailedToShow(string placementId) { m_InterstitialPlayEvent?.OnAdFailedToShow(placementId); } public void onInterstitialAdClose(string placementId, ATCallbackInfo callbackInfo) { m_InterstitialPlayEvent?.OnAdClose(placementId); } public void onInterstitialAdClick(string placementId, ATCallbackInfo callbackInfo) { m_InterstitialPlayEvent?.OnAdClick(placementId); } /** *广告开始播放视频(注意:对于Android来说,所有回调方法均不在Unity的主线程) * @param placementId 广告位id */ public void onInterstitialAdStartPlayingVideo(string placementId, ATCallbackInfo callbackInfo) { m_InterstitialPlayEvent?.OnAdStartPlayingVideo(placementId); } /** *广告视频播放结束(注意:对于Android来说,所有回调方法均不在Unity的主线程) * @param placementId 广告位id */ public void onInterstitialAdEndPlayingVideo(string placementId, ATCallbackInfo callbackInfo) { m_InterstitialPlayEvent?.OnAdEndPlayingVideo(placementId); } /** *广告播放视频失败(注意:对于Android来说,所有回调方法均不在Unity的主线程) * @param placementId 广告位id * @param code 错误码 * @param message 错误信息 */ public void onInterstitialAdFailedToPlayVideo(string placementId, string code, string message) { m_InterstitialPlayEvent?.OnAdFailedToPlayVideo(placementId); } 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) { } 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) { } } /// /// 激励视频回调 /// public static ATRewardedVideoListener atRewardedVideoListener; /// /// 加载激励视频广告 /// /// 广告位 /// 激励视频加载事件 /// 参数 public void LoadRewardedVideoAd(string placeId, RewardVideoLoadEvent rewardVideoLoadEvent, Param param = null) { atRewardedVideoListener.SetLoadEvent(rewardVideoLoadEvent); Debug.Log("Developer init video....placementId:" + placeId); Dictionary jsonmap = new Dictionary(); //如果需要通过开发者的服务器进行奖励的下发(部分广告平台支持此服务器激励),则需要传递下面两个key //ATConst.USERID_KEY必传,用于标识每个用户;ATConst.USER_EXTRA_DATA为可选参数,传入后将透传到开发者的服务器 jsonmap.Add(ATConst.USERID_KEY, "test_user_id"); jsonmap.Add(ATConst.USER_EXTRA_DATA, "test_user_extra_data"); ATRewardedVideo.Instance.loadVideoAd(placeId, jsonmap); } /// /// 展示激励视频广告 /// /// 广告位 /// 激励视频播放事件 /// 参数 public void ShowRewardedVideoAd(string placeId, RewardVideoPlayEvent rewardVideoLoadEvent, Param param = null) { atRewardedVideoListener.SetPlayEvent(rewardVideoLoadEvent); Debug.Log("Developer show video...."); ATRewardedVideo.Instance.showAd(placeId); } /// /// 广告是否准备好 /// /// 广告位 /// 是否准备好 public bool IsRewardVideoReady(string placeId) { return ATRewardedVideo.Instance.hasAdReady(placeId); } public class ATRewardedVideoListener : AnyThinkAds.Api.ATRewardedVideoListener { /// /// 激励视频加载事件 /// private RewardVideoLoadEvent m_RewardVideoLoadEvent; /// /// 激励视频播放事件 /// private RewardVideoPlayEvent m_RewardVideoPlayEvent; /// /// 设置加载事件 /// /// 加载事件 public void SetLoadEvent(RewardVideoLoadEvent loadEvent) { this.m_RewardVideoLoadEvent = loadEvent; } /// /// 设置播放事件 /// /// 播放事件 public void SetPlayEvent(RewardVideoPlayEvent playEvent) { this.m_RewardVideoPlayEvent = playEvent; } public void onRewardedVideoAdLoaded(string placementId) { Debug.Log("Developer onRewardedVideoAdLoaded------"); m_RewardVideoLoadEvent?.OnLoadResult(placementId, true); } public void onRewardedVideoAdLoadFail(string placementId, string code, string message) { Debug.Log("Developer onRewardedVideoAdLoadFail------:code" + code + "--message:" + message); m_RewardVideoLoadEvent?.OnLoadResult(placementId, false); } public void onRewardedVideoAdPlayStart(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer onRewardedVideoAdPlayStart------" + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); m_RewardVideoPlayEvent?.OnPlay(placementId); } public void onRewardedVideoAdPlayEnd(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer onRewardedVideoAdPlayEnd------" + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); m_RewardVideoPlayEvent?.OnPlayEnd(placementId); } public void onRewardedVideoAdPlayFail(string placementId, string code, string message) { Debug.Log("Developer onRewardedVideoAdPlayFail------code:" + code + "---message:" + message); m_RewardVideoPlayEvent?.OnPlayFail(placementId); } public void onRewardedVideoAdPlayClosed(string placementId, bool isReward, ATCallbackInfo callbackInfo) { Debug.Log("Developer onRewardedVideoAdPlayClosed------isReward:" + isReward + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); m_RewardVideoPlayEvent?.OnClose(placementId); } public void onRewardedVideoAdPlayClicked(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer onRewardVideoAdPlayClicked------" + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); m_RewardVideoPlayEvent?.OnPlayClick(placementId); } public void onReward(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer onReward------" + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); m_RewardVideoPlayEvent?.OnReward(placementId); } 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) { } 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) { } } /// /// 插屏回调 /// public static ATBannerListener atBannerListener; /// /// 加载横幅广告 /// /// 广告位 /// 横幅事件 /// 参数 public void LoadBannerAd(string placeId, BannerEvent bannerEvent, Param param = null) { atBannerListener.SetBannerEvent(bannerEvent); Dictionary jsonmap = new Dictionary(); #if UNITY_ANDROID ATSize bannerSize = new ATSize(960, 150, true); jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraBannerAdSizeStruct, bannerSize); jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraAdaptiveWidth, bannerSize.width); jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraAdaptiveOrientation, ATBannerAdLoadingExtra.kATBannerAdLoadingExtraAdaptiveOrientationPortrait); #elif UNITY_IOS || UNITY_IPHONE ATSize bannerSize = new ATSize(320, 50, false); jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraBannerAdSizeStruct, bannerSize); jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraAdaptiveWidth, bannerSize.width); jsonmap.Add(ATBannerAdLoadingExtra.kATBannerAdLoadingExtraAdaptiveOrientation, ATBannerAdLoadingExtra.kATBannerAdLoadingExtraAdaptiveOrientationPortrait); #endif ATBannerAd.Instance.loadBannerAd(placeId, jsonmap); } /// /// 移除横幅广告 /// /// 广告位 /// 参数 public void RemoveBannerAd(string placeId, Param param = null) { ATBannerAd.Instance.cleanBannerAd(placeId); } public class ATBannerListener : ATBannerAdListener { /// /// 横幅事件 /// private BannerEvent m_BannerEvent; /// /// 设置横幅事件 /// /// 事件 public void SetBannerEvent(BannerEvent bannerEvent) { m_BannerEvent = bannerEvent; } public void onAdAutoRefresh(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer callback onAdAutoRefresh :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); m_BannerEvent?.OnAutoRefresh(placementId); } public void onAdAutoRefreshFail(string placementId, string code, string message) { Debug.Log("Developer callback onAdAutoRefreshFail : " + placementId + "--code:" + code + "--msg:" + message); m_BannerEvent?.OnAutoRefreshFailed(placementId); } public void onAdClick(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer callback onAdClick :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); m_BannerEvent?.OnClick(placementId); } public void onAdClose(string placementId) { Debug.Log("Developer callback onAdClose :" + placementId); m_BannerEvent?.OnClose(placementId); } public void onAdCloseButtonTapped(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer callback onAdCloseButtonTapped :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); m_BannerEvent?.OnClose(placementId); } public void startLoadingADSource(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer callback startLoadingADSource :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); } public void finishLoadingADSource(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer callback finishLoadingADSource :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); } public void failToLoadADSource(string placementId, ATCallbackInfo callbackInfo, string code, string message) { Debug.Log("Developer callback failToLoadADSource :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); } public void startBiddingADSource(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer callback startBiddingADSource :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); } public void finishBiddingADSource(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer callback finishBiddingADSource :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); } public void failBiddingADSource(string placementId, ATCallbackInfo callbackInfo, string code, string message) { Debug.Log("Developer callback failBiddingADSource :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); } public void onAdImpress(string placementId, ATCallbackInfo callbackInfo) { Debug.Log("Developer callback onAdImpress :" + placementId + "->" + JsonMapper.ToJson(callbackInfo.toDictionary())); m_BannerEvent?.OnImpress(placementId); } public void onAdLoad(string placementId) { Debug.Log("Developer callback onAdLoad :" + placementId); m_BannerEvent?.OnLoadResult(placementId, true); } public void onAdLoadFail(string placementId, string code, string message) { Debug.Log("Developer callback onAdLoadFail : : " + placementId + "--code:" + code + "--msg:" + message); m_BannerEvent?.OnLoadResult(placementId, false); } } } }