添加max广告类
This commit is contained in:
parent
4662ead100
commit
927ede23b4
|
@ -1,18 +1,238 @@
|
||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using NUnit.Framework.Internal;
|
||||||
|
using Script.Common;
|
||||||
|
using Script.SDKManager.AdsSDKManager.Utils;
|
||||||
|
using Script.Utils;
|
||||||
|
using SDK.Utils;
|
||||||
|
using SDKManager.AdsSDKManager.Constant;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class MaxAdsManager : MonoBehaviour
|
namespace Script.SDKManager.AdsSDKManager.MaxAdsManager
|
||||||
{
|
{
|
||||||
// Start is called before the first frame update
|
public class MaxAdsManager : NormalSingleton<MaxAdsManager>, IAdService
|
||||||
void Start()
|
|
||||||
{
|
{
|
||||||
|
public string ClientName => "Max";
|
||||||
|
private string max_app_key;
|
||||||
|
private string max_interstitial_units;
|
||||||
|
private string max_rewarded_units;
|
||||||
|
private MaxSdkBase.AdInfo _interAdInfo;
|
||||||
|
private MaxSdkBase.AdInfo _rewardAdInfo;
|
||||||
|
private int _rewardRetryAttempt;
|
||||||
|
private int _interRetryAttempt;
|
||||||
|
private float _rvStartLoadTime = 0;
|
||||||
|
private float _ivStartLoadTime = 0;
|
||||||
|
|
||||||
}
|
public void Initialize()
|
||||||
|
{
|
||||||
|
max_app_key = AdConfigParser.GetMaxAppKey();
|
||||||
|
max_interstitial_units = AdConfigParser.GetMaxAdUnits(AdsType.Interstitial).ToArray()[0];
|
||||||
|
max_rewarded_units = AdConfigParser.GetMaxAdUnits(AdsType.Rewarded).ToArray()[0];
|
||||||
|
if (string.IsNullOrEmpty(max_app_key)) return;
|
||||||
|
LoggerUtils.Debug("[Max] init max");
|
||||||
|
// 关闭反转debug面板
|
||||||
|
MaxSdk.SetCreativeDebuggerEnabled(false);
|
||||||
|
|
||||||
// Update is called once per frame
|
InitRewardedCallback();
|
||||||
void Update()
|
InitInterstitialCallback();
|
||||||
{
|
|
||||||
|
MaxSdkCallbacks.OnSdkInitializedEvent += (MaxSdkBase.SdkConfiguration obj) =>
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] init max success");
|
||||||
|
|
||||||
|
};
|
||||||
|
MaxSdk.SetVerboseLogging(false);
|
||||||
|
MaxSdk.SetSdkKey(max_app_key);
|
||||||
|
MaxSdk.InitializeSdk();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#region 激励广告功能
|
||||||
|
private void InitRewardedCallback()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(max_rewarded_units)) return;
|
||||||
|
MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent;
|
||||||
|
MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailedEvent;
|
||||||
|
MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent;
|
||||||
|
MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += OnRewardedAdDisplayedEvent;
|
||||||
|
MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent;
|
||||||
|
MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdHiddenEvent;
|
||||||
|
MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnRewardedRevenue;
|
||||||
|
|
||||||
|
}
|
||||||
|
public void LoadRewarded()
|
||||||
|
{
|
||||||
|
MaxSdk.LoadRewardedAd(max_rewarded_units);
|
||||||
|
}
|
||||||
|
public void DisplayRewarded()
|
||||||
|
{
|
||||||
|
MaxSdk.ShowRewardedAd(max_rewarded_units);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double GetRewardedRevenue()
|
||||||
|
{
|
||||||
|
if (IsRewardedAvailable() && _rewardAdInfo != null)
|
||||||
|
{
|
||||||
|
return _rewardAdInfo.Revenue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsRewardedAvailable()
|
||||||
|
{
|
||||||
|
return string.IsNullOrEmpty(max_rewarded_units) ? false : MaxSdk.IsRewardedAdReady(max_rewarded_units);;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRewardedRevenue(string adunit, MaxSdkBase.AdInfo adInfo)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnRewardedRevenue network: " + adInfo.NetworkName + " revenue: " + adInfo.Revenue + " NetworkPlacement: " + adInfo.NetworkPlacement + " AdUnitIdentifier: " + adInfo.AdUnitIdentifier + " Placement: " + adInfo.Placement);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRewardedAdLoadedEvent(string adunit, MaxSdkBase.AdInfo adInfo)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnRewardedAdLoadedEvent network: " + adInfo.NetworkName + " revenue: " + adInfo.Revenue + " NetworkPlacement: " + adInfo.NetworkPlacement + " AdUnitIdentifier: " + adInfo.AdUnitIdentifier + " Placement: " + adInfo.Placement);
|
||||||
|
_rewardAdInfo = adInfo;
|
||||||
|
_rewardRetryAttempt = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRewardedAdLoadFailedEvent(string adunit, MaxSdkBase.ErrorInfo errorInfo)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnRewardedAdLoadFailedEvent errorInfo" + errorInfo.Message);
|
||||||
|
_rewardRetryAttempt++;
|
||||||
|
double retryDelay = Math.Pow(2, Math.Min(6, _rewardRetryAttempt));
|
||||||
|
TimerUtils.DelayExecute((float)retryDelay, LoadRewarded);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRewardedAdDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo info)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnRewardedAdDisplayedEvent network: " + info.NetworkName + " revenue: " + info.Revenue + " NetworkPlacement: " + info.NetworkPlacement + " AdUnitIdentifier: " + info.AdUnitIdentifier + " Placement: " + info.Placement);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRewardedAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnRewardedAdClickedEvent");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnRewardedAdFailedToDisplayEvent :" + errorInfo.Message + " " + errorInfo.Code + " " + errorInfo.MediatedNetworkErrorMessage + " " + errorInfo.MediatedNetworkErrorCode);
|
||||||
|
LoadRewarded();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRewardedAdHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
|
||||||
|
{
|
||||||
|
LoadRewarded();
|
||||||
|
LoggerUtils.Debug("[Max] OnRewardedAdHiddenEvent");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 插页广告功能
|
||||||
|
private void InitInterstitialCallback()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(max_interstitial_units)) return;
|
||||||
|
MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent;
|
||||||
|
MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialLoadFailedEvent;
|
||||||
|
MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += OnInterstitialAdFailedToDisplayEvent;
|
||||||
|
MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialHiddenEvent;
|
||||||
|
MaxSdkCallbacks.Interstitial.OnAdDisplayedEvent += OnInterstitialDisplayedEvent;
|
||||||
|
MaxSdkCallbacks.Interstitial.OnAdRevenuePaidEvent += OnInterstitialRevenue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void LoadInterstitial()
|
||||||
|
{
|
||||||
|
MaxSdk.LoadInterstitial(max_interstitial_units);
|
||||||
|
}
|
||||||
|
public double GetInterstitialRevenue()
|
||||||
|
{
|
||||||
|
if (IsInterstitialAvailable() && _interAdInfo != null)
|
||||||
|
{
|
||||||
|
return _interAdInfo.Revenue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DisplayInterstitial()
|
||||||
|
{
|
||||||
|
MaxSdk.ShowInterstitial(max_interstitial_units);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsInterstitialAvailable()
|
||||||
|
{
|
||||||
|
return string.IsNullOrEmpty(max_interstitial_units) ? false : MaxSdk.IsInterstitialReady(max_interstitial_units);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnInterstitialLoadedEvent(string adunit, MaxSdkBase.AdInfo adInfo)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnInterstitialLoadedEvent network: " + adInfo.NetworkName + " revenue: " + adInfo.Revenue + " NetworkPlacement: " + adInfo.NetworkPlacement + " AdUnitIdentifier: " + adInfo.AdUnitIdentifier + " Placement: " + adInfo.Placement);
|
||||||
|
|
||||||
|
_interAdInfo = adInfo;
|
||||||
|
_interRetryAttempt = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnInterstitialLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnInterstitialLoadFailedEvent :" + errorInfo);
|
||||||
|
_interRetryAttempt++;
|
||||||
|
double retryDelay = Math.Pow(2, Math.Min(6, _interRetryAttempt));
|
||||||
|
TimerUtils.DelayExecute((float)retryDelay, LoadInterstitial);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnInterstitialAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo,MaxSdkBase.AdInfo arg3)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnInterstitialAdFailedToDisplayEvent :" + errorInfo.Message + " " + errorInfo.Code + " " + errorInfo.MediatedNetworkErrorMessage + " " + errorInfo.MediatedNetworkErrorCode);
|
||||||
|
LoadInterstitial();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnInterstitialDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo info)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug($"[Max] OnInterstitialDisplayedEvent network: {info.NetworkName}, revenue: {info.Revenue},NetworkPlacement: {info.NetworkPlacement}, AdUnitIdentifier: {info.AdUnitIdentifier},Placement: {info.Placement}");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnInterstitialHiddenEvent(string adUnitId, MaxSdkBase.AdInfo info)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug($"[Max] OnInterstitialHiddenEvent");
|
||||||
|
LoadInterstitial();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnInterstitialRevenue(string adunit, MaxSdkBase.AdInfo adInfo)
|
||||||
|
{
|
||||||
|
LoggerUtils.Debug("[Max] OnInterstitialRevenue network: " + adInfo.NetworkName + " revenue: " + adInfo.Revenue + " NetworkPlacement: " + adInfo.NetworkPlacement + " AdUnitIdentifier: " + adInfo.AdUnitIdentifier + " Placement: " + adInfo.Placement);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 开屏广告功能
|
||||||
|
public void LoadSplash() { }
|
||||||
|
public bool IsSplashAvailable() { return false; }
|
||||||
|
public void DisplaySplash(){}
|
||||||
|
public double GetSplashRevenue() {return 0;}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 原生广告功能
|
||||||
|
public void LoadNative(){}
|
||||||
|
public double GetNativeRevenue() {return 0;}
|
||||||
|
public void DisplayNative(NativeAdPosition position){}
|
||||||
|
public bool IsNativeAvailable(){return false;}
|
||||||
|
public void RemoveNative(){}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 横幅广告功能
|
||||||
|
public void LoadBanner(){}
|
||||||
|
public bool IsBannerAvailable(){return false;}
|
||||||
|
public void HideBanner(){}
|
||||||
|
public double GetBannerRevenue(){return 0;}
|
||||||
|
public void DisplayBanner(){}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,29 +4,62 @@ using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Script.Utils
|
namespace Script.Utils
|
||||||
{
|
{
|
||||||
public static class TimerUtils
|
public class TimerUtils : MonoBehaviour
|
||||||
{
|
{
|
||||||
private static MonoBehaviour _coroutineRunner;
|
private static TimerUtils _instance;
|
||||||
|
|
||||||
public static void Initialize(MonoBehaviour runner)
|
public static void Initialize()
|
||||||
{
|
{
|
||||||
_coroutineRunner = runner;
|
if (_instance != null) return;
|
||||||
|
GameObject timerObject = new GameObject("TimerUtils");
|
||||||
|
timerObject.hideFlags = HideFlags.HideInHierarchy;
|
||||||
|
DontDestroyOnLoad(timerObject);
|
||||||
|
_instance = timerObject.AddComponent<TimerUtils>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void DelayExecute(float delay, Action action)
|
public static void DelayExecute(float delay, System.Action action)
|
||||||
{
|
{
|
||||||
if (_coroutineRunner != null)
|
if (_instance == null)
|
||||||
{
|
{
|
||||||
_coroutineRunner.StartCoroutine(DelayExecuteCoroutine(delay, action));
|
Debug.LogWarning("TimerUtils not initialized. Call TimerUtils.Initialize() first.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_instance.StartCoroutine(DelayExecuteCoroutine(delay, action));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void StopAllDelayedActions()
|
||||||
|
{
|
||||||
|
if (_instance != null)
|
||||||
|
{
|
||||||
|
_instance.StopAllCoroutines();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerator DelayExecuteCoroutine(float delay, Action action)
|
public static void Dispose()
|
||||||
|
{
|
||||||
|
if (_instance != null)
|
||||||
|
{
|
||||||
|
Destroy(_instance.gameObject);
|
||||||
|
_instance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerator DelayExecuteCoroutine(float delay, System.Action action)
|
||||||
{
|
{
|
||||||
yield return new WaitForSeconds(delay);
|
yield return new WaitForSeconds(delay);
|
||||||
action?.Invoke();
|
action?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OnDestroy()
|
||||||
|
{
|
||||||
|
// 清理静态引用,防止内存泄漏
|
||||||
|
if (_instance == this)
|
||||||
|
{
|
||||||
|
_instance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue