SDK_UnityMoney/Assets/Script/SDKManager/AdsSDKManager/AdsSDKManager.cs

555 lines
20 KiB
C#
Raw Normal View History

2025-08-30 04:15:48 +00:00
using System;
using System.Collections;
2025-08-31 05:15:14 +00:00
using System.Collections.Generic;
using System.Linq;
using Firebase.RemoteConfig;
using Newtonsoft.Json;
2025-08-30 04:15:48 +00:00
using UnityEngine;
2025-09-01 10:32:50 +00:00
namespace WZ
2025-08-30 04:15:48 +00:00
{
2025-09-02 09:14:49 +00:00
public class AdsSDKManager : D_MonoSingleton<AdsSDKManager>
2025-08-30 04:15:48 +00:00
{
2025-09-01 10:32:50 +00:00
private List<IAdService> _adNetworks = new List<IAdService>();
// 是否有激励视频或者插屏广告在展示
public bool otherAdsOnShow = false;
2025-09-01 10:32:50 +00:00
public Action OnSplashAdCloseCallback;
2025-08-30 04:15:48 +00:00
2025-09-01 10:32:50 +00:00
public void InitSDK(Action action)
2025-08-31 05:15:14 +00:00
{
2025-09-02 13:42:07 +00:00
LoggerUtils.Debug("init ads sdk");
2025-09-01 10:32:50 +00:00
OnSplashAdCloseCallback = action;
2025-09-02 09:14:49 +00:00
// 初始化广告平台状态,用于竞价失败后刷新
2025-09-01 10:32:50 +00:00
BidPlatformManager.Instance.InitializePlatformStates(AdConfigParser.GetAdExpireInSec());
InitializeAdNetworks();
2025-08-31 05:15:14 +00:00
}
2025-09-01 10:32:50 +00:00
private void InitializeAdNetworks()
2025-08-31 05:15:14 +00:00
{
2025-09-17 00:16:54 +00:00
_adNetworks.Add(AdmobAdsManager.Instance);
_adNetworks.Add(BigoAdsManager.Instance);
_adNetworks.Add(TpnAdsManager.Instance);
_adNetworks.Add(MaxAdsManager.Instance);
_adNetworks.Add(KwaiAdsManager.Instance);
2025-09-01 10:32:50 +00:00
foreach (var network in _adNetworks)
{
2025-09-02 13:42:07 +00:00
LoggerUtils.Debug("init ads sdk network:"+network.ToString());
2025-09-01 10:32:50 +00:00
network.Initialize();
}
2025-09-01 13:28:17 +00:00
}
public void RefreshAdsData()
{
foreach (var network in _adNetworks)
{
LoggerUtils.Debug("init ads sdk network:"+network.ToString());
2025-09-01 13:28:17 +00:00
network.RefreshAdsData();
network.Initialize();
2025-09-01 13:28:17 +00:00
}
2025-08-31 05:15:14 +00:00
}
2025-09-01 15:29:31 +00:00
#region 广
2025-09-01 10:32:50 +00:00
public bool IsRewardAdReady()
2025-08-31 05:15:14 +00:00
{
2025-09-01 10:32:50 +00:00
return _adNetworks.Any(network => network.IsRewardedAvailable());
2025-08-31 05:15:14 +00:00
}
2025-09-01 10:32:50 +00:00
/// <summary>
/// 展示激励广告
/// </summary>
/// <param name="callback"></param>
2025-09-01 15:29:31 +00:00
public void ShowRewardAd(string _adPos, Action<bool, double> _rewardCallback = null, Action _showFailedCallback = null)
2025-08-31 05:15:14 +00:00
{
2025-09-02 09:14:49 +00:00
LoggerUtils.Debug("ShowRewardAd _adPos:"+_adPos+" ready:"+IsRewardAdReady());
otherAdsOnShow = true;
2025-09-01 10:32:50 +00:00
PlatformType result = GetBestPlatformType(false);
BidPlatformManager.Instance.RecordBidSuccess(result, AdsType.Rewarded);
if (result == PlatformType.AppLovin)
{
MaxAdsManager.Instance.DisplayRewarded(_adPos, _rewardCallback, _showFailedCallback);
}
else if (result == PlatformType.Admob)
{
AdmobAdsManager.Instance.DisplayRewarded(_adPos, _rewardCallback, _showFailedCallback);
}
else if (result == PlatformType.Bigo)
{
BigoAdsManager.Instance.DisplayRewarded(_adPos, _rewardCallback, _showFailedCallback);
}
else if (result == PlatformType.Topon)
{
TpnAdsManager.Instance.DisplayRewarded(_adPos, _rewardCallback, _showFailedCallback);
}
else if (result == PlatformType.Kwai)
{
KwaiAdsManager.Instance.DisplayRewarded(_adPos,_rewardCallback, _showFailedCallback);
2025-09-01 10:32:50 +00:00
}
else
{
_showFailedCallback?.Invoke();
}
2025-09-04 09:19:05 +00:00
AdPlayCountManager.IncrementAdsActionCount(AdsType.Rewarded,AdPlayCountManager.PLAY_COUNT_SUFFIX);
2025-09-02 13:57:18 +00:00
AdjustTrackEvent.Instance.TrackEventName("RV_Show", new Dictionary<string, object>());
2025-09-01 10:32:50 +00:00
CheckAndRefreshExpiredBids(AdsType.Rewarded);
2025-08-31 05:15:14 +00:00
}
/// <summary>
/// 获取激励广告价值
/// </summary>
/// <returns></returns>
public double GetRewardedAdRevenue()
{
if (!IsRewardAdReady())
{
return -1;
}
PlatformType result = GetBestPlatformType(false);
if (result == PlatformType.AppLovin)
{
return MaxAdsManager.Instance.GetRewardedRevenue();
}
else if (result == PlatformType.Admob)
{
return AdmobAdsManager.Instance.GetRewardedRevenue();
}
else if (result == PlatformType.Bigo)
{
return BigoAdsManager.Instance.GetRewardedRevenue();
}
else if (result == PlatformType.Kwai)
{
return KwaiAdsManager.Instance.GetRewardedRevenue();
}
2025-09-10 08:42:40 +00:00
else if (result == PlatformType.Topon)
{
return TpnAdsManager.Instance.GetRewardedRevenue();
}
else
{
return -1;
}
}
2025-09-01 15:29:31 +00:00
#endregion
2025-09-01 10:32:50 +00:00
2025-09-01 15:29:31 +00:00
#region 广
2025-09-01 10:32:50 +00:00
public bool IsInterstitialReady()
2025-08-31 05:15:14 +00:00
{
2025-09-01 10:32:50 +00:00
return _adNetworks.Any(network => network.IsInterstitialAvailable());
2025-08-31 05:15:14 +00:00
}
2025-09-01 10:32:50 +00:00
/// <summary>
/// 展示激励广告
/// </summary>
/// <param name="callback"></param>
2025-09-01 14:14:33 +00:00
public void ShowInterstitialAd(string _adPos, IvType _IvType = IvType.IV1, Action<double> _closeCallback = null)
2025-08-31 05:15:14 +00:00
{
2025-09-02 09:14:49 +00:00
AdsSDKManager.Instance.otherAdsOnShow = true;
2025-09-01 10:32:50 +00:00
PlatformType result = GetBestPlatformType(true);
BidPlatformManager.Instance.RecordBidSuccess(result, AdsType.Interstitial);
if (result == PlatformType.AppLovin)
{
MaxAdsManager.Instance.DisplayInterstitial(_adPos, _IvType, _closeCallback);
}
else if (result == PlatformType.Admob)
{
AdmobAdsManager.Instance.DisplayInterstitial(_adPos, _IvType, _closeCallback);
}
else if (result == PlatformType.Bigo)
{
BigoAdsManager.Instance.DisplayInterstitial(_adPos, _IvType, _closeCallback);
}
else if (result == PlatformType.Topon)
{
TpnAdsManager.Instance.DisplayInterstitial(_adPos, _IvType, _closeCallback);
}
else if (result == PlatformType.Kwai)
{
KwaiAdsManager.Instance.DisplayInterstitial(_adPos, _IvType, _closeCallback);
2025-09-01 10:32:50 +00:00
}
else
{
2025-09-01 14:14:33 +00:00
_closeCallback?.Invoke(0);
2025-09-01 10:32:50 +00:00
}
2025-08-31 05:15:14 +00:00
2025-09-04 09:19:05 +00:00
AdPlayCountManager.IncrementAdsActionCount(AdsType.Interstitial,AdPlayCountManager.PLAY_COUNT_SUFFIX);
2025-09-02 13:57:18 +00:00
AdjustTrackEvent.Instance.TrackEventName("IV_Show", new Dictionary<string, object>());
2025-09-01 10:32:50 +00:00
// 刷新其他类型广告
CheckAndRefreshExpiredBids(AdsType.Interstitial);
2025-08-31 05:15:14 +00:00
}
/// <summary>
/// 获取插屏广告价值
/// </summary>
/// <returns></returns>
public double GetInterstitialAdRevenue()
{
if (!IsInterstitialReady())
{
return -1;
}
PlatformType result = GetBestPlatformType(true);
if (result == PlatformType.AppLovin)
{
return MaxAdsManager.Instance.GetInterstitialRevenue();
}
else if (result == PlatformType.Admob)
{
return AdmobAdsManager.Instance.GetInterstitialRevenue();
}
else if (result == PlatformType.Bigo)
{
return BigoAdsManager.Instance.GetInterstitialRevenue();
}
else if (result == PlatformType.Topon)
{
return TpnAdsManager.Instance.GetInterstitialRevenue();
}
else if (result == PlatformType.Kwai)
{
return KwaiAdsManager.Instance.GetInterstitialRevenue();
}
else
{
return -1;
}
}
2025-09-01 15:29:31 +00:00
#endregion
#region 广
2025-09-16 13:29:51 +00:00
public bool IsBannerAdReady(BannerType bannerType)
2025-09-01 15:29:31 +00:00
{
2025-09-16 13:29:51 +00:00
return AdmobAdsManager.Instance.IsBannerAvailable(bannerType);
2025-09-01 15:29:31 +00:00
}
2025-09-16 13:29:51 +00:00
public void ShowBanner(BannerType bannerType,BannerAlignType bannerAlignType)
2025-09-01 15:29:31 +00:00
{
2025-09-16 13:29:51 +00:00
AdmobAdsManager.Instance.DisplayBanner(bannerType,bannerAlignType);
2025-09-04 09:19:05 +00:00
AdPlayCountManager.IncrementAdsActionCount(AdsType.Banner,AdPlayCountManager.PLAY_COUNT_SUFFIX);
AdjustTrackEvent.Instance.TrackEventName("Banner_Show", new Dictionary<string, object>());
2025-09-01 15:29:31 +00:00
}
2025-09-16 13:29:51 +00:00
public void HideBanner(BannerType bannerType)
2025-09-01 15:29:31 +00:00
{
2025-09-16 13:29:51 +00:00
AdmobAdsManager.Instance.HideBanner(bannerType);
2025-09-01 15:29:31 +00:00
}
#endregion
#region 广
2025-09-02 02:07:10 +00:00
public bool IsNativeAdReady(string adUnitId)
2025-09-01 15:29:31 +00:00
{
2025-09-02 02:07:10 +00:00
return AdmobAdsManager.Instance.IsNativeAvailable(adUnitId);
2025-09-01 15:29:31 +00:00
}
2025-09-02 02:07:10 +00:00
public void ShowNativeAd(string _adPos, string adUnitId, NativeAdPosition position)
2025-09-01 15:29:31 +00:00
{
AdmobAdsManager.Instance.DisplayNative(_adPos, adUnitId, position);
AdjustTrackEvent.Instance.TrackEventName("NA_Show", new Dictionary<string, object>());
2025-09-04 09:19:05 +00:00
AdPlayCountManager.IncrementAdsActionCount(AdsType.Native,AdPlayCountManager.PLAY_COUNT_SUFFIX);
2025-09-01 15:29:31 +00:00
}
2025-09-02 02:07:10 +00:00
public void RemoveNativeAd(string adUnitId)
2025-09-01 15:29:31 +00:00
{
2025-09-02 02:07:10 +00:00
AdmobAdsManager.Instance.RemoveNative(adUnitId);
2025-09-01 15:29:31 +00:00
}
public double GetNativeAdRevenue(string adUnitId)
{
if (!IsNativeAdReady(adUnitId))
{
return -1;
}
return AdmobAdsManager.Instance.GetNativeRevenue(adUnitId);
}
2025-09-01 15:29:31 +00:00
#endregion
2025-09-01 10:32:50 +00:00
#region 广
public bool IsSplashAvailable()
2025-08-31 05:15:14 +00:00
{
2025-09-02 13:42:07 +00:00
return AdmobAdsManager.Instance.IsSplashAvailable();
2025-08-31 05:15:14 +00:00
}
2025-09-01 10:32:50 +00:00
public void ShowSplashAd()
2025-08-31 05:15:14 +00:00
{
2025-09-02 13:57:18 +00:00
AdjustTrackEvent.Instance.TrackEventName("SP_Show", new Dictionary<string, object>());
2025-09-04 09:19:05 +00:00
AdPlayCountManager.IncrementAdsActionCount(AdsType.Splash,AdPlayCountManager.PLAY_COUNT_SUFFIX);
2025-09-01 10:32:50 +00:00
AdmobAdsManager.Instance.DisplaySplash();
2025-08-31 05:15:14 +00:00
}
2025-09-01 10:32:50 +00:00
public void LoadSplashAd()
2025-08-31 05:15:14 +00:00
{
2025-09-01 10:32:50 +00:00
AdmobAdsManager.Instance.LoadSplash();
2025-08-31 05:15:14 +00:00
}
2025-09-01 10:32:50 +00:00
#endregion
private PlatformType GetBestPlatformType(bool isInterstitial)
2025-08-31 05:15:14 +00:00
{
2025-09-01 10:32:50 +00:00
if (isInterstitial)
{
AdPriceInfo priceInfo = new AdPriceInfo(
2025-09-17 00:16:03 +00:00
maxPrice: MaxAdsManager.Instance.GetInterstitialRevenue(),
admobPrice: AdmobAdsManager.Instance.GetInterstitialRevenue(),
bigoPrice: BigoAdsManager.Instance.GetInterstitialRevenue(),
kwaiPrice: KwaiAdsManager.Instance.GetInterstitialRevenue(),
2025-09-01 12:53:08 +00:00
toponAdUnitId: TpnAdsManager.Instance._topon_interstitial_units
2025-09-01 10:32:50 +00:00
);
return AdsBidResult.GetPlatformType(priceInfo);
}
else
{
AdPriceInfo priceInfo = new AdPriceInfo(
2025-09-17 00:16:03 +00:00
maxPrice: MaxAdsManager.Instance.GetRewardedRevenue(),
admobPrice: AdmobAdsManager.Instance.GetRewardedRevenue(),
bigoPrice: BigoAdsManager.Instance.GetRewardedRevenue(),
kwaiPrice: KwaiAdsManager.Instance.GetRewardedRevenue(),
2025-09-01 12:53:08 +00:00
toponAdUnitId: TpnAdsManager.Instance._topon_rewarded_units
2025-09-01 10:32:50 +00:00
);
return AdsBidResult.GetPlatformType(priceInfo);
}
2025-08-31 05:15:14 +00:00
}
2025-09-10 08:42:40 +00:00
/// <summary>
/// 广告比价类型
/// </summary>
/// <returns></returns>
public AdsType GetAdBidType()
{
var adsType = AdsBidResult.GetAdsType(TpnAdsManager.Instance._topon_rewarded_units, TpnAdsManager.Instance._topon_interstitial_units, GetRewardedAdRevenue(), GetInterstitialAdRevenue(), RushSDKManager.Instance.GetFullNativeRevenue());
LoggerUtils.Debug($"[SDK] AdBidType: {adsType} ");
return adsType;
}
2025-08-31 05:15:14 +00:00
2025-09-02 06:20:54 +00:00
#region IvRules
public bool IvRulesShow(IvType ivadType, bool isShow = true)
2025-09-01 15:29:31 +00:00
{
//1.获取远程配置
string json = FireBaseRemoteConfigManager.Instance.GetRemoteConfigString("IV_RULES");
if (string.IsNullOrEmpty(json))
{
LoggerUtils.Debug("[SDK] 获取远程配置IV_RULES是空 没有限制");
return true;
}
//2.解析配置
var dates = JsonConvert.DeserializeObject<IvRulesData[]>(json);
if (dates == null && dates.Length == 0)
{
LoggerUtils.Debug("[SDK] 获取远程配置信息是空");
return true;
}
//3.获取IVADType对应的配置
2025-09-01 15:29:31 +00:00
IvRulesData ivRulesData = null;
foreach (var data in dates)
{
if (data.type == (int)ivadType)
{
ivRulesData = data;
}
}
if (ivRulesData == null)
{
LoggerUtils.Debug("[SDK] 远程配置没有配置对应的IvType");
2025-09-01 15:29:31 +00:00
return true;
}
//4.判断skip(次安装跳过几次触发不展示广告)
int skipLevel = ivRulesData.skipLevel;
2025-09-02 06:20:54 +00:00
int currentSkipLevel = PlayerPrefsUtils.GetPlayerPrefsInt($"{IvRulesKey.KEY_SKIPLEVEL}_{ivadType.ToString()}", 0);
LoggerUtils.Debug($"[SDK] {ivadType.ToString()} 前N次不展示插屏, 本地次数是{currentSkipLevel + 1} 远程参数是{skipLevel}");
2025-09-01 15:29:31 +00:00
if (currentSkipLevel < skipLevel)
{
if (isShow)
{
PlayerPrefsUtils.SavePlayerPrefsInt($"{IvRulesKey.KEY_SKIPLEVEL}_{ivadType.ToString()}", currentSkipLevel + 1);
}
2025-09-01 15:29:31 +00:00
return false;
}
//5.判断overLevel(每跳过几次触发) 第一次会展示 之后每展示一次间隔+1
2025-09-01 15:29:31 +00:00
int overLevel = ivRulesData.overLevel;
2025-09-02 06:20:54 +00:00
int currentOverLevel = IvRulesConst.OverLevels.ContainsKey(ivadType.ToString()) ? IvRulesConst.OverLevels[ivadType.ToString()] : 0;
LoggerUtils.Debug($"[SDK] {ivadType.ToString()} 当前间隔次数: 本地次数是{currentOverLevel + 1} 远程参数是{overLevel}");
if (currentOverLevel != 0)
2025-09-01 15:29:31 +00:00
{
if (isShow)
{
if (currentOverLevel >= overLevel)
{
IvRulesConst.OverLevels[ivadType.ToString()] = 0;
}
else
{
IvRulesConst.OverLevels[ivadType.ToString()] += 1;
}
}
2025-09-01 15:29:31 +00:00
return false;
}
//6.判断interval(广告时间间隔)
int interval = ivRulesData.interval;
2025-09-02 06:20:54 +00:00
long currentInterval = IvRulesConst.Intervals.ContainsKey(ivadType.ToString()) ? IvRulesConst.Intervals[ivadType.ToString()] : 0;
2025-09-01 15:29:31 +00:00
long localTimestamp = TimeUtils.GetLocalTimestamp();
LoggerUtils.Debug($"[SDK] {ivadType.ToString()} 远程参数是: {interval} CanShowFlag: {localTimestamp > (currentInterval + (interval * 1000L))}");
2025-09-01 15:29:31 +00:00
if (localTimestamp < currentInterval + (interval * 1000L))
{
return false;
}
return true;
2025-09-01 15:29:31 +00:00
}
2025-09-02 06:20:54 +00:00
/// <summary>
/// 看激励广告之后调用
/// </summary>
public void ClearIvRules()
{
var localTimestamp = TimeUtils.GetLocalTimestamp();
foreach (var key in IvRulesConst.Intervals.Keys.ToList())
{
IvRulesConst.Intervals[key] = localTimestamp;
}
}
2025-09-02 06:20:54 +00:00
#endregion
2025-09-01 15:29:31 +00:00
#region
2025-09-01 10:32:50 +00:00
private void CheckAndRefreshExpiredBids(AdsType _adsType)
2025-08-31 05:15:14 +00:00
{
2025-09-01 10:32:50 +00:00
Dictionary<PlatformType, List<AdsType>> expiredBids = BidPlatformManager.Instance.GetExpiredBids();
2025-08-31 05:15:14 +00:00
2025-09-01 10:32:50 +00:00
foreach (var kvp in expiredBids)
{
PlatformType platformName = kvp.Key;
foreach (AdsType adType in kvp.Value)
{
if (_adsType == adType)
{
LoggerUtils.Debug($"{platformName} 平台 {adType} 广告竞价已过期,重新加载广告");
RefreshPlatformAds(platformName, adType);
}
}
}
2025-08-31 05:15:14 +00:00
}
2025-09-01 10:32:50 +00:00
private void RefreshPlatformAds(PlatformType platformName, AdsType adType)
2025-08-31 05:15:14 +00:00
{
2025-09-01 10:32:50 +00:00
switch (platformName)
{
case PlatformType.Admob:
RefreshAdmobAds(adType);
break;
case PlatformType.Topon:
RefreshTopOnAds(adType);
break;
case PlatformType.AppLovin:
RefreshMaxAds(adType);
break;
case PlatformType.Bigo:
RefreshBigoAds(adType);
break;
case PlatformType.Kwai:
RefreshKwaiAds(adType);
break;
default:
break;
}
2025-08-31 05:15:14 +00:00
}
2025-08-30 04:15:48 +00:00
2025-09-01 10:32:50 +00:00
private void RefreshAdmobAds(AdsType adType)
{
2025-09-01 10:32:50 +00:00
switch (adType)
{
2025-09-01 10:32:50 +00:00
case AdsType.Rewarded:
AdmobAdsManager.Instance.LoadRewarded();
break;
case AdsType.Interstitial:
AdmobAdsManager.Instance.LoadInterstitial();
break;
case AdsType.Banner:
AdmobAdsManager.Instance.LoadBanner();
break;
case AdsType.Native:
AdmobAdsManager.Instance.LoadNative();
break;
case AdsType.Splash:
AdmobAdsManager.Instance.LoadSplash();
break;
default:
break;
}
}
2025-09-01 10:32:50 +00:00
// 刷新TopOn广告
private void RefreshTopOnAds(AdsType adType)
{
2025-09-01 10:32:50 +00:00
switch (adType)
{
case AdsType.Rewarded:
TpnAdsManager.Instance.LoadRewarded();
break;
case AdsType.Interstitial:
TpnAdsManager.Instance.LoadInterstitial();
break;
default:
break;
}
}
2025-09-01 10:32:50 +00:00
// 刷新Max广告
private void RefreshMaxAds(AdsType adType)
{
2025-09-01 10:32:50 +00:00
switch (adType)
{
case AdsType.Rewarded:
MaxAdsManager.Instance.LoadRewarded();
break;
case AdsType.Interstitial:
MaxAdsManager.Instance.LoadInterstitial();
break;
default:
break;
}
}
2025-09-01 10:32:50 +00:00
// 刷新Bigo广告
private void RefreshBigoAds(AdsType adType)
{
2025-09-01 10:32:50 +00:00
switch (adType)
{
case AdsType.Rewarded:
BigoAdsManager.Instance.LoadRewarded();
break;
case AdsType.Interstitial:
BigoAdsManager.Instance.LoadInterstitial();
break;
default:
break;
}
}
2025-09-01 10:32:50 +00:00
private void RefreshKwaiAds(AdsType adType)
{
2025-09-01 10:32:50 +00:00
switch (adType)
{
case AdsType.Rewarded:
KwaiAdsManager.Instance.LoadRewarded();
2025-09-01 10:32:50 +00:00
break;
case AdsType.Interstitial:
KwaiAdsManager.Instance.LoadInterstitial();
2025-09-01 10:32:50 +00:00
break;
default:
break;
}
}
2025-09-01 15:29:31 +00:00
#endregion
}
2025-09-01 10:32:50 +00:00
}