using System; using System.Collections.Generic; using System.Linq; using GoogleMobileAds.Api; namespace WZ { public class AdmobInterstitialAdManager { private Dictionary _interstitialAds = new Dictionary(); private Dictionary _adRevenueCache = new Dictionary(); public void InitializeAdUnits(List adUnitIds, Action onAdLoaded = null, Action onAdLoadFailed = null, Action onAdShowed = null, Action onAdDismissed = null, Action onAdError = null, Action onAdPaid = null, Action onAdClicked = null) { foreach (var adUnitId in adUnitIds) { CreateRewardedAd(adUnitId, onAdLoaded, onAdLoadFailed, onAdShowed, onAdDismissed, onAdError,onAdPaid, onAdClicked); } } private void CreateRewardedAd(string adUnitId, Action onAdLoaded, Action onAdLoadFailed, Action onAdShowed, Action onAdDismissed, Action onAdError, Action onAdPaid, Action onAdClicked) { if (string.IsNullOrEmpty(adUnitId)) return; if (_interstitialAds.ContainsKey(adUnitId)) { LoggerUtils.Debug($"[Admob] Interstitial Ad unit {adUnitId} already exists"); return; } _adRevenueCache[adUnitId] = 0; InterstitialAd.Load(adUnitId, new AdRequest(), (InterstitialAd ad, LoadAdError error) => { if (error != null || ad == null) { LoggerUtils.Debug("[Admob] interstitial ad failed to load an ad with error : " + error); onAdLoadFailed?.Invoke(adUnitId, error.GetCode(), error.GetMessage()); return; } if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Interstitial, adUnitId)) { return; } LoggerUtils.Debug("Admob Interstitial ad loaded with response : " + ad.GetResponseInfo().ToString()); LoggerUtils.Debug("Admob Interstitial ad loaded with Getsrevenue:"+AdmobUtils.GetInterstitialAdEcpm(ad)); _interstitialAds[adUnitId] = ad; _adRevenueCache[adUnitId] = AdmobUtils.GetInterstitialAdEcpm(ad); AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Interstitial); onAdLoaded?.Invoke(ad?.GetResponseInfo()?.GetLoadedAdapterResponseInfo()?.AdSourceName ?? "", adUnitId); AdsActionEvents.TrackAdmobValue(PlatformType.Admob, ad?.GetResponseInfo()?.GetLoadedAdapterResponseInfo()?.AdSourceName ?? "", adUnitId, AdsType.Interstitial, AdmobUtils.GetInterstitialAdEcpm(ad)); ad.OnAdPaid += (AdValue adValue) => { LoggerUtils.Debug(String.Format("[Admob] Interstitial ad paid {0} {1}.", adValue.Value, adValue.CurrencyCode)); AdmobAdsManager.Instance.TrackAdImpression(ad?.GetResponseInfo()?.GetLoadedAdapterResponseInfo().AdSourceName ??"", ad?.GetResponseInfo()?.GetLoadedAdapterResponseInfo().AdSourceInstanceId ??"", adValue, AdsType.Interstitial, adUnitId); onAdPaid?.Invoke(adValue); }; ad.OnAdImpressionRecorded += () => { LoggerUtils.Debug("[Admob] Interstitial ad recorded an impression."); }; ad.OnAdClicked += () => { LoggerUtils.Debug("[Admob] Interstitial ad was clicked."); onAdClicked?.Invoke(ad?.GetResponseInfo()?.GetLoadedAdapterResponseInfo()?.AdSourceName ?? "", adUnitId, AdmobUtils.GetInterstitialAdEcpm(ad)); }; ad.OnAdFullScreenContentOpened += () => { LoggerUtils.Debug("[Admob] Interstitial ad full screen content opened."); onAdShowed?.Invoke(adUnitId); }; ad.OnAdFullScreenContentClosed += () => { LoggerUtils.Debug("[Admob] Interstitial ad full screen content closed."); onAdDismissed?.Invoke(ad?.GetResponseInfo()?.GetLoadedAdapterResponseInfo()?.AdSourceName ?? "", adUnitId, AdmobUtils.GetInterstitialAdEcpm(ad)); }; ad.OnAdFullScreenContentFailed += (AdError error) => { LoggerUtils.Debug("[Admob] Interstitial ad failed to open full screen content with error : " + error); onAdError?.Invoke(adUnitId, error.GetCode(), error.GetMessage()); }; }); } public void LoadAd(string adUnitId) { } // 显示价格最高的广告 public void ShowHighestPayingAd() { var highestPayingAdUnit = GetHighestPayingAdUnit(); if (!string.IsNullOrEmpty(highestPayingAdUnit)) { ShowAd(highestPayingAdUnit); } } // 显示特定广告位的广告 private void ShowAd(string adUnitId) { if (_interstitialAds.TryGetValue(adUnitId, out var ad)) { ad.Show(); } } // 检查特定广告位是否可用 private bool IsAdAvailable(string adUnitId) { if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Interstitial, adUnitId)) { return false; } return _interstitialAds.TryGetValue(adUnitId, out var ad) && ad.CanShowAd(); } // 获取所有可用的广告位 public List GetAvailableAdUnits() { var available = new List(); foreach (var kvp in _interstitialAds) { if (kvp.Value.CanShowAd()) { available.Add(kvp.Key); } } return available; } // 获取价格最高的广告位ID public string GetHighestPayingAdUnit() { return _adRevenueCache .Where(kvp => IsAdAvailable(kvp.Key)) .OrderByDescending(kvp => kvp.Value) .Select(kvp => kvp.Key) .FirstOrDefault(); } // 获取价格最高的广告收益信息 public double GetHighestPayingAdRevenue() { var highestPayingAdUnit = GetHighestPayingAdUnit(); if (!string.IsNullOrEmpty(highestPayingAdUnit) && _adRevenueCache.TryGetValue(highestPayingAdUnit, out var revenue)) { return revenue; } return -1; } // 清理资源 public void Destroy() { foreach (var ad in _interstitialAds.Values) { ad.Destroy(); } _interstitialAds.Clear(); _adRevenueCache.Clear(); } public void ClearAds(string[] adUnitIds) { // 将数组转换为HashSet以提高查找性能 HashSet validKeys = new HashSet(adUnitIds); // 收集需要移除的key List keysToRemove = new List(); foreach (var key in _interstitialAds.Keys) { if (!validKeys.Contains(key)) { keysToRemove.Add(key); } } // 移除不在数组中的key foreach (string key in keysToRemove) { _interstitialAds.Remove(key); _adRevenueCache.Remove(key); } } } }