113 lines
4.2 KiB
C#
113 lines
4.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
using UnityEngine;
|
|
using Newtonsoft.Json;
|
|
|
|
public partial class AdManager
|
|
{
|
|
int retryAttemptReward;
|
|
|
|
private Action<int> _rewardCallback;
|
|
private bool _rewardOK = false;
|
|
|
|
public void InitializeRewardedAds()
|
|
{
|
|
// Attach callback
|
|
MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent;
|
|
MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailedEvent;
|
|
MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += OnRewardedAdDisplayedEvent;
|
|
MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent;
|
|
MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnRewardedAdRevenuePaidEvent;
|
|
MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdHiddenEvent;
|
|
MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent;
|
|
MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent;
|
|
|
|
// Load the first rewarded ad
|
|
LoadRewardedAd();
|
|
}
|
|
|
|
private void LoadRewardedAd()
|
|
{
|
|
MaxSdk.LoadRewardedAd(adRewardUnitId);
|
|
}
|
|
|
|
private void OnRewardedAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
|
|
{
|
|
// Rewarded ad is ready for you to show. MaxSdk.IsRewardedAdReady(adUnitId) now returns 'true'.
|
|
|
|
// Reset retry attempt
|
|
retryAttemptReward = 0;
|
|
}
|
|
|
|
private void OnRewardedAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
|
|
{
|
|
// Rewarded ad failed to load
|
|
// AppLovin recommends that you retry with exponentially higher delays, up to a maximum delay (in this case 64 seconds).
|
|
|
|
retryAttemptReward++;
|
|
double retryDelay = Math.Pow(2, Math.Min(6, retryAttemptReward));
|
|
|
|
Invoke("LoadRewardedAd", (float)retryDelay);
|
|
}
|
|
|
|
private void OnRewardedAdDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { }
|
|
|
|
private void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
|
|
{
|
|
// Rewarded ad failed to display. AppLovin recommends that you load the next ad.
|
|
LoadRewardedAd();
|
|
_rewardCallback?.Invoke(1);
|
|
}
|
|
|
|
private void OnRewardedAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { }
|
|
|
|
private void OnRewardedAdHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
|
|
{
|
|
// Rewarded ad is hidden. Pre-load the next ad
|
|
LoadRewardedAd();
|
|
if (_rewardOK)
|
|
{
|
|
_rewardCallback?.Invoke(0);
|
|
}
|
|
else{
|
|
_rewardCallback?.Invoke(1);
|
|
}
|
|
_rewardOK = false;
|
|
_rewardCallback = null;
|
|
}
|
|
|
|
private void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo)
|
|
{
|
|
_rewardOK = true;
|
|
}
|
|
|
|
private void OnRewardedAdRevenuePaidEvent(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", "reward");
|
|
var result = JsonConvert.SerializeObject(dict);
|
|
luaAdRevenuePaidEventCallback(result);
|
|
}
|
|
} |