c1_unity/Assets/Scripts/Common/SDK/AdManagerInterstitial.cs
2025-11-03 10:59:33 +08:00

100 lines
4.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System;
using Newtonsoft.Json;
public partial class AdManager
{
int retryAttemptInterstitial;
private Action<int> _interstitialAdCallback;
public void InitializeInterstitialAds()
{
// Attach callback
MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent;
MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialLoadFailedEvent;
MaxSdkCallbacks.Interstitial.OnAdDisplayedEvent += OnInterstitialDisplayedEvent;
MaxSdkCallbacks.Interstitial.OnAdClickedEvent += OnInterstitialClickedEvent;
MaxSdkCallbacks.Interstitial.OnAdRevenuePaidEvent += OnInterstitialAdRevenuePaidEvent;
MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialHiddenEvent;
MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += OnInterstitialAdFailedToDisplayEvent;
// Load the first interstitial
LoadInterstitial();
}
private void LoadInterstitial()
{
MaxSdk.LoadInterstitial(adInterstitialUnitId);
}
private void OnInterstitialLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
// Interstitial ad is ready for you to show. MaxSdk.IsInterstitialReady(adUnitId) now returns 'true'
// Reset retry attempt
retryAttemptInterstitial = 0;
}
private void OnInterstitialLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
{
// Interstitial ad failed to load
// AppLovin recommends that you retry with exponentially higher delays, up to a maximum delay (in this case 64 seconds)
retryAttemptInterstitial++;
double retryDelay = Math.Pow(2, Math.Min(6, retryAttemptInterstitial));
Invoke("LoadInterstitial", (float)retryDelay);
}
private void OnInterstitialDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
_interstitialAdCallback?.Invoke(0);
}
private void OnInterstitialAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
{
// Interstitial ad failed to display. AppLovin recommends that you load the next ad.
LoadInterstitial();
_interstitialAdCallback?.Invoke(1);
}
private void OnInterstitialClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { }
private void OnInterstitialHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
// Interstitial ad is hidden. Pre-load the next ad.
LoadInterstitial();
_interstitialAdCallback?.Invoke(1);
}
private void OnInterstitialAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
if (luaAdRevenuePaidEventCallback == null)
{
return;
}
double revenue = adInfo.Revenue;
// Miscellaneous data
string countryCode = MaxSdk.GetSdkConfiguration().CountryCode; // "US" for the United States, etc - Note: Do not confuse this with currency code which is "USD" in most cases!
string networkName = adInfo.NetworkName; // Display name of the network that showed the ad (e.g. "AdColony")
string adUnitIdentifier = adInfo.AdUnitIdentifier; // The MAX Ad Unit ID
string placement = adInfo.Placement; // The placement this ad's postbacks are tied to
string networkPlacement = adInfo.NetworkPlacement; // The placement ID from the network that showed the ad
string adFormat = adInfo.AdFormat;
var dict = new Dictionary<string, System.Object>();
dict.Add("revenue", revenue);
dict.Add("country_code", countryCode);
dict.Add("network_name", networkName);
dict.Add("ad_unit_Id", adUnitId);
dict.Add("ad_unit_identifier", adUnitIdentifier);
dict.Add("placement", placement);
dict.Add("network_placement", networkPlacement);
dict.Add("ad_format", adFormat);
dict.Add("ad_type", "interstitial");
var result = JsonConvert.SerializeObject(dict);
luaAdRevenuePaidEventCallback(result);
}
}