using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using GoogleMobileAds.Api; using GoogleMobileAds.Common; using UnityEngine; namespace WZ { public class AdmobAdsManager : D_MonoSingleton, IAdService { public string ClientName => "Admob"; public PlatformType Platfrom => PlatformType.Admob; private AdmobBannerAdManager _admobBannerAdManager; private AdmobCollapsibleBannerAdManager _admobCollapsibleBannerAdManager; private AdmobInterstitialAdManager _admobInterstitialAdManager; private AdmobRewardedAdManager _admobRewardedAdManager; private AdmobNativeAdManager _admobNativeAdManager; private AdmobSplashAdManager _admobSplashAdManager; private List _interstitialAdUnits = new List(); private List _rewardedAdUnits = new List(); private List _nativeAdUnits = new List(); private List _splashAdUnits = new List(); private List _bannerAdUnits = new List(); private List _collapsibleBannerAdUnits = new List(); public string _rvPos; public string _ivPos; public string _naPos; public NativeType _naType; private Action _rvCloseCallback = null; private Action _ivCloseCallback = null; private Action _rvShowFailedCallback = null; private float _rvStartLoadTime = 0; private float _ivStartLoadTime = 0; private float _spStartLoadTime = 0; public bool _splashLoaded = false; public bool _initialized { get; private set; } = false; private bool _receivedReward = false; public void ParseAdmobConfig() { var d = "{\"admob_app_id\":\"ca-app-pub-5381644556376235~3756187187\",\"admob_interstitial_units\":[\"ca-app-pub-4412893362169872/3366670333\"],\"admob_splash_units\":[\"ca-app-pub-4412893362169872/7716978882\"],\"admob_rewarded_units\":[\"ca-app-pub-4412893362169872/8427425327\"],\"admob_banner_units\":[\"ca-app-pub-4412893362169872/7908550574\"],\"admob_native_units\":[\"ca-app-pub-4412893362169872/5090815546\"]}"; var ads = RushSDKManager.Instance.GetRemoteConfigStr("adUnits_adm",d); var config = AdConfigParser.Parse(ads); _bannerAdUnits = config.admob_banner_units; _collapsibleBannerAdUnits = config.admob_collbanner_units; _interstitialAdUnits = config.admob_interstitial_units; _nativeAdUnits = config.admob_native_units; _rewardedAdUnits = config.admob_rewarded_units; _splashAdUnits = config.admob_splash_units; } public void Initialize() { if (_initialized) return; MobileAds.RaiseAdEventsOnUnityMainThread = true; _initialized = true; MobileAds.Initialize(initStatus => { LoadBanner(); if (_interstitialAdUnits.Count > 0) LoadInterstitial(); if (_rewardedAdUnits.Count > 0) LoadRewarded(); if (_splashAdUnits.Count > 0) AdsSplashManager.Instance.InitSplash(); if (_nativeAdUnits.Count > 0) LoadNative(); LoggerUtils.Debug("[Admob] init success"); }); } public void RefreshAdsData() { } #region 激励广告功能 public void LoadRewarded() { _admobRewardedAdManager?.Destroy(); _admobRewardedAdManager = new AdmobRewardedAdManager(); _admobRewardedAdManager.InitializeAdUnits( _rewardedAdUnits, OnRewardedAdLoaded, OnRewardedAdLoadFailed, OnRewardedAdShowed, OnRewardedAdDismissed, OnRewardedAdError, OnRewardEarnReward, OnRewardedAdClicked ); _rvStartLoadTime = Time.realtimeSinceStartup; } public void DisplayRewarded(string adPos, Action rewardCallback = null, Action showFailedCallback = null) { _rvPos = adPos; _rvCloseCallback = rewardCallback; _rvShowFailedCallback = showFailedCallback; _admobRewardedAdManager.ShowHighestPayingAd(); } public double GetRewardedRevenue() { return _admobRewardedAdManager.GetHighestPayingAdRevenue(); } public bool IsRewardedAvailable() { if (!_initialized || _rewardedAdUnits.Count == 0) return false; return _admobRewardedAdManager.GetAvailableAdUnits().Count > 0; } private void OnRewardedAdLoaded(string adSource,string adUnitId) { AdsActionEvents.TrackAdLoaded(Platfrom,adSource,adUnitId,AdsType.Rewarded,Time.realtimeSinceStartup - _rvStartLoadTime); } private void OnRewardedAdLoadFailed(string adUnitId, int errorCode, string errorMsg) { AdsActionEvents.TrackAdFailToLoad(Platfrom,"",adUnitId,AdsType.Rewarded,Time.realtimeSinceStartup - _rvStartLoadTime,errorMsg); } private void OnRewardedAdShowed(string adUnitId) { } private void OnRewardedAdDismissed(string adSource,string adUnitId,double revenue) { AdsActionEvents.TrackAdClosed(Platfrom, adSource, adUnitId, AdsType.Rewarded, _rvPos, revenue, AdPlayCountManager.GetAdsActionCount(AdsType.Rewarded,_rvPos+AdPlayCountManager.POS_SUFFIX)); _rvCloseCallback?.Invoke(_receivedReward,revenue); _rvCloseCallback = null; _receivedReward = false; LoadRewarded(); } private void OnRewardedAdError(string adUnitId, int errorCode, string errorMsg) { _rvShowFailedCallback?.Invoke(); _rvShowFailedCallback = null; LoadRewarded(); } private void OnRewardedAdClicked(string adSource,string adUnitId,double revenue) { AdsActionEvents.TrackAdClicked(Platfrom, adSource, adUnitId, AdsType.Rewarded, _rvPos, revenue, AdPlayCountManager.GetAdsActionCount(AdsType.Rewarded,_rvPos+AdPlayCountManager.POS_SUFFIX), NativeType.None); } private void OnRewardEarnReward(bool reward) { _receivedReward = reward; } #endregion #region 插页广告功能 public void LoadInterstitial() { _admobInterstitialAdManager?.Destroy(); _admobInterstitialAdManager = new AdmobInterstitialAdManager(); _admobInterstitialAdManager.InitializeAdUnits( _interstitialAdUnits, OnInterstitialAdLoaded, OnInterstitialAdLoadFailed, OnInterstitialAdShowed, OnInterstitialAdDismissed, OnInterstitialAdError, OnInterstitialAdPaid, OnInterstitialAdClicked ); _ivStartLoadTime = Time.realtimeSinceStartup; } public double GetInterstitialRevenue() { return _admobInterstitialAdManager.GetHighestPayingAdRevenue(); } public void DisplayInterstitial(string ivPos, IvType _IvType = IvType.IV1, Action closeCallback = null) { _ivPos = ivPos; _ivCloseCallback = closeCallback; _admobInterstitialAdManager.ShowHighestPayingAd(); } public bool IsInterstitialAvailable() { if (!_initialized || _interstitialAdUnits.Count == 0) return false; return _admobInterstitialAdManager.GetAvailableAdUnits().Count > 0; } private void OnInterstitialAdLoaded(string adSource,string adUnitId) { AdsActionEvents.TrackAdLoaded(Platfrom,adSource,adUnitId,AdsType.Interstitial,Time.realtimeSinceStartup - _ivStartLoadTime); } private void OnInterstitialAdLoadFailed(string adUnitId, int errorCode, string errorMsg) { AdsActionEvents.TrackAdFailToLoad(Platfrom,"","",AdsType.Interstitial,Time.realtimeSinceStartup - _ivStartLoadTime,errorMsg); } private void OnInterstitialAdClicked(string adSource,string adUnitId,double revenue) { AdsActionEvents.TrackAdClicked(Platfrom, adSource, adUnitId, AdsType.Interstitial, _ivPos, revenue, AdPlayCountManager.GetAdsActionCount(AdsType.Interstitial,_ivPos+AdPlayCountManager.POS_SUFFIX), NativeType.None); } private void OnInterstitialAdPaid(AdValue adValue) { } private void OnInterstitialAdShowed(string adUnitId) { } private void OnInterstitialAdDismissed(string adSource,string adUnitId,double revenue) { AdsActionEvents.TrackAdClosed(Platfrom, adSource, adUnitId, AdsType.Interstitial, _ivPos, revenue, AdPlayCountManager.GetAdsActionCount(AdsType.Interstitial,_ivPos+AdPlayCountManager.POS_SUFFIX) ); _ivCloseCallback?.Invoke(revenue); _ivCloseCallback = null; LoadInterstitial(); } private void OnInterstitialAdError(string adUnitId, int errorCode, string errorMsg) { _ivCloseCallback?.Invoke(0); _ivCloseCallback = null; LoadInterstitial(); } #endregion #region 开屏广告功能 public void LoadSplash() { _admobSplashAdManager?.Destroy(); _admobSplashAdManager = new AdmobSplashAdManager(); _admobSplashAdManager.InitializeAdUnits( _splashAdUnits, OnSplashAdLoaded, OnSplashAdLoadFailed, OnSplashAdShowed, OnSplashAdDismissed, OnSplashAdError, OnSplashAdPaid, OnSplashAdClicked ); _spStartLoadTime = Time.realtimeSinceStartup; } public bool IsSplashAvailable() { if (!_initialized) return false; return _admobSplashAdManager.GetAvailableAdUnits().Count > 0; } public void DisplaySplash() { _admobSplashAdManager.ShowHighestPayingAd(); } public double GetSplashRevenue() { return _admobSplashAdManager.GetHighestPayingAdRevenue(); } private void OnSplashAdLoaded(string adSource,string adUnitId) { _splashLoaded = true; _retryAttempt = 0; AdsActionEvents.TrackAdLoaded(Platfrom,adSource,adUnitId,AdsType.Splash,Time.realtimeSinceStartup - _spStartLoadTime); } private int _retryAttempt = 0; private void OnSplashAdLoadFailed(string adUnitId, int errorCode, string errorMsg) { _splashLoaded = true; _retryAttempt++; double retryDelay = Math.Pow(2, Math.Min(6, _retryAttempt)); Invoke(nameof(LoadSplash), (float)retryDelay); AdsActionEvents.TrackAdFailToLoad(Platfrom,"","",AdsType.Splash,Time.realtimeSinceStartup - _spStartLoadTime,errorMsg); } private void OnSplashAdShowed(string adUnitId) { AdsSDKManager.Instance.otherAdsOnShow = true; } private void OnSplashAdDismissed(string adSource, string adUnitId, double revenue) { AdsActionEvents.TrackAdClosed(Platfrom, adSource, adUnitId, AdsType.Splash, AdsSplashManager.Instance._coldLaunch ? "Cold" : "Hot", revenue, AdPlayCountManager.GetAdsActionCount(AdsType.Splash,"Splash"+AdPlayCountManager.POS_SUFFIX) ); AdsSDKManager.Instance.otherAdsOnShow = false; AdsSplashManager.Instance.OnSplashAdCloseCallback?.Invoke(); LoadSplash(); } private void OnSplashAdError(string adUnitId, int errorCode, string errorMsg) { AdsSDKManager.Instance.otherAdsOnShow = false; AdsSplashManager.Instance.OnSplashAdCloseCallback?.Invoke(); LoadSplash(); } private void OnSplashAdClicked(string adSource,string adUnitId,double revenue) { AdsActionEvents.TrackAdClicked(Platfrom, adSource, adUnitId, AdsType.Splash, AdsSplashManager.Instance._coldLaunch ? "Cold" : "Hot", revenue, AdPlayCountManager.GetAdsActionCount(AdsType.Splash,"Splash"+AdPlayCountManager.POS_SUFFIX), NativeType.None); } private void OnSplashAdPaid(AdValue adValue) { } #endregion #region 原生广告功能 public void LoadNative() { _admobNativeAdManager?.Destroy(); _admobNativeAdManager = new AdmobNativeAdManager(); _admobNativeAdManager.InitializeAdUnits( _nativeAdUnits ); } public bool IsNativeAvailable(string adUnitId) { return _admobNativeAdManager?.IsAdAvailable(adUnitId) ?? false; } public void DisplayNative(string _adPos, string adUnitId, NativeAdPosition position, NativeType nativeType) { _naPos = _adPos; _naType = nativeType; _admobNativeAdManager.ShowAd(position, adUnitId); } public void RemoveNative(string adUnitId) { StartCoroutine(_admobNativeAdManager.RemoveNative(adUnitId)); } public double GetNativeRevenue(string adUnitId) { return _admobNativeAdManager.GetAdRevenue(adUnitId); } #endregion #region 横幅广告功能 public void LoadBanner() { LoadStandardBanner(); LoadCollapsingBanner(); } private void LoadStandardBanner() { if (_bannerAdUnits.Count > 0) { _admobBannerAdManager?.Destroy(); _admobBannerAdManager = new AdmobBannerAdManager(); _admobBannerAdManager.InitializeAdUnits(BannerAlignType.CenterBottom,_bannerAdUnits); } } public void LoadCollapsingBanner() { if (_collapsibleBannerAdUnits.Count > 0) { _admobCollapsibleBannerAdManager?.Destroy(); _admobCollapsibleBannerAdManager = new AdmobCollapsibleBannerAdManager(); _admobCollapsibleBannerAdManager.InitializeAdUnits(BannerAlignType.CenterBottom,_collapsibleBannerAdUnits); } } public bool IsBannerAvailable(BannerType bannerType) { if (!_initialized) return false; if (bannerType == BannerType.Standard) { return _admobBannerAdManager?.GetAvailableAdUnits().Count > 0; } else { return _admobCollapsibleBannerAdManager?.GetAvailableAdUnits().Count > 0; } } public void HideBanner(BannerType bannerType) { if (bannerType == BannerType.Standard) { _admobBannerAdManager?.HideBanner(); } else { _admobCollapsibleBannerAdManager?.HideBanner(); } } public double GetBannerRevenue(BannerType bannerType) { return _admobBannerAdManager.GetHighestPayingAdRevenue(); } public void DisplayBanner(BannerType bannerType,BannerAlignType bannerAlignType) { if (bannerType == BannerType.Standard) { _admobBannerAdManager?.ShowHighestPayingAd(bannerAlignType); } else { _admobCollapsibleBannerAdManager?.ShowHighestPayingAd(bannerAlignType); } } #endregion #region 收益上报 public void TrackAdImpression(string adsourceName,string adSourceInstanceName, AdValue adValue, AdsType type, string placement,NativeType nativeType) { AdjustTrackEvent.Instance.TrackAdEvent(adValue.Value / 1000000f, adsourceName, placement, adSourceInstanceName); var position = type switch { AdsType.Rewarded => _rvPos, AdsType.Interstitial => _ivPos, AdsType.Native => _naPos, _ => "" }; FireBaseAnalyticsManager.Instance.OnAdRevenueEvent(PlatformType.Admob.ToString(), adsourceName, placement, type, adValue.Value / 1000000f, position, AdPlayCountManager.GetAdsActionCount(type, AdPlayCountManager.PLAY_COUNT_SUFFIX)); var positionNumKey = position; if(string.IsNullOrEmpty(positionNumKey)) positionNumKey = "Banner"; ShuShuEvent.Instance.OnAdRevenueEvent(PlatformType.Admob.ToString(), adsourceName, placement, type, adValue.Value / 1000000f, position, AdPlayCountManager.GetAdsActionCount(type, AdPlayCountManager.PLAY_COUNT_SUFFIX), AdPlayCountManager.GetAdsActionCount(type,positionNumKey+AdPlayCountManager.POS_SUFFIX), nativeType); } #endregion #region 广告行为事件上报 public void TrackAdAction(AdapterResponseInfo loadedAdapterResponseInfo, double revenue, AdsType type, string placement) { } #endregion } }