native 显示和隐藏

This commit is contained in:
luojian 2025-09-15 15:55:38 +08:00
parent 55ceded004
commit d9e113a8b8
1 changed files with 24 additions and 14 deletions

View File

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