using System; using Color = UnityEngine.Color; namespace RND.Mediation { public class MaxSDK : IAdsSDK { public string BannerUnit => Settings.ADS_BANNER_UNIT; public string InterstitialUnit => Settings.ADS_INTERSTITIAL_UNIT; public string RewardUnit => Settings.ADS_REWARD_UNIT; public string SdkKey => Settings.ADS_MEDIATION_KEY; private int _interRetryAttempt = 0; private int _rewardRetryAttempt = 0; public void Init(Action callback) { #if RND_MAX MaxSdk.SetSdkKey(SdkKey); MaxSdk.InitializeSdk(); MaxSdkCallbacks.OnSdkInitializedEvent += sdkConfiguration => { #if GAME_IS_DEV MaxSdk.ShowMediationDebugger(); #endif InitializeBannerAds(); InitializeInterstitialAds(); InitializeRewardedAds(); callback?.Invoke(); }; #endif } private void InitializeBannerAds() { #if RND_MAX // Banners are automatically sized to 320x50 on phones and 728x90 on tablets // You may use the utility method `MaxSdkUtils.isTablet()` to help with view sizing adjustments MaxSdk.CreateBanner(BannerUnit, MaxSdkBase.BannerPosition.BottomCenter); // Set background or background color for banners to be fully functional MaxSdk.SetBannerBackgroundColor(BannerUnit, Color.black); ShowBanner(); #endif } public bool HasRewardVideo() { #if RND_MAX return MaxSdk.IsRewardedAdReady(RewardUnit); #else return false; #endif } public bool HasInterstitialVideo() { #if RND_MAX return MaxSdk.IsInterstitialReady(InterstitialUnit); #else return false; #endif } public void ShowBanner() { #if RND_MAX MaxSdk.ShowBanner(BannerUnit); #endif } private void HideBanner() { #if RND_MAX MaxSdk.HideBanner(BannerUnit); #endif } public void ShowInterstitial() { #if RND_MAX if (MaxSdk.IsInterstitialReady(InterstitialUnit)) MaxSdk.ShowInterstitial(InterstitialUnit); Log.Info("MaxSDK: Interstitial"); #endif } public void ShowReward() { #if RND_MAX if (MaxSdk.IsRewardedAdReady(RewardUnit)) MaxSdk.ShowRewardedAd(RewardUnit); #endif } private void InitializeInterstitialAds() { #if RND_MAX // Attach callback MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent; MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialFailedEvent; MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += InterstitialFailedToDisplayEvent; MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialDismissedEvent; // Load the first interstitial LoadInterstitial(); #endif } #if RND_MAX private void OnInterstitialLoadedEvent(string adUnitId, MaxSdkBase.AdInfo info) { // Interstitial ad is ready to be shown. MaxSdk.IsInterstitialReady(adUnitId) will now return 'true' // Reset retry attempt _interRetryAttempt = 0; } private void OnInterstitialFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo error) { // Interstitial ad failed to load // We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds) double retryDelay = Math.Pow(2, Math.Min(6, ++_interRetryAttempt)); G.Instance.Invoke(nameof(LoadInterstitial), (float)retryDelay); } private void InterstitialFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo error, MaxSdkBase.AdInfo info) { Ads.OnInterstitialFailedEvent?.Invoke(); // Interstitial ad failed to display. We recommend loading the next ad LoadInterstitial(); } private void OnInterstitialDismissedEvent(string adUnitId, MaxSdkBase.AdInfo info) { Ads.OnInterstitialFailedEvent?.Invoke(); // Interstitial ad is hidden. Pre-load the next ad LoadInterstitial(); } #endif #if RND_MAX private void LoadInterstitial() { MaxSdk.LoadInterstitial(InterstitialUnit); } #endif private void InitializeRewardedAds() { #if RND_MAX // Attach callback MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent; MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdFailedEvent; MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent; MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent; MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdDismissedEvent; MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent; // Load the first rewarded ad LoadRewardedAd(); #endif } #if RND_MAX private void LoadRewardedAd() { MaxSdk.LoadRewardedAd(RewardUnit); } private void OnRewardedAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo info) { // Rewarded ad is ready to be shown. MaxSdk.IsRewardedAdReady(adUnitId) will now return 'true' // Reset retry attempt _rewardRetryAttempt = 0; } private void OnRewardedAdFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo error) { // Rewarded ad failed to load // We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds) var retryDelay = Math.Pow(2, Math.Min(6, ++_rewardRetryAttempt)); G.Instance.Invoke(nameof(LoadRewardedAd), (float)retryDelay); Ads.RewardFailed?.Invoke(); } private void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo error, MaxSdkBase.AdInfo info) { // Rewarded ad failed to display. We recommend loading the next ad LoadRewardedAd(); Ads.RewardFailed?.Invoke(); } private static void OnRewardedAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo info) { } private void OnRewardedAdDismissedEvent(string adUnitId, MaxSdkBase.AdInfo info) { // Rewarded ad is hidden. Pre-load the next ad LoadRewardedAd(); } private static void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdkBase.Reward reward, MaxSdkBase.AdInfo info) { // Rewarded ad was displayed and user should receive the reward Ads.RewardReceived?.Invoke(); } #endif } }