Compare commits
No commits in common. "fb022bf083a196d984371718356dd2711c4cdb3f" and "3ed6641e3d57ecb640622f6f6b0cd6b394476a61" have entirely different histories.
fb022bf083
...
3ed6641e3d
|
|
@ -2,24 +2,11 @@ using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using EFSDK;
|
|
||||||
using GoogleMobileAds.Api;
|
using GoogleMobileAds.Api;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace WZ
|
namespace WZ
|
||||||
{
|
{
|
||||||
class ShowNativePosition
|
|
||||||
{
|
|
||||||
public NativeOverlayAd NativeOverlayAd;
|
|
||||||
public NativeAdPosition Position;
|
|
||||||
|
|
||||||
public ShowNativePosition(NativeOverlayAd nativeOverlayAd, NativeAdPosition position)
|
|
||||||
{
|
|
||||||
NativeOverlayAd = nativeOverlayAd;
|
|
||||||
Position = position;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class AdmobNativeAdManager
|
public class AdmobNativeAdManager
|
||||||
{
|
{
|
||||||
private Dictionary<string, NativeOverlayAd> _nativeAds = new Dictionary<string, NativeOverlayAd>();
|
private Dictionary<string, NativeOverlayAd> _nativeAds = new Dictionary<string, NativeOverlayAd>();
|
||||||
|
|
@ -27,7 +14,6 @@ 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, ShowNativePosition> showingNativeAds = new();
|
|
||||||
|
|
||||||
public void InitializeAdUnits(List<string> adUnitIds)
|
public void InitializeAdUnits(List<string> adUnitIds)
|
||||||
{
|
{
|
||||||
|
|
@ -50,19 +36,21 @@ namespace WZ
|
||||||
_adRevenueCache[adUnitId] = 0;
|
_adRevenueCache[adUnitId] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LoadAd(string adUnitId, bool timingRefresh = false)
|
public void LoadAd(string adUnitId)
|
||||||
{
|
{
|
||||||
LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load start , timingRefresh {timingRefresh}");
|
LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load start");
|
||||||
//判断在线参数是否包含这个id
|
//判断在线参数是否包含这个id
|
||||||
if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Native, adUnitId))
|
if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Native, adUnitId))
|
||||||
{
|
{
|
||||||
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) =>
|
||||||
{
|
{
|
||||||
_adStartLoadTimes[adUnitId] = Time.realtimeSinceStartup;
|
_adStartLoadTimes[adUnitId] = Time.realtimeSinceStartup;
|
||||||
LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load end, timingRefresh {timingRefresh}. {ad} error {error}");
|
LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load end. {ad} error {error}");
|
||||||
if (error != null || ad == null)
|
if (error != null || ad == null)
|
||||||
{
|
{
|
||||||
if (!_retryCounters.TryAdd(adUnitId, 0))
|
if (!_retryCounters.TryAdd(adUnitId, 0))
|
||||||
|
|
@ -91,11 +79,11 @@ namespace WZ
|
||||||
reason);
|
reason);
|
||||||
|
|
||||||
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, timingRefresh); });
|
TimerUtils.Instance.DelayExecute((float)retryDelay, () => { LoadAd(adUnitId); });
|
||||||
LoggerUtils.Error($"[Admob] Native Ad unit {adUnitId}, timingRefresh {timingRefresh} ad failed to load an ad with error : " + error + " \n retryDelay :" + retryDelay);
|
LoggerUtils.Debug("[Admob] Native 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;
|
||||||
|
|
@ -108,13 +96,10 @@ namespace WZ
|
||||||
Time.realtimeSinceStartup - _adStartLoadTimes[adUnitId]);
|
Time.realtimeSinceStartup - _adStartLoadTimes[adUnitId]);
|
||||||
_retryCounters[adUnitId] = 0;
|
_retryCounters[adUnitId] = 0;
|
||||||
|
|
||||||
|
|
||||||
// 临时缓存上一次的native ad,以便于刷新的时候,显示了新的,隐藏老的。
|
|
||||||
var tempAd = _nativeAds.GetValueOrDefault(adUnitId, null);
|
|
||||||
var nativeEcpm = AdmobUtils.GetNativeEcpm(ad);
|
var nativeEcpm = AdmobUtils.GetNativeEcpm(ad);
|
||||||
_nativeAds[adUnitId] = ad;
|
_nativeAds[adUnitId] = ad;
|
||||||
_adRevenueCache[adUnitId] = nativeEcpm;
|
_adRevenueCache[adUnitId] = nativeEcpm;
|
||||||
LoggerUtils.Debug($"Admob Native ad loaded with, timingRefresh {timingRefresh} nativeEcpm = {nativeEcpm} response : " + ad.GetResponseInfo().ToString());
|
LoggerUtils.Debug($"Admob Native ad loaded with nativeEcpm = {nativeEcpm} response : " + ad.GetResponseInfo().ToString());
|
||||||
AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Native);
|
AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Native);
|
||||||
|
|
||||||
ad.OnAdPaid += (AdValue adValue) =>
|
ad.OnAdPaid += (AdValue adValue) =>
|
||||||
|
|
@ -151,19 +136,6 @@ namespace WZ
|
||||||
AdmobUtils.GetNativeEcpm(ad));
|
AdmobUtils.GetNativeEcpm(ad));
|
||||||
LoggerUtils.Debug("[Admob] Native ad full screen content closed.");
|
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}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -181,13 +153,13 @@ namespace WZ
|
||||||
}
|
}
|
||||||
|
|
||||||
// 显示特定广告位的广告
|
// 显示特定广告位的广告
|
||||||
public void ShowAd(NativeAdPosition position, string adUnitId, NativeOverlayAd lastAd = null)
|
public void ShowAd(NativeAdPosition position, string adUnitId)
|
||||||
{
|
{
|
||||||
if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Native, adUnitId))
|
if (!AdmobAdsManager.Instance.FindAdsID(AdsType.Native, adUnitId))
|
||||||
{
|
{
|
||||||
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))
|
||||||
|
|
@ -211,50 +183,10 @@ 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] = new ShowNativePosition(ad, position);
|
|
||||||
ad.Show();
|
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)
|
private float GetLoadedTime(string adUnitId)
|
||||||
{
|
{
|
||||||
|
|
@ -308,7 +240,7 @@ namespace WZ
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取广告收益信息
|
// 获取广告收益信息
|
||||||
public double GetAdRevenue(string adUnit)
|
public double GetAdRevenue(string adUnit)
|
||||||
{
|
{
|
||||||
|
|
@ -320,7 +252,7 @@ namespace WZ
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 清理资源
|
// 清理资源
|
||||||
public void Destroy()
|
public void Destroy()
|
||||||
|
|
@ -336,35 +268,25 @@ 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 key in showingNativeAds.Keys.ToList())
|
foreach (var nativeOverlayAd in _nativeAds)
|
||||||
{
|
{
|
||||||
LoggerUtils.Debug($"[Admob] Native ad removing NativeAd {adUnitId}");
|
nativeOverlayAd.Value.Hide();
|
||||||
showingNativeAds[key].NativeOverlayAd.Hide();
|
LoadAd(nativeOverlayAd.Key);
|
||||||
// 从字典中删除元素
|
|
||||||
showingNativeAds.Remove(key);
|
|
||||||
LoadAd(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (showingNativeAds.TryGetValue(adUnitId, out var tempAd))
|
if (_nativeAds.TryGetValue(adUnitId, out var tempAd))
|
||||||
{
|
{
|
||||||
LoggerUtils.Debug($"[Admob] Native ad removing NativeAd {adUnitId}");
|
tempAd.Hide();
|
||||||
tempAd.NativeOverlayAd.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以提高查找性能
|
||||||
|
|
@ -388,6 +310,7 @@ namespace WZ
|
||||||
_adStartLoadTimes.Remove(key);
|
_adStartLoadTimes.Remove(key);
|
||||||
_adRevenueCache.Remove(key);
|
_adRevenueCache.Remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue