using System.Collections; using System.Collections.Generic; using System; using UnityEngine; using Newtonsoft.Json; public partial class AdManager { int retryAttemptReward; private Action _rewardCallback; private bool _rewardOK = false; public Action luaAdRevenuePaidEventCallback; 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(); } public void SetAdRevenuePaidEventCallback(Action callback) { luaAdRevenuePaidEventCallback = callback; } 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 retryAttemptInterstitial = 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). retryAttemptInterstitial++; double retryDelay = Math.Pow(2, Math.Min(6, retryAttemptInterstitial)); 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(); Debug.Log(Tag + "==================== OnRewardedAdFailedToDisplayEvent"); _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; Debug.Log(Tag + "==================== OnRewardedAdHiddenEvent"); } private void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo) { // The rewarded ad displayed and the user should receive the reward. Debug.Log(Tag + "Rewarded user: " + reward.Amount + " " + reward.Label); _rewardOK = true; // _rewardCallback?.Invoke(0); // _rewardCallback = null; } 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(); dict.Add("revenue", revenue.ToString()); 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); var result = JsonConvert.SerializeObject(dict); luaAdRevenuePaidEventCallback(result); } }