diff --git a/Assets/Script/SDKManager/AdsSDKManager/AdmobAdsManager/AdmobNativeAdManager.cs b/Assets/Script/SDKManager/AdsSDKManager/AdmobAdsManager/AdmobNativeAdManager.cs index f0d44dc..8815bd7 100644 --- a/Assets/Script/SDKManager/AdsSDKManager/AdmobAdsManager/AdmobNativeAdManager.cs +++ b/Assets/Script/SDKManager/AdsSDKManager/AdmobAdsManager/AdmobNativeAdManager.cs @@ -14,6 +14,7 @@ namespace WZ private Dictionary _retryCounters = new Dictionary(); private Dictionary _adStartLoadTimes = new Dictionary(); + private Dictionary showingNativeAds = new Dictionary(); public void InitializeAdUnits(List adUnitIds) { @@ -44,8 +45,6 @@ namespace WZ { return; } - - _nativeAds.Remove(adUnitId); NativeOverlayAd.Load(adUnitId, new AdRequest(), new NativeAdOptions(), (NativeOverlayAd ad, LoadAdError error) => { @@ -80,10 +79,10 @@ namespace WZ var retryDelay = Math.Pow(2, Math.Min(6, _retryCounters[adUnitId])); TimerUtils.Instance.DelayExecute((float)retryDelay, () => { LoadAd(adUnitId); }); - LoggerUtils.Debug("[Admob] Native ad failed to load an ad with error : " + error + " \n retryDelay :" + retryDelay); + LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} ad failed to load an ad with error : " + error + " \n retryDelay :" + retryDelay); return; } - + if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Native, adUnitId)) { return; @@ -159,7 +158,7 @@ namespace WZ { return; } - + LoggerUtils.Debug($"[Admob] Native ad ShowAd start {adUnitId} , {position}"); if (_nativeAds.TryGetValue(adUnitId, out var ad)) @@ -183,6 +182,7 @@ namespace WZ // and anchored to the bottom of the screne. ad.RenderTemplate(style, new AdSize(position.Width, position.Height), position.X, position.Y); + showingNativeAds[adUnitId] = ad; ad.Show(); } } @@ -240,7 +240,7 @@ namespace WZ return 0; } - + // 获取广告收益信息 public double GetAdRevenue(string adUnit) { @@ -252,7 +252,7 @@ namespace WZ return -1; } - + // 清理资源 public void Destroy() @@ -268,25 +268,36 @@ namespace WZ public IEnumerator RemoveNative(string adUnitId) { - yield return new WaitForSeconds(0.2f); + // 不需要等待了 + // yield return new WaitForSeconds(0.2f); if (adUnitId == null || string.IsNullOrEmpty(adUnitId)) { - foreach (var nativeOverlayAd in _nativeAds) + foreach (var showingNativeAd in showingNativeAds) { - nativeOverlayAd.Value.Hide(); - LoadAd(nativeOverlayAd.Key); + LoggerUtils.Debug($"[Admob] Native ad removing NativeAd {adUnitId}"); + showingNativeAd.Value.Hide(); + + LoadAd(showingNativeAd.Key); } + showingNativeAds.Clear(); + yield break; } - if (_nativeAds.TryGetValue(adUnitId, out var tempAd)) + if (showingNativeAds.TryGetValue(adUnitId, out var tempAd)) { + LoggerUtils.Debug($"[Admob] Native ad removing NativeAd {adUnitId}"); tempAd.Hide(); + showingNativeAds.Remove(adUnitId); LoadAd(adUnitId); } + else + { + LoggerUtils.Debug($"[Admob] Native ad removing NativeAd {adUnitId} , failed to remove NativeAd"); + } } - + public void ClearAds(string[] adUnitIds) { // 将数组转换为HashSet以提高查找性能 @@ -310,7 +321,6 @@ namespace WZ _adStartLoadTimes.Remove(key); _adRevenueCache.Remove(key); } - } } } \ No newline at end of file