using System.Diagnostics; using System; using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using UnityEngine.Advertisements; public enum BannerPosition { TopLeft, TopCenter, TopRight, Centered, CenterLeft, CenterRight, BottomLeft, BottomCenter, BottomRight } public enum MBRPosition { TopLeft, TopCenter, TopRight, Centered, CenterLeft, CenterRight, BottomLeft, BottomCenter, BottomRight } public class AdManager : MonoBehaviour { public string MaxSdkKey = "Enter_SDK_Key_HERE"; public string InterstitialAdUnitId = "ENTER_INTER_AD_UNIT_ID_HERE"; public string RewardedAdUnitId = "ENTER_REWARD_AD_UNIT_ID_HERE"; public string RewardedInterstitialAdUnitId = "ENTER_REWARD_INTER_AD_UNIT_ID_HERE"; public string BannerAdUnitId = "ENTER_BANNER_AD_UNIT_ID_HERE"; public string MRecAdUnitId = "ENTER_MBR_AD_UNIT_ID_HERE"; [Header("Banner & MBR Settings")] public BannerPosition BannerPosition = BannerPosition.TopCenter; MaxSdkBase.BannerPosition position; public MBRPosition MBRPosition = MBRPosition.BottomCenter; MaxSdkBase.AdViewPosition MBRposition; public bool AutoShowBanner=true; private bool isBannerShowing; private bool isMRecShowing; private int interstitialRetryAttempt; private int rewardedRetryAttempt; private int rewardedInterstitialRetryAttempt; public float IntestetailAdbreakDelay; private bool ShowIntestetial=false; private float timecounter; public static AdManager _Instance; private static bool IsAdUnitArrayNullOrEmpty (ICollection adUnitArray) { return (adUnitArray == null || adUnitArray.Count == 0); } void Awake () { MakeSingleton (); } void MakeSingleton () { if (_Instance != null) { Destroy (this.gameObject); } else { _Instance = this; DontDestroyOnLoad (this.gameObject); } } void Start() { MaxSdkCallbacks.OnSdkInitializedEvent += sdkConfiguration => { // AppLovin SDK is initialized, configure and start loading ads. UnityEngine.Debug.Log("MAX SDK Initialized"); position = (MaxSdkBase.BannerPosition)BannerPosition; MBRposition = (MaxSdkBase.AdViewPosition)MBRPosition; InitializeInterstitialAds(); InitializeRewardedAds(); InitializeRewardedInterstitialAds(); InitializeBannerAds(); InitializeMRecAds(); }; MaxSdk.SetSdkKey(MaxSdkKey); MaxSdk.InitializeSdk(); } void Update(){ if(ShowIntestetial){ timecounter+=Time.unscaledDeltaTime; if(timecounter>=IntestetailAdbreakDelay){ ShowIntestetial=false; timecounter=0; ShowIntestetialafterdelay(); } } } #region Interstitial Ad Methods private void InitializeInterstitialAds() { // Attach callbacks MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent; MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialFailedEvent; MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += InterstitialFailedToDisplayEvent; MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialDismissedEvent; MaxSdkCallbacks.Interstitial.OnAdRevenuePaidEvent += OnInterstitialRevenuePaidEvent; // Load the first interstitial LoadInterstitial(); } void LoadInterstitial() { MaxSdk.LoadInterstitial(InterstitialAdUnitId); } public void ShowInterstitial() { if (MaxSdk.IsInterstitialReady(InterstitialAdUnitId)) { this.gameObject.transform.GetChild(0).gameObject.SetActive(true); ShowIntestetial=true; } } void ShowIntestetialafterdelay(){ this.gameObject.transform.GetChild(0).gameObject.SetActive(false); MaxSdk.ShowInterstitial(InterstitialAdUnitId); } private void OnInterstitialLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Interstitial ad is ready to be shown. MaxSdk.IsInterstitialReady(interstitialAdUnitId) will now return 'true' // Reset retry attempt interstitialRetryAttempt = 0; } private void OnInterstitialFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) { // Interstitial ad failed to load. We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds). interstitialRetryAttempt++; double retryDelay = Math.Pow(2, Math.Min(6, interstitialRetryAttempt)); UnityEngine.Debug.Log("Interstitial failed to load with error code: " + errorInfo.Code); Invoke("LoadInterstitial", (float) retryDelay); } private void InterstitialFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo) { // Interstitial ad failed to display. We recommend loading the next ad UnityEngine.Debug.Log("Interstitial failed to display with error code: " + errorInfo.Code); LoadInterstitial(); } private void OnInterstitialDismissedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Interstitial ad is hidden. Pre-load the next ad UnityEngine.Debug.Log("Interstitial dismissed"); LoadInterstitial(); } private void OnInterstitialRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Interstitial ad revenue paid. Use this callback to track user revenue. UnityEngine.Debug.Log("Interstitial revenue paid"); // Ad revenue 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 } #endregion #region Rewarded Ad Methods private void InitializeRewardedAds() { // Attach callbacks MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent; MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdFailedEvent; MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent; MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += OnRewardedAdDisplayedEvent; MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent; MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdDismissedEvent; MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent; MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnRewardedAdRevenuePaidEvent; // Load the first RewardedAd LoadRewardedAd(); } private void LoadRewardedAd() { MaxSdk.LoadRewardedAd(RewardedAdUnitId); } public void showRewardedVideo() { if (MaxSdk.IsRewardedAdReady(RewardedAdUnitId)) { MaxSdk.ShowRewardedAd(RewardedAdUnitId); }else if (MaxSdk.IsRewardedInterstitialAdReady(RewardedInterstitialAdUnitId)) { MaxSdk.ShowRewardedInterstitialAd(RewardedInterstitialAdUnitId); } } private void OnRewardedAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Rewarded ad is ready to be shown. MaxSdk.IsRewardedAdReady(rewardedAdUnitId) will now return 'true' UnityEngine.Debug.Log("Rewarded ad loaded"); // Reset retry attempt rewardedRetryAttempt = 0; } private void OnRewardedAdFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) { // Rewarded ad failed to load. We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds). rewardedRetryAttempt++; double retryDelay = Math.Pow(2, Math.Min(6, rewardedRetryAttempt)); PlayerPrefs.SetInt ("reward", 2); UnityEngine.Debug.Log("Rewarded ad failed to load with error code: " + errorInfo.Code); Invoke("LoadRewardedAd", (float) retryDelay); } private void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo) { // Rewarded ad failed to display. We recommend loading the next ad PlayerPrefs.SetInt ("reward", 2); UnityEngine.Debug.Log("Rewarded ad failed to display with error code: " + errorInfo.Code); LoadRewardedAd(); } private void OnRewardedAdDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { UnityEngine.Debug.Log("Rewarded ad displayed"); } private void OnRewardedAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { UnityEngine.Debug.Log("Rewarded ad clicked"); } private void OnRewardedAdDismissedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Rewarded ad is hidden. Pre-load the next ad UnityEngine.Debug.Log("Rewarded ad dismissed"); LoadRewardedAd(); } private void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo) { PlayerPrefs.SetInt ("reward", 1); // Rewarded ad was displayed and user should receive the reward UnityEngine.Debug.Log("Rewarded ad received reward"); } private void OnRewardedAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Rewarded ad revenue paid. Use this callback to track user revenue. UnityEngine.Debug.Log("Rewarded ad revenue paid"); // Ad revenue 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 } #endregion #region Rewarded Interstitial Ad Methods private void InitializeRewardedInterstitialAds() { // Attach callbacks MaxSdkCallbacks.RewardedInterstitial.OnAdLoadedEvent += OnRewardedInterstitialAdLoadedEvent; MaxSdkCallbacks.RewardedInterstitial.OnAdLoadFailedEvent += OnRewardedInterstitialAdFailedEvent; MaxSdkCallbacks.RewardedInterstitial.OnAdDisplayFailedEvent += OnRewardedInterstitialAdFailedToDisplayEvent; MaxSdkCallbacks.RewardedInterstitial.OnAdDisplayedEvent += OnRewardedInterstitialAdDisplayedEvent; MaxSdkCallbacks.RewardedInterstitial.OnAdClickedEvent += OnRewardedInterstitialAdClickedEvent; MaxSdkCallbacks.RewardedInterstitial.OnAdHiddenEvent += OnRewardedInterstitialAdDismissedEvent; MaxSdkCallbacks.RewardedInterstitial.OnAdReceivedRewardEvent += OnRewardedInterstitialAdReceivedRewardEvent; MaxSdkCallbacks.RewardedInterstitial.OnAdRevenuePaidEvent += OnRewardedInterstitialAdRevenuePaidEvent; // Load the first RewardedInterstitialAd LoadRewardedInterstitialAd(); } private void LoadRewardedInterstitialAd() { MaxSdk.LoadRewardedInterstitialAd(RewardedInterstitialAdUnitId); } private void ShowRewardedInterstitialAd() { if (MaxSdk.IsRewardedInterstitialAdReady(RewardedInterstitialAdUnitId)) { MaxSdk.ShowRewardedInterstitialAd(RewardedInterstitialAdUnitId); } } private void OnRewardedInterstitialAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Rewarded interstitial ad is ready to be shown. MaxSdk.IsRewardedInterstitialAdReady(rewardedInterstitialAdUnitId) will now return 'true' UnityEngine.Debug.Log("Rewarded interstitial ad loaded"); // Reset retry attempt rewardedInterstitialRetryAttempt = 0; } private void OnRewardedInterstitialAdFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) { PlayerPrefs.SetInt ("reward", 2); // Rewarded interstitial ad failed to load. We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds). rewardedInterstitialRetryAttempt++; double retryDelay = Math.Pow(2, Math.Min(6, rewardedInterstitialRetryAttempt)); UnityEngine.Debug.Log("Rewarded interstitial ad failed to load with error code: " + errorInfo.Code); Invoke("LoadRewardedInterstitialAd", (float) retryDelay); } private void OnRewardedInterstitialAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo) { PlayerPrefs.SetInt ("reward", 2); // Rewarded interstitial ad failed to display. We recommend loading the next ad UnityEngine.Debug.Log("Rewarded interstitial ad failed to display with error code: " + errorInfo.Code); LoadRewardedInterstitialAd(); } private void OnRewardedInterstitialAdDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { UnityEngine.Debug.Log("Rewarded interstitial ad displayed"); } private void OnRewardedInterstitialAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { UnityEngine.Debug.Log("Rewarded interstitial ad clicked"); } private void OnRewardedInterstitialAdDismissedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Rewarded interstitial ad is hidden. Pre-load the next ad UnityEngine.Debug.Log("Rewarded interstitial ad dismissed"); LoadRewardedInterstitialAd(); } private void OnRewardedInterstitialAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo) { PlayerPrefs.SetInt ("reward", 1); // Rewarded interstitial ad was displayed and user should receive the reward UnityEngine.Debug.Log("Rewarded interstitial ad received reward"); } private void OnRewardedInterstitialAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Rewarded interstitial ad revenue paid. Use this callback to track user revenue. UnityEngine.Debug.Log("Rewarded interstitial ad revenue paid"); // Ad revenue 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 } #endregion #region Banner Ad Methods private void InitializeBannerAds() { // Attach Callbacks MaxSdkCallbacks.Banner.OnAdLoadedEvent += OnBannerAdLoadedEvent; MaxSdkCallbacks.Banner.OnAdLoadFailedEvent += OnBannerAdFailedEvent; MaxSdkCallbacks.Banner.OnAdClickedEvent += OnBannerAdClickedEvent; MaxSdkCallbacks.Banner.OnAdRevenuePaidEvent += OnBannerAdRevenuePaidEvent; // 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(BannerAdUnitId, position); // Set background or background color for banners to be fully functional. MaxSdk.SetBannerBackgroundColor(BannerAdUnitId, Color.black); } public void ShowBanner () { MaxSdk.ShowBanner(BannerAdUnitId); } public void HideBanner (){ MaxSdk.HideBanner(BannerAdUnitId); } private void OnBannerAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { if(AutoShowBanner) ShowBanner(); // Banner ad is ready to be shown. // If you have already called MaxSdk.ShowBanner(BannerAdUnitId) it will automatically be shown on the next ad refresh. UnityEngine.Debug.Log("Banner ad loaded"); } private void OnBannerAdFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) { // Banner ad failed to load. MAX will automatically try loading a new ad internally. UnityEngine.Debug.Log("Banner ad failed to load with error code: " + errorInfo.Code); } private void OnBannerAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { UnityEngine.Debug.Log("Banner ad clicked"); } private void OnBannerAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // Banner ad revenue paid. Use this callback to track user revenue. UnityEngine.Debug.Log("Banner ad revenue paid"); // Ad revenue 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 } #endregion #region MREC Ad Methods private void InitializeMRecAds() { // Attach Callbacks MaxSdkCallbacks.MRec.OnAdLoadedEvent += OnMRecAdLoadedEvent; MaxSdkCallbacks.MRec.OnAdLoadFailedEvent += OnMRecAdFailedEvent; MaxSdkCallbacks.MRec.OnAdClickedEvent += OnMRecAdClickedEvent; MaxSdkCallbacks.MRec.OnAdRevenuePaidEvent += OnMRecAdRevenuePaidEvent; // MRECs are automatically sized to 300x250. MaxSdk.CreateMRec(MRecAdUnitId, MBRposition); } public void ShowMRec() { MaxSdk.ShowMRec(MRecAdUnitId);; } public void HideMRec(){ MaxSdk.HideMRec(MRecAdUnitId); } private void OnMRecAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // MRec ad is ready to be shown. // If you have already called MaxSdk.ShowMRec(MRecAdUnitId) it will automatically be shown on the next MRec refresh. UnityEngine.Debug.Log("MRec ad loaded"); } private void OnMRecAdFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) { // MRec ad failed to load. MAX will automatically try loading a new ad internally. UnityEngine.Debug.Log("MRec ad failed to load with error code: " + errorInfo.Code); } private void OnMRecAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { UnityEngine.Debug.Log("MRec ad clicked"); } private void OnMRecAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // MRec ad revenue paid. Use this callback to track user revenue. UnityEngine.Debug.Log("MRec ad revenue paid"); // Ad revenue 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 } #endregion public void MediationDebugger() { MaxSdk.ShowMediationDebugger(); } }