211 lines
7.1 KiB
C#
211 lines
7.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BigoAds.Scripts.Api;
|
|
using Script.Utils;
|
|
using UnityEngine;
|
|
|
|
namespace Script.SDKManager.AdsSDKManager.BigoAdsManager
|
|
{
|
|
|
|
public class BigoRewardedAdManager
|
|
{
|
|
private Dictionary<string, BigoRewardedAd> _rewardedAds = new Dictionary<string, BigoRewardedAd>();
|
|
private Dictionary<string, int> _retryCounters = new Dictionary<string, int>();
|
|
private Dictionary<string, Action> _loadCallbacks = new Dictionary<string, Action>();
|
|
private Dictionary<string, double> _adRevenueCache = new Dictionary<string, double>();
|
|
|
|
public void InitializeAdUnits(List<string> adUnitIds, Action<string> onAdLoaded = null,
|
|
Action<string, int, string> onAdLoadFailed = null,
|
|
Action<string> onAdShowed = null,
|
|
Action<string> onAdDismissed = null,
|
|
Action<string, int, string> onAdError = null,
|
|
Action onAdClicked = null)
|
|
{
|
|
foreach (var adUnitId in adUnitIds)
|
|
{
|
|
CreateRewardedAd(adUnitId, onAdLoaded, onAdLoadFailed, onAdShowed, onAdDismissed, onAdError, onAdClicked);
|
|
}
|
|
}
|
|
|
|
private void CreateRewardedAd(string adUnitId, Action<string> onAdLoaded,
|
|
Action<string, int, string> onAdLoadFailed,
|
|
Action<string> onAdShowed,
|
|
Action<string> onAdDismissed,
|
|
Action<string, int, string> onAdError,
|
|
Action onAdClicked)
|
|
{
|
|
if (_rewardedAds.ContainsKey(adUnitId))
|
|
{
|
|
Debug.LogWarning($"[Bigo] Ad unit {adUnitId} already exists");
|
|
return;
|
|
}
|
|
|
|
var rewardedAd = new BigoRewardedAd(adUnitId);
|
|
_rewardedAds[adUnitId] = rewardedAd;
|
|
_retryCounters[adUnitId] = 0;
|
|
_adRevenueCache[adUnitId] = 0;
|
|
|
|
rewardedAd.OnLoad += () =>
|
|
{
|
|
_retryCounters[adUnitId] = 0;
|
|
_adRevenueCache[adUnitId] = GetAdRevenue(rewardedAd);
|
|
LoggerUtils.Debug($"[Bigo] Rewarded-[Load]: {adUnitId} successfully");
|
|
onAdLoaded?.Invoke(adUnitId);
|
|
};
|
|
|
|
rewardedAd.OnLoadFailed += ((code, msg) =>
|
|
{
|
|
_retryCounters[adUnitId]++;
|
|
double retryDelay = Math.Pow(2, Math.Min(6, _retryCounters[adUnitId]));
|
|
LoggerUtils.Debug($"[Bigo] Rewarded-[Load]: {adUnitId} failed, errorCode ={code}, error message = {msg}");
|
|
|
|
// 延迟重试加载
|
|
_loadCallbacks[adUnitId] = () => LoadAd(adUnitId);
|
|
TimerUtils.DelayExecute((float)retryDelay, () => _loadCallbacks[adUnitId]?.Invoke());
|
|
onAdLoadFailed?.Invoke(adUnitId, code, msg);
|
|
});
|
|
|
|
rewardedAd.OnAdShowed += (() =>
|
|
{
|
|
LoggerUtils.Debug($"[Bigo] Rewarded-[interaction]: {adUnitId} show");
|
|
AdjustTrackEvent.Instance.TrackAdEvent(GetAdRevenue(rewardedAd),"Bigo",adUnitId,adUnitId);
|
|
onAdShowed?.Invoke(adUnitId);
|
|
});
|
|
|
|
rewardedAd.OnAdDismissed += (() =>
|
|
{
|
|
LoggerUtils.Debug($"[Bigo] Rewarded-[interaction]: {adUnitId} dismiss");
|
|
onAdDismissed?.Invoke(adUnitId);
|
|
});
|
|
|
|
rewardedAd.OnAdError += ((code, msg) =>
|
|
{
|
|
LoggerUtils.Debug($"[Bigo] Rewarded-[interaction]: {adUnitId} ad error, errorCode ={code}, error message = {msg}");
|
|
onAdError?.Invoke(adUnitId, code, msg);
|
|
});
|
|
|
|
rewardedAd.OnAdClicked += () =>
|
|
{
|
|
onAdClicked?.Invoke();
|
|
};
|
|
LoadAd(adUnitId);
|
|
}
|
|
|
|
public void LoadAd(string adUnitId)
|
|
{
|
|
if (_rewardedAds.TryGetValue(adUnitId, out var ad))
|
|
{
|
|
ad.Load(new BigoRewardedRequest());
|
|
}
|
|
}
|
|
|
|
// 显示价格最高的广告
|
|
public void ShowHighestPayingAd()
|
|
{
|
|
var highestPayingAdUnit = GetHighestPayingAdUnit();
|
|
if (!string.IsNullOrEmpty(highestPayingAdUnit))
|
|
{
|
|
ShowAd(highestPayingAdUnit);
|
|
}
|
|
}
|
|
|
|
// 显示特定广告位的广告
|
|
private void ShowAd(string adUnitId)
|
|
{
|
|
if (_rewardedAds.TryGetValue(adUnitId, out var ad))
|
|
{
|
|
ad.Show();
|
|
}
|
|
}
|
|
|
|
|
|
// 检查特定广告位是否可用
|
|
private bool IsAdAvailable(string adUnitId)
|
|
{
|
|
return _rewardedAds.TryGetValue(adUnitId, out var ad) && ad.IsLoaded() && !ad.IsExpired();
|
|
}
|
|
|
|
// 获取所有可用的广告位
|
|
public List<string> GetAvailableAdUnits()
|
|
{
|
|
var available = new List<string>();
|
|
foreach (var kvp in _rewardedAds)
|
|
{
|
|
if (kvp.Value.IsLoaded() && !kvp.Value.IsExpired())
|
|
{
|
|
available.Add(kvp.Key);
|
|
}
|
|
}
|
|
return available;
|
|
}
|
|
|
|
// 获取广告收益
|
|
private double GetAdRevenue(BigoRewardedAd ad)
|
|
{
|
|
var bid = ad?.GetBid();
|
|
if (bid == null) return 0;
|
|
return bid.getPrice() / 1000;
|
|
}
|
|
|
|
// 获取特定广告位的收益信息
|
|
public double GetAdRevenue(string adUnitId)
|
|
{
|
|
if (_adRevenueCache.TryGetValue(adUnitId, out var revenue))
|
|
{
|
|
return revenue;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// 获取价格最高的广告位ID
|
|
public string GetHighestPayingAdUnit()
|
|
{
|
|
string highestPayingAdUnit = null;
|
|
double highestRevenue = 0;
|
|
|
|
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 _rewardedAds.Values)
|
|
{
|
|
ad.DestroyAd();
|
|
}
|
|
_rewardedAds.Clear();
|
|
_retryCounters.Clear();
|
|
_loadCallbacks.Clear();
|
|
_adRevenueCache.Clear();
|
|
}
|
|
}
|
|
|
|
}
|
|
|