Compare commits

..

No commits in common. "3286e0e33eeb6b513792e042860f88a508c97ce8" and "a1dff3ea7c1601b5e94b4295d2d07c3aba000b17" have entirely different histories.

5 changed files with 17 additions and 207 deletions

View File

@ -1,5 +1,4 @@
using System;
using GoogleMobileAds.Api;
using UnityEditor.Media;
namespace WZ
@ -197,8 +196,6 @@ namespace WZ
/// 广告高度
/// </summary>
public int Height;
public NativeTemplateStyle NativeTemplateStyle;
}
[System.Serializable]

View File

@ -1,199 +1,22 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using GoogleMobileAds.Api;
using UnityEngine;
namespace WZ
{
public class AdmobNativeAdManager
public class AdmobNativeAdManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
private Dictionary<string, NativeOverlayAd> _nativeAds = new Dictionary<string, NativeOverlayAd>();
private Dictionary<string, double> _adRevenueCache = new Dictionary<string, double>();
private Dictionary<string, int> _retryCounters = new Dictionary<string, int>();
public void InitializeAdUnits(List<string> adUnitIds)
{
foreach (var adUnitId in adUnitIds)
{
CreateNativeAd(adUnitId);
}
}
private void CreateNativeAd(string adUnitId)
// Update is called once per frame
void Update()
{
if (_nativeAds.ContainsKey(adUnitId))
{
LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} already exists");
return;
}
LoadAd(adUnitId);
_adRevenueCache[adUnitId] = 0;
}
public void LoadAd(string adUnitId)
{
LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load start");
NativeOverlayAd.Load(adUnitId, new AdRequest(), new NativeAdOptions(), (NativeOverlayAd ad, LoadAdError error) =>
{
if (error != null || ad == null)
{
_retryCounters[adUnitId]++;
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);
return;
}
_retryCounters[adUnitId] = 0;
LoggerUtils.Debug("Admob Native ad loaded with response : " + ad.GetResponseInfo().ToString());
_nativeAds[adUnitId] = ad;
_adRevenueCache[adUnitId] = AdmobUtils.GetNativeEcpm(ad);
AdsKeyEvents.Instance.LogAdFPUEvents(AdsType.Native);
ad.OnAdPaid += (AdValue adValue) =>
{
LoggerUtils.Debug($"[Admob] Native ad paid {adValue.Value} {adValue.CurrencyCode}.");
AdmobAdsManager.Instance.TrackAdImpression(ad.GetResponseInfo().GetLoadedAdapterResponseInfo(),
adValue,
AdsType.Native,
AdmobUtils.ParseResponseInfo(ad.GetResponseInfo()));
};
ad.OnAdImpressionRecorded += () => { LoggerUtils.Debug("[Admob] Native ad recorded an impression."); };
ad.OnAdClicked += () => { LoggerUtils.Debug("[Admob] Native ad was clicked."); };
ad.OnAdFullScreenContentOpened += () => { LoggerUtils.Debug("[Admob] Native ad full screen content opened."); };
ad.OnAdFullScreenContentClosed += () => { LoggerUtils.Debug("[Admob] Native ad full screen content closed."); };
});
}
// 显示价格最高的广告
public void ShowHighestPayingAd(NativeAdPosition position)
{
LoggerUtils.Debug($"[Admob] Native ad ShowHighestPayingAd {position}");
var highestPayingAdUnit = GetHighestPayingAdUnit();
LoggerUtils.Debug($"[Admob] Native ad ShowHighestPayingAd {position} , highestPayingAdUnit = {highestPayingAdUnit}");
if (!string.IsNullOrEmpty(highestPayingAdUnit))
{
ShowAd(position, highestPayingAdUnit);
}
}
// 显示特定广告位的广告
public void ShowAd(NativeAdPosition position, string adUnitId)
{
LoggerUtils.Debug($"[Admob] Native ad ShowAd start {adUnitId} , {position}");
if (_nativeAds.TryGetValue(adUnitId, out var ad))
{
LoggerUtils.Debug($"[Admob] Native ad ShowAd end {adUnitId} , {position}");
// Define a native template style with a custom style.
var style = position.NativeTemplateStyle ?? new NativeTemplateStyle()
{
TemplateId = NativeTemplateId.Medium,
// TemplateId = "big"
// MainBackgroundColor = Color.green,
CallToActionText = new NativeTemplateTextStyle()
{
FontSize = 9,
Style = NativeTemplateFontStyle.Bold
}
};
// Renders a native overlay ad at the default size
// and anchored to the bottom of the screne.
ad.RenderTemplate(style, new AdSize(position.Width, position.Height), position.X, position.Y);
ad.Show();
}
}
// 检查特定广告位是否可用
private bool IsAdAvailable(string adUnitId)
{
return _nativeAds.TryGetValue(adUnitId, out _);
}
// 获取所有可用的广告位
public List<string> GetAvailableAdUnits()
{
return _nativeAds.Select(kvp => kvp.Key).ToList();
}
// 获取价格最高的广告位ID
public string GetHighestPayingAdUnit()
{
string highestPayingAdUnit = null;
double highestRevenue = -1;
LoggerUtils.Debug($"[Admob] Native ad GetHighestPayingAdUnit {_adRevenueCache.Count}");
foreach (var kvp in _adRevenueCache)
{
var adUnitId = kvp.Key;
var revenue = kvp.Value;
// 确保广告确实已加载并且价格更高
if (IsAdAvailable(adUnitId) && revenue > highestRevenue)
{
highestRevenue = revenue;
highestPayingAdUnit = adUnitId;
}
}
return highestPayingAdUnit;
}
// 获取价格最高的广告收益信息
public double GetHighestPayingAdRevenue()
{
var highestPayingAdUnit = GetHighestPayingAdUnit();
if (!string.IsNullOrEmpty(highestPayingAdUnit) &&
_adRevenueCache.TryGetValue(highestPayingAdUnit, out var revenue))
{
return revenue;
}
return 0;
}
// 清理资源
public void Destroy()
{
foreach (var ad in _nativeAds.Values)
{
ad.Destroy();
}
_nativeAds.Clear();
_adRevenueCache.Clear();
}
public void RemoveNative(string adUnitId)
{
if (adUnitId == null || string.IsNullOrEmpty(adUnitId))
{
foreach (var ad in _nativeAds.Values)
{
ad.Hide();
}
return;
}
if (_nativeAds.TryGetValue(adUnitId, out var tempAd))
{
tempAd.Hide();
}
}
}
}

View File

@ -24,13 +24,9 @@ namespace WZ
pubid = loadedAdapterResponse["mediation_group_name"]?.ToString();
}
}
catch
{
}
catch { }
return pubid;
}
public static double GetBannerEcpm(BannerView bannerView)
{
return GetEcpm(bannerView, "bannerView", "adView", BannerStack);
@ -51,12 +47,6 @@ namespace WZ
return GetEcpm(appOpenAd, "androidAppOpenAd", "appOpenAd", SpStack);
}
public static double GetNativeEcpm(NativeOverlayAd ad)
{
return 0.0;
}
private static double GetEcpm(object o, string cShapeFieldName, string javaFieldName, string[] stack)
{
try

View File

@ -15,9 +15,9 @@ public class AppSDKManager : D_MonoSingleton<AppSDKManager>
{
FileParse.Parse();
// AdConfigParser.Parse();
FireBaseSDKManager.Instance.Init();
AdmobAdsManager.Instance.RefreshAdsData();
AdmobAdsManager.Instance.Initialize();
FireBaseSDKManager.Instance.Init();
AdjustManager.Instance.Init();
ShuShuMangage.Instance.Init();
AdsSDKManager.Instance.InitSDK(null);

View File

@ -10,10 +10,10 @@ namespace WZ
{
public void FetchRemoteConfig()
{
// Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
// {
// if (task.Result == Firebase.DependencyStatus.Available)
// {
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
{
if (task.Result == Firebase.DependencyStatus.Available)
{
Firebase.RemoteConfig.FirebaseRemoteConfig.DefaultInstance.FetchAsync(TimeSpan.Zero).ContinueWithOnMainThread(task =>
{
FirebaseRemoteConfig.DefaultInstance.ActivateAsync().ContinueWithOnMainThread(task =>
@ -33,8 +33,8 @@ namespace WZ
// 检查Adjust归因
});
});
// }
// });
}
});
}