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, float> _adStartLoadTimes = new Dictionary<string, float>();
private Dictionary<string, NativeOverlayAd> showingNativeAds = new Dictionary<string, NativeOverlayAd>();
public void InitializeAdUnits(List<string> adUnitIds)
{
@ -45,8 +46,6 @@ namespace WZ
return;
}
_nativeAds.Remove(adUnitId);
NativeOverlayAd.Load(adUnitId, new AdRequest(), new NativeAdOptions(), (NativeOverlayAd ad, LoadAdError error) =>
{
_adStartLoadTimes[adUnitId] = Time.realtimeSinceStartup;
@ -80,7 +79,7 @@ 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;
}
@ -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();
}
}
@ -268,23 +268,34 @@ 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)
@ -310,7 +321,6 @@ namespace WZ
_adStartLoadTimes.Remove(key);
_adRevenueCache.Remove(key);
}
}
}
}