Compare commits

..

5 Commits

1 changed files with 98 additions and 21 deletions

View File

@ -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<string, NativeOverlayAd> _nativeAds = new Dictionary<string, NativeOverlayAd>();
@ -14,6 +27,7 @@ namespace WZ
private Dictionary<string, int> _retryCounters = new Dictionary<string, int>();
private Dictionary<string, float> _adStartLoadTimes = new Dictionary<string, float>();
private Dictionary<string, ShowNativePosition> showingNativeAds = new();
public void InitializeAdUnits(List<string> adUnitIds)
{
@ -36,21 +50,19 @@ 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))
{
return;
}
_nativeAds.Remove(adUnitId);
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))
@ -79,11 +91,11 @@ 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 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;
}
if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Native, adUnitId))
{
return;
@ -96,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) =>
@ -136,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}");
}
}
});
}
@ -153,13 +181,13 @@ 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))
{
return;
}
LoggerUtils.Debug($"[Admob] Native ad ShowAd start {adUnitId} , {position}");
if (_nativeAds.TryGetValue(adUnitId, out var ad))
@ -183,10 +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] = 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)
{
@ -240,7 +308,7 @@ namespace WZ
return 0;
}
// 获取广告收益信息
public double GetAdRevenue(string adUnit)
{
@ -252,7 +320,7 @@ namespace WZ
return -1;
}
// 清理资源
public void Destroy()
@ -268,25 +336,35 @@ 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 key in showingNativeAds.Keys.ToList())
{
nativeOverlayAd.Value.Hide();
LoadAd(nativeOverlayAd.Key);
LoggerUtils.Debug($"[Admob] Native ad removing NativeAd {adUnitId}");
showingNativeAds[key].NativeOverlayAd.Hide();
// 从字典中删除元素
showingNativeAds.Remove(key);
LoadAd(key);
}
yield break;
}
if (_nativeAds.TryGetValue(adUnitId, out var tempAd))
if (showingNativeAds.TryGetValue(adUnitId, out var tempAd))
{
tempAd.Hide();
LoggerUtils.Debug($"[Admob] Native ad removing NativeAd {adUnitId}");
tempAd.NativeOverlayAd.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 +388,6 @@ namespace WZ
_adStartLoadTimes.Remove(key);
_adRevenueCache.Remove(key);
}
}
}
}