From f19681ea71d799d395725abf9836dfe6b9e8094b Mon Sep 17 00:00:00 2001 From: luojian Date: Mon, 15 Sep 2025 23:12:13 +0800 Subject: [PATCH] =?UTF-8?q?native=20=E8=87=AA=E5=8A=A8=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AdmobAdsManager/AdmobNativeAdManager.cs | 99 ++++++++++++++++--- 1 file changed, 83 insertions(+), 16 deletions(-) diff --git a/Assets/Script/SDKManager/AdsSDKManager/AdmobAdsManager/AdmobNativeAdManager.cs b/Assets/Script/SDKManager/AdsSDKManager/AdmobAdsManager/AdmobNativeAdManager.cs index 8815bd7..a7cb996 100644 --- a/Assets/Script/SDKManager/AdsSDKManager/AdmobAdsManager/AdmobNativeAdManager.cs +++ b/Assets/Script/SDKManager/AdsSDKManager/AdmobAdsManager/AdmobNativeAdManager.cs @@ -2,11 +2,24 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using EFSDK; using GoogleMobileAds.Api; using UnityEngine; namespace WZ { + class ShowNativePosition + { + public NativeOverlayAd NativeOverlayAd; + public NativeAdPosition Position; + + public ShowNativePosition(NativeOverlayAd nativeOverlayAd, NativeAdPosition position) + { + NativeOverlayAd = nativeOverlayAd; + Position = position; + } + } + public class AdmobNativeAdManager { private Dictionary _nativeAds = new Dictionary(); @@ -14,7 +27,7 @@ namespace WZ private Dictionary _retryCounters = new Dictionary(); private Dictionary _adStartLoadTimes = new Dictionary(); - private Dictionary showingNativeAds = new Dictionary(); + private Dictionary showingNativeAds = new(); public void InitializeAdUnits(List adUnitIds) { @@ -37,9 +50,9 @@ namespace WZ _adRevenueCache[adUnitId] = 0; } - public void LoadAd(string adUnitId) + public void LoadAd(string adUnitId, bool timingRefresh = false) { - LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load start"); + LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load start , timingRefresh {timingRefresh}"); //判断在线参数是否包含这个id if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Native, adUnitId)) { @@ -49,7 +62,7 @@ namespace WZ NativeOverlayAd.Load(adUnitId, new AdRequest(), new NativeAdOptions(), (NativeOverlayAd ad, LoadAdError error) => { _adStartLoadTimes[adUnitId] = Time.realtimeSinceStartup; - LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load end. {ad} error {error}"); + LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load end, timingRefresh {timingRefresh}. {ad} error {error}"); if (error != null || ad == null) { if (!_retryCounters.TryAdd(adUnitId, 0)) @@ -78,8 +91,8 @@ namespace WZ reason); var retryDelay = Math.Pow(2, Math.Min(6, _retryCounters[adUnitId])); - TimerUtils.Instance.DelayExecute((float)retryDelay, () => { LoadAd(adUnitId); }); - LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} ad failed to load an ad with error : " + error + " \n retryDelay :" + retryDelay); + TimerUtils.Instance.DelayExecute((float)retryDelay, () => { LoadAd(adUnitId, timingRefresh); }); + LoggerUtils.Error($"[Admob] Native Ad unit {adUnitId}, timingRefresh {timingRefresh} ad failed to load an ad with error : " + error + " \n retryDelay :" + retryDelay); return; } @@ -95,10 +108,13 @@ namespace WZ Time.realtimeSinceStartup - _adStartLoadTimes[adUnitId]); _retryCounters[adUnitId] = 0; + + // 临时缓存上一次的native ad,以便于刷新的时候,显示了新的,隐藏老的。 + var tempAd = _nativeAds.GetValueOrDefault(adUnitId, null); var nativeEcpm = AdmobUtils.GetNativeEcpm(ad); _nativeAds[adUnitId] = ad; _adRevenueCache[adUnitId] = nativeEcpm; - LoggerUtils.Debug($"Admob Native ad loaded with nativeEcpm = {nativeEcpm} response : " + ad.GetResponseInfo().ToString()); + LoggerUtils.Debug($"Admob Native ad loaded with, timingRefresh {timingRefresh} nativeEcpm = {nativeEcpm} response : " + ad.GetResponseInfo().ToString()); AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Native); ad.OnAdPaid += (AdValue adValue) => @@ -135,6 +151,19 @@ namespace WZ AdmobUtils.GetNativeEcpm(ad)); LoggerUtils.Debug("[Admob] Native ad full screen content closed."); }; + + if (timingRefresh && tempAd != null) + { + if (showingNativeAds.TryGetValue(adUnitId, out var showing)) + { + LoggerUtils.Warning("[Admob] Native ad timing refresh , show ad"); + ShowAd(showing.Position, adUnitId, tempAd); + } + else + { + LoggerUtils.Warning($"[Admob] Native ad timing refresh , show fail , showing native ads not ad unit id , {adUnitId}"); + } + } }); } @@ -152,7 +181,7 @@ namespace WZ } // 显示特定广告位的广告 - public void ShowAd(NativeAdPosition position, string adUnitId) + public void ShowAd(NativeAdPosition position, string adUnitId, NativeOverlayAd lastAd = null) { if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Native, adUnitId)) { @@ -182,11 +211,50 @@ 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; + showingNativeAds[adUnitId] = new ShowNativePosition(ad, position); ad.Show(); + lastAd?.Hide(); + TimingRefresh(adUnitId); } } + private void TimingRefresh(string adUnitId) + { + var nativeReflashGap = int.Parse(FireBaseRemoteConfigManager.Instance.GetRemoteConfigString("Native_Reflash_Gap", "0")); + + if (nativeReflashGap <= 0) + { + LoggerUtils.Debug($"[Admob] Native ad ({adUnitId}) timing refresh failed. nativeReflashGap = 0"); + return; + } + + if (adUnitId == StaticValue.AdmobFullNativeId) + { + LoggerUtils.Debug($"[Admob] Native ad ({adUnitId}) timing refresh finished. ad unit id is full native id."); + return; + } + + if (!showingNativeAds.ContainsKey(adUnitId)) + { + LoggerUtils.Debug($"[Admob] Native ad ({adUnitId}) timing refresh finished. ad unit is not show."); + return; + } + + WLoom.QueueOnMainThread(o => + { + var refreshAdUnitId = (string)o; + LoggerUtils.Debug($"[Admob] Native ad ({adUnitId}) timing refresh load start refreshAdUnitId : {refreshAdUnitId}."); + if (showingNativeAds.ContainsKey(refreshAdUnitId)) + { + LoadAd(refreshAdUnitId, true); + } + else + { + LoggerUtils.Warning($"[Admob] Native ad ({adUnitId}) timing refresh finished. ad unit is not show. refreshAdUnitId : {refreshAdUnitId}."); + } + }, adUnitId, nativeReflashGap); + } + private float GetLoadedTime(string adUnitId) { @@ -272,23 +340,22 @@ namespace WZ // yield return new WaitForSeconds(0.2f); if (adUnitId == null || string.IsNullOrEmpty(adUnitId)) { - foreach (var showingNativeAd in showingNativeAds) + foreach (var key in showingNativeAds.Keys.ToList()) { LoggerUtils.Debug($"[Admob] Native ad removing NativeAd {adUnitId}"); - showingNativeAd.Value.Hide(); - - LoadAd(showingNativeAd.Key); + showingNativeAds[key].NativeOverlayAd.Hide(); + // 从字典中删除元素 + showingNativeAds.Remove(key); + LoadAd(key); } - showingNativeAds.Clear(); - yield break; } if (showingNativeAds.TryGetValue(adUnitId, out var tempAd)) { LoggerUtils.Debug($"[Admob] Native ad removing NativeAd {adUnitId}"); - tempAd.Hide(); + tempAd.NativeOverlayAd.Hide(); showingNativeAds.Remove(adUnitId); LoadAd(adUnitId); }