452 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			452 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | ||
| using System.Collections.Generic;
 | ||
| using System.Linq;
 | ||
| using Firebase.RemoteConfig;
 | ||
| using Newtonsoft.Json;
 | ||
| using UnityEngine;
 | ||
| 
 | ||
| namespace WZ
 | ||
| {
 | ||
|     public class AdsSDKManager : NormalSingleton<AdsSDKManager>
 | ||
|     {
 | ||
|         private List<IAdService> _adNetworks = new List<IAdService>();
 | ||
|         // 是否有激励视频或者插屏广告在展示
 | ||
|         public bool otherAdsOnShow = false;
 | ||
|         /// <summary>
 | ||
|         /// 看完广告的回调
 | ||
|         /// </summary>
 | ||
|         private Action<double> AdRewardCallback;
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 广告竞价开关:激励、插屏、全屏Native广告竞价
 | ||
|         /// </summary>
 | ||
|         public bool IsMoreAdsBidding = false;
 | ||
| 
 | ||
|         public Action OnSplashAdCloseCallback;
 | ||
| 
 | ||
|         public void InitSDK(Action action)
 | ||
|         {
 | ||
|             OnSplashAdCloseCallback = action;
 | ||
|             // 初始化广告平台状态
 | ||
|             BidPlatformManager.Instance.InitializePlatformStates(AdConfigParser.GetAdExpireInSec());
 | ||
|             InitializeAdNetworks();
 | ||
|         }
 | ||
| 
 | ||
|         private void InitializeAdNetworks()
 | ||
|         {
 | ||
|             _adNetworks.Add(AdmobAdsManager.Instance);
 | ||
|             _adNetworks.Add(BigoAdsManager.Instance);
 | ||
|             _adNetworks.Add(TpnAdsManager.Instance);
 | ||
|             _adNetworks.Add(MaxAdsManager.Instance);
 | ||
| 
 | ||
|             foreach (var network in _adNetworks)
 | ||
|             {
 | ||
|                 network.RefreshAdsData();
 | ||
|                 network.Initialize();
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         public void RefreshAdsData()
 | ||
|         {
 | ||
|             foreach (var network in _adNetworks)
 | ||
|             {
 | ||
|                 network.RefreshAdsData();
 | ||
|             }
 | ||
|         }
 | ||
| #region  激励视频广告
 | ||
|         public bool IsRewardAdReady()
 | ||
|         {
 | ||
|             return _adNetworks.Any(network => network.IsRewardedAvailable());
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 展示激励广告
 | ||
|         /// </summary>
 | ||
|         /// <param name="callback"></param>
 | ||
|         public void ShowRewardAd(string _adPos, Action<bool, double> _rewardCallback = null, Action _showFailedCallback = null)
 | ||
|         {
 | ||
|             AdsActionEvents.TrackAdPosition(AdsType.Rewarded, _adPos);
 | ||
|             if (!IsRewardAdReady())
 | ||
|             {
 | ||
|                 _showFailedCallback?.Invoke();
 | ||
|                 return;
 | ||
|             }
 | ||
|             PlatformType result = GetBestPlatformType(false);
 | ||
|             BidPlatformManager.Instance.RecordBidSuccess(result, AdsType.Rewarded);
 | ||
|             if (result == PlatformType.AppLovin)
 | ||
|             {
 | ||
|                 MaxAdsManager.Instance.DisplayRewarded(_adPos, _rewardCallback, _showFailedCallback);
 | ||
|             }
 | ||
|             else if (result == PlatformType.Admob)
 | ||
|             {
 | ||
|                 AdmobAdsManager.Instance.DisplayRewarded(_adPos, _rewardCallback, _showFailedCallback);
 | ||
|             }
 | ||
|             else if (result == PlatformType.Bigo)
 | ||
|             {
 | ||
|                 BigoAdsManager.Instance.DisplayRewarded(_adPos, _rewardCallback, _showFailedCallback);
 | ||
|             }
 | ||
|             else if (result == PlatformType.Topon)
 | ||
|             {
 | ||
|                 TpnAdsManager.Instance.DisplayRewarded(_adPos, _rewardCallback, _showFailedCallback);
 | ||
|             }
 | ||
|             else if (result == PlatformType.Kwai)
 | ||
|             {
 | ||
| 
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 _showFailedCallback?.Invoke();
 | ||
|             }
 | ||
|             AdPlayCountManager.IncrementAdPlayCount(AdsType.Rewarded);
 | ||
|             CheckAndRefreshExpiredBids(AdsType.Rewarded);
 | ||
|         }
 | ||
| #endregion
 | ||
| 
 | ||
| #region  插屏广告
 | ||
|         public bool IsInterstitialReady()
 | ||
|         {
 | ||
|             return _adNetworks.Any(network => network.IsInterstitialAvailable());
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 展示激励广告
 | ||
|         /// </summary>
 | ||
|         /// <param name="callback"></param>
 | ||
|         public void ShowInterstitialAd(string _adPos, IvType _IvType = IvType.IV1, Action<double> _closeCallback = null)
 | ||
|         {
 | ||
|             AdsActionEvents.TrackAdPosition(AdsType.Interstitial, _adPos);
 | ||
|             if (!IsRewardAdReady())
 | ||
|             {
 | ||
|                 _closeCallback?.Invoke(0);
 | ||
|                 _closeCallback = null;
 | ||
|                 return;
 | ||
|             }
 | ||
|             otherAdsOnShow = true;
 | ||
|             PlatformType result = GetBestPlatformType(true);
 | ||
|             BidPlatformManager.Instance.RecordBidSuccess(result, AdsType.Interstitial);
 | ||
|             if (result == PlatformType.AppLovin)
 | ||
|             {
 | ||
|                 MaxAdsManager.Instance.DisplayInterstitial(_adPos, _IvType, _closeCallback);
 | ||
|             }
 | ||
|             else if (result == PlatformType.Admob)
 | ||
|             {
 | ||
|                 AdmobAdsManager.Instance.DisplayInterstitial(_adPos, _IvType, _closeCallback);
 | ||
|             }
 | ||
|             else if (result == PlatformType.Bigo)
 | ||
|             {
 | ||
|                 BigoAdsManager.Instance.DisplayInterstitial(_adPos, _IvType, _closeCallback);
 | ||
|             }
 | ||
|             else if (result == PlatformType.Topon)
 | ||
|             {
 | ||
|                 TpnAdsManager.Instance.DisplayInterstitial(_adPos, _IvType, _closeCallback);
 | ||
|             }
 | ||
|             else if (result == PlatformType.Kwai)
 | ||
|             {
 | ||
| 
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 _closeCallback?.Invoke(0);
 | ||
|             }
 | ||
| 
 | ||
|             AdPlayCountManager.IncrementAdPlayCount(AdsType.Interstitial);
 | ||
|             // 刷新其他类型广告
 | ||
|             CheckAndRefreshExpiredBids(AdsType.Interstitial);
 | ||
|         }
 | ||
|         #endregion
 | ||
| 
 | ||
|         #region  横幅广告
 | ||
|         public bool IsBannerAdReady()
 | ||
|         {
 | ||
|             return AdmobAdsManager.Instance.IsBannerAvailable();
 | ||
|         }
 | ||
| 
 | ||
|         public void ShowBanner()
 | ||
|         {
 | ||
|             AdsActionEvents.TrackAdPosition(AdsType.Interstitial, "");
 | ||
|             if (IsBannerAdReady())
 | ||
|             {
 | ||
|                 AdmobAdsManager.Instance.DisplayBanner();
 | ||
|                 AdPlayCountManager.IncrementAdPlayCount(AdsType.Banner);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         public void HideBanner()
 | ||
|         {
 | ||
|             AdmobAdsManager.Instance.HideBanner();
 | ||
|         }
 | ||
|         #endregion
 | ||
| 
 | ||
| 
 | ||
|         #region 原生广告
 | ||
|         public bool IsNativeAdReady()
 | ||
|         {
 | ||
|             return AdmobAdsManager.Instance.IsNativeAvailable();
 | ||
|         }
 | ||
| 
 | ||
|         public void ShowNativeAd(NativeAdPosition position)
 | ||
|         {
 | ||
|             if (IsNativeAdReady())
 | ||
|             {
 | ||
|                 AdmobAdsManager.Instance.DisplayNative(position);
 | ||
|                 AdPlayCountManager.IncrementAdPlayCount(AdsType.Native);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         public void RemoveNativeAd()
 | ||
|         {
 | ||
|             AdmobAdsManager.Instance.RemoveNative();
 | ||
|         }
 | ||
|         #endregion
 | ||
| 
 | ||
|         #region 开屏广告
 | ||
|         public bool IsSplashAvailable()
 | ||
|         {
 | ||
|             return _adNetworks.Any(network => network.IsSplashAvailable());
 | ||
|         }
 | ||
| 
 | ||
|         public void ShowSplashAd()
 | ||
|         {
 | ||
|             AdmobAdsManager.Instance.DisplaySplash();
 | ||
|         }
 | ||
| 
 | ||
|         public void LoadSplashAd()
 | ||
|         {
 | ||
|             AdmobAdsManager.Instance.LoadSplash();
 | ||
|         }
 | ||
|         #endregion
 | ||
| 
 | ||
|         private PlatformType GetBestPlatformType(bool isInterstitial)
 | ||
|         {
 | ||
|             if (isInterstitial)
 | ||
|             {
 | ||
|                 AdPriceInfo priceInfo = new AdPriceInfo(
 | ||
|                     maxPrice: MaxAdsManager.Instance.GetInterstitialRevenue(),
 | ||
|                     admobPrice: AdmobAdsManager.Instance.GetInterstitialRevenue(),
 | ||
|                     bigoPrice: BigoAdsManager.Instance.GetInterstitialRevenue(),
 | ||
|                     // todo : 这里的kwaiPrice暂时设置为0,后续需要根据实际情况设置
 | ||
|                     kwaiPrice: 0,
 | ||
|                     toponAdUnitId: TpnAdsManager.Instance._topon_interstitial_units
 | ||
|                 );
 | ||
|                 return AdsBidResult.GetPlatformType(priceInfo);
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 AdPriceInfo priceInfo = new AdPriceInfo(
 | ||
|                     maxPrice: MaxAdsManager.Instance.GetRewardedRevenue(),
 | ||
|                     admobPrice: AdmobAdsManager.Instance.GetRewardedRevenue(),
 | ||
|                     bigoPrice: BigoAdsManager.Instance.GetRewardedRevenue(),
 | ||
|                     // todo : 这里的kwaiPrice暂时设置为0,后续需要根据实际情况设置
 | ||
|                     kwaiPrice: 0,
 | ||
|                     toponAdUnitId: TpnAdsManager.Instance._topon_rewarded_units
 | ||
|                 );
 | ||
|                 return AdsBidResult.GetPlatformType(priceInfo);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 广告看完回调
 | ||
|         /// </summary>
 | ||
|         public void OnRewardAdCallback(double price)
 | ||
|         {
 | ||
|             AdRewardCallback?.Invoke(price);
 | ||
|             AdRewardCallback = null;
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 根据IvRules判断是否可以展示插屏
 | ||
|         /// </summary>
 | ||
|         /// <returns></returns>
 | ||
|         public bool IvRulesShow(IvType ivadType)
 | ||
|         {
 | ||
|             //1.获取远程配置
 | ||
|             string json = FireBaseRemoteConfigManager.Instance.GetRemoteConfigString("IV_RULES");
 | ||
|             if (string.IsNullOrEmpty(json))
 | ||
|             {
 | ||
|                 return true;
 | ||
|             }
 | ||
|             //2.解析配置
 | ||
|             var dates = JsonConvert.DeserializeObject<IvRulesData[]>(json);
 | ||
|             if (dates == null && dates.Length == 0)
 | ||
|             {
 | ||
|                 return true;
 | ||
|             }
 | ||
|             //3.获取IVADType对应的配置
 | ||
|             IvRulesData ivRulesData = null;
 | ||
|             foreach (var data in dates)
 | ||
|             {
 | ||
|                 if (data.type == (int)ivadType)
 | ||
|                 {
 | ||
|                     ivRulesData = data;
 | ||
|                 }
 | ||
|             }
 | ||
|             if (ivRulesData == null)
 | ||
|             {
 | ||
|                 return true;
 | ||
|             }
 | ||
|             //4.判断skip(次安装跳过几次触发不展示广告)
 | ||
|             int skipLevel = ivRulesData.skipLevel;
 | ||
|             int currentSkipLevel = PlayerPrefsUtils.GetPlayerPrefsInt(IvRulesKey.KEY_SKIPLEVEL, 0);
 | ||
|             if (currentSkipLevel < skipLevel)
 | ||
|             {
 | ||
|                 LoggerUtils.Debug($"[SDK] skipLevel limit");
 | ||
|                 PlayerPrefsUtils.SavePlayerPrefsInt(IvRulesKey.KEY_SKIPLEVEL, currentSkipLevel + 1);
 | ||
|                 return false;
 | ||
|             }
 | ||
|             //5.判断overLevel(没跳过几次触发)
 | ||
|             int overLevel = ivRulesData.overLevel;
 | ||
|             int currentOverLevel = IvRulesConst.CurrentOverLevel;
 | ||
|             if (currentOverLevel < overLevel)
 | ||
|             {
 | ||
|                 LoggerUtils.Debug($"[SDK] overLevel limit");
 | ||
|                 IvRulesConst.CurrentOverLevel++;
 | ||
|                 return false;
 | ||
|             }
 | ||
| 
 | ||
|             //6.判断interval(广告时间间隔)
 | ||
|             int interval = ivRulesData.interval;
 | ||
|             long currentInterval = IvRulesConst.CurrentInterval;
 | ||
|             long localTimestamp = TimeUtils.GetLocalTimestamp();
 | ||
| 
 | ||
|             if (localTimestamp < currentInterval + (interval * 1000L))
 | ||
|             {
 | ||
|                 LoggerUtils.Debug($"[SDK] interval limit");
 | ||
|                 return false;
 | ||
|             }
 | ||
| 
 | ||
|             return true;
 | ||
|         }
 | ||
|         
 | ||
|         #region 检查并刷新过期竞价
 | ||
|         private void CheckAndRefreshExpiredBids(AdsType _adsType)
 | ||
|         {
 | ||
|             Dictionary<PlatformType, List<AdsType>> expiredBids = BidPlatformManager.Instance.GetExpiredBids();
 | ||
| 
 | ||
|             foreach (var kvp in expiredBids)
 | ||
|             {
 | ||
|                 PlatformType platformName = kvp.Key;
 | ||
|                 foreach (AdsType adType in kvp.Value)
 | ||
|                 {
 | ||
|                     if (_adsType == adType)
 | ||
|                     {
 | ||
|                         LoggerUtils.Debug($"{platformName} 平台 {adType} 广告竞价已过期,重新加载广告");
 | ||
|                         RefreshPlatformAds(platformName, adType);
 | ||
|                     }
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
|         private void RefreshPlatformAds(PlatformType platformName, AdsType adType)
 | ||
|         {
 | ||
|             switch (platformName)
 | ||
|             {
 | ||
|                 case PlatformType.Admob:
 | ||
|                     RefreshAdmobAds(adType);
 | ||
|                     break;
 | ||
|                 case PlatformType.Topon:
 | ||
|                     RefreshTopOnAds(adType);
 | ||
|                     break;
 | ||
|                 case PlatformType.AppLovin:
 | ||
|                     RefreshMaxAds(adType);
 | ||
|                     break;
 | ||
|                 case PlatformType.Bigo:
 | ||
|                     RefreshBigoAds(adType);
 | ||
|                     break;
 | ||
|                 case PlatformType.Kwai:
 | ||
|                     RefreshKwaiAds(adType);
 | ||
|                     break;
 | ||
|                 default:
 | ||
|                     break;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         private void RefreshAdmobAds(AdsType adType)
 | ||
|         {
 | ||
|             switch (adType)
 | ||
|             {
 | ||
|                 case AdsType.Rewarded:
 | ||
|                     AdmobAdsManager.Instance.LoadRewarded();
 | ||
|                     break;
 | ||
|                 case AdsType.Interstitial:
 | ||
|                     AdmobAdsManager.Instance.LoadInterstitial();
 | ||
|                     break;
 | ||
|                 case AdsType.Banner:
 | ||
|                     AdmobAdsManager.Instance.LoadBanner();
 | ||
|                     break;
 | ||
|                 case AdsType.Native:
 | ||
|                     AdmobAdsManager.Instance.LoadNative();
 | ||
|                     break;
 | ||
|                 case AdsType.Splash:
 | ||
|                     AdmobAdsManager.Instance.LoadSplash();
 | ||
|                     break;
 | ||
|                 default:
 | ||
|                     break;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         // 刷新TopOn广告
 | ||
|         private void RefreshTopOnAds(AdsType adType)
 | ||
|         {
 | ||
|             switch (adType)
 | ||
|             {
 | ||
|                 case AdsType.Rewarded:
 | ||
|                     TpnAdsManager.Instance.LoadRewarded();
 | ||
|                     break;
 | ||
|                 case AdsType.Interstitial:
 | ||
|                     TpnAdsManager.Instance.LoadInterstitial();
 | ||
|                     break;
 | ||
|                 default:
 | ||
|                     break;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         // 刷新Max广告
 | ||
|         private void RefreshMaxAds(AdsType adType)
 | ||
|         {
 | ||
|             switch (adType)
 | ||
|             {
 | ||
|                 case AdsType.Rewarded:
 | ||
|                     MaxAdsManager.Instance.LoadRewarded();
 | ||
|                     break;
 | ||
|                 case AdsType.Interstitial:
 | ||
|                     MaxAdsManager.Instance.LoadInterstitial();
 | ||
|                     break;
 | ||
|                 default:
 | ||
|                     break;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         // 刷新Bigo广告
 | ||
|         private void RefreshBigoAds(AdsType adType)
 | ||
|         {
 | ||
|             switch (adType)
 | ||
|             {
 | ||
|                 case AdsType.Rewarded:
 | ||
|                     BigoAdsManager.Instance.LoadRewarded();
 | ||
|                     break;
 | ||
|                 case AdsType.Interstitial:
 | ||
|                     BigoAdsManager.Instance.LoadInterstitial();
 | ||
|                     break;
 | ||
|                 default:
 | ||
|                     break;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         private void RefreshKwaiAds(AdsType adType)
 | ||
|         {
 | ||
|             switch (adType)
 | ||
|             {
 | ||
|                 case AdsType.Rewarded:
 | ||
|                     // todo: 刷新激励广告
 | ||
|                     KwaiAdsManager.Instance.LoadRewardAd();
 | ||
|                     break;
 | ||
|                 case AdsType.Interstitial:
 | ||
|                     // todo: 刷新插屏广告
 | ||
|                     KwaiAdsManager.Instance.LoadInterstitialAd();
 | ||
|                     break;
 | ||
|                 default:
 | ||
|                     break;
 | ||
|             }
 | ||
|         }
 | ||
| #endregion
 | ||
|     }
 | ||
| } |