namespace GleyMobileAds { using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Events; public class CustomMoPub : MonoBehaviour, ICustomAds { #if USE_MOPUB private const float reloadTime = 30; private readonly int maxRetryCount = 10; private bool debug; private string bannerAdUnit; private string interstitialAdUnit; private string rewardedVideoAdUnit; private bool bannerUsed; private UnityAction DisplayResult; private BannerPosition position; private BannerType bannerType; private bool initialized; private UserConsent consent; private UserConsent ccpaConsent; private UnityAction OnInterstitialClosed; private UnityAction OnInterstitialClosedWithAdvertiser; private int currentRetryInterstitial; private int currentRetryRewardedVideo; private bool rewardedVideoAvailable; private UnityAction OnCompleteMethod; private UnityAction OnCompleteMethodWithAdvertiser; private bool rewardedWatched; #region Initialize /// /// Initializing MoPub /// /// user consent -> if true show personalized ads /// contains all required settings for this publisher public void InitializeAds(UserConsent consent, UserConsent ccpaConsent, List platformSettings) { debug = Advertisements.Instance.debug; if (initialized == false) { initialized = true; //get settings #if UNITY_ANDROID PlatformSettings settings = platformSettings.First(cond => cond.platform == SupportedPlatforms.Android); #endif #if UNITY_IOS PlatformSettings settings = platformSettings.First(cond => cond.platform == SupportedPlatforms.iOS); #endif //apply settings bannerAdUnit = settings.idBanner.id; interstitialAdUnit = settings.idInterstitial.id; rewardedVideoAdUnit = settings.idRewarded.id; this.consent = consent; this.ccpaConsent = ccpaConsent; //verify settings if (debug) { Debug.Log(this + " Initialize"); ScreenWriter.Write(this + " Initialize"); Debug.Log(this + " Banner ID: " + bannerAdUnit); ScreenWriter.Write(this + " Banner ID: " + bannerAdUnit); Debug.Log(this + " Interstitial ID: " + interstitialAdUnit); ScreenWriter.Write(this + " Interstitial ID: " + interstitialAdUnit); Debug.Log(this + " Rewarded ID: " + rewardedVideoAdUnit); ScreenWriter.Write(this + " Rewarded ID: " + rewardedVideoAdUnit); } MoPubManager.OnSdkInitializedEvent += SDKInitialized; string defaultID = null; if (string.IsNullOrEmpty(defaultID)) { defaultID = bannerAdUnit; } if (string.IsNullOrEmpty(defaultID)) { defaultID = interstitialAdUnit; } if (string.IsNullOrEmpty(defaultID)) { defaultID = rewardedVideoAdUnit; } if (!string.IsNullOrEmpty(defaultID)) { MoPub.InitializeSdk(defaultID); } } } /// /// Called after SDK was initialized /// /// ID used to initialize SDK private void SDKInitialized(string defaultID) { if (debug) { Debug.Log(this + " Initialized " + defaultID); ScreenWriter.Write(this + " Initialized " + defaultID); } UpdateConsent(consent, ccpaConsent); //prepare ad types if (!string.IsNullOrEmpty(bannerAdUnit)) { MoPubManager.OnAdLoadedEvent += OnAdLoadedEvent; MoPubManager.OnAdFailedEvent += OnAdFailedEvent; MoPub.LoadBannerPluginsForAdUnits(new string[] { bannerAdUnit }); } if (!string.IsNullOrEmpty(interstitialAdUnit)) { MoPubManager.OnInterstitialLoadedEvent += InterstitialLoaded; MoPubManager.OnInterstitialFailedEvent += InterstitialFailed; MoPubManager.OnInterstitialDismissedEvent += InterstitialClosed; MoPub.LoadInterstitialPluginsForAdUnits(new string[] { interstitialAdUnit }); LoadInterstitial(); } if (!string.IsNullOrEmpty(rewardedVideoAdUnit)) { MoPubManager.OnRewardedVideoLoadedEvent += RewardedVideoLoaded; MoPubManager.OnRewardedVideoFailedEvent += RewardedVideoFailed; MoPubManager.OnRewardedVideoFailedToPlayEvent += RewardedVideoFailed; MoPubManager.OnRewardedVideoClosedEvent += OnAdClosed; MoPubManager.OnRewardedVideoReceivedRewardEvent += RewardedVideoWatched; MoPub.LoadRewardedVideoPluginsForAdUnits(new string[] { rewardedVideoAdUnit }); LoadRewardedVideo(); } } /// /// Updates consent at runtime /// /// the new consent public void UpdateConsent(UserConsent consent, UserConsent ccpaConsent) { if (consent == UserConsent.Deny || ccpaConsent == UserConsent.Deny) { MoPub.PartnerApi.RevokeConsent(); } else { MoPub.PartnerApi.GrantConsent(); } if (debug) { Debug.Log(this + " Update consent to " + consent + " " + MoPub.CurrentConsentStatus); ScreenWriter.Write(this + " Update consent to " + consent + " " + MoPub.CurrentConsentStatus); } } #endregion #region Banners /// /// Show MoPub banner /// /// can be TOP or BOTTOM /// /// can be Banner or SmartBanner public void ShowBanner(BannerPosition position, BannerType bannerType, UnityAction DisplayResult) { bannerUsed = true; this.DisplayResult = DisplayResult; this.position = position; this.bannerType = bannerType; HideBanner(); MoPub.AdPosition bannerPosition; if (position == BannerPosition.BOTTOM) { bannerPosition = MoPub.AdPosition.BottomCenter; } else { bannerPosition = MoPub.AdPosition.TopCenter; } MoPub.MaxAdSize bannerSize; if (bannerType == BannerType.Banner) { bannerSize = MoPub.MaxAdSize.Width300Height50; } else { bannerSize = MoPub.MaxAdSize.ScreenWidthHeight50; } MoPub.RequestBanner(bannerAdUnit, bannerPosition, bannerSize); } /// /// Event triggered when banner failed to load /// /// loaded ID /// private void OnAdFailedEvent(string ID, string reason) { if (debug) { Debug.Log(this + " OnAdFailedEvent ID:" + ID + " Reason " + reason); ScreenWriter.Write(this + " OnAdFailedEvent ID:" + ID + " Reason " + reason); } if (DisplayResult != null) { DisplayResult(false, position, bannerType); DisplayResult = null; } } /// /// Event triggered when banner load is succesfull /// /// /// private void OnAdLoadedEvent(string ID, float height) { if (debug) { Debug.Log(this + " OnAdLoadedEvent " + ID + " " + height); ScreenWriter.Write(this + " OnAdLoadedEvent " + ID + " " + height); } if (DisplayResult != null) { DisplayResult(true, position, bannerType); DisplayResult = null; } } /// /// Used for mediation purpose /// /// true if current banner failed to load public bool BannerAlreadyUsed() { return bannerUsed; } /// /// Hides MoPub banner /// public void HideBanner() { MoPub.DestroyBanner(bannerAdUnit); } /// /// Check if MoPub banner is available /// /// true if a banner is available public bool IsBannerAvailable() { return true; } /// /// Used for mediation purpose /// public void ResetBannerUsage() { bannerUsed = false; } #endregion #region Interstitial /// /// Check if MoPub interstitial is available /// /// true if an interstitial is available public bool IsInterstitialAvailable() { return MoPub.IsInterstitialReady(interstitialAdUnit); } /// /// Show MoPub interstitial /// /// callback called when user closes interstitial public void ShowInterstitial(UnityAction InterstitialClosed) { if (IsInterstitialAvailable()) { OnInterstitialClosed = InterstitialClosed; MoPub.ShowInterstitialAd(interstitialAdUnit); } } /// /// Show MoPub interstitial /// /// callback called when user closes interstitial public void ShowInterstitial(UnityAction InterstitialClosed) { if (IsInterstitialAvailable()) { OnInterstitialClosedWithAdvertiser = InterstitialClosed; MoPub.ShowInterstitialAd(interstitialAdUnit); } } /// /// Event triggered when an interstitial is closed /// /// private void InterstitialClosed(string adUnitId) { if (debug) { Debug.Log(this + " Reload Interstitial"); ScreenWriter.Write(this + " Reload Interstitial"); } //reload interstitial LoadInterstitial(); //trigger complete event if (OnInterstitialClosed != null) { OnInterstitialClosed(); OnInterstitialClosed = null; } if (OnInterstitialClosedWithAdvertiser != null) { OnInterstitialClosedWithAdvertiser(SupportedAdvertisers.MoPub.ToString()); OnInterstitialClosedWithAdvertiser = null; } } /// /// Coroutine to reload an interstitial after a fail /// /// time to wait /// private IEnumerator ReloadInterstitial(float reloadTime) { yield return new WaitForSeconds(reloadTime); LoadInterstitial(); } /// /// Loads MoPub interstitial /// private void LoadInterstitial() { if (debug) { Debug.Log(this + " Start Loading Interstitial"); ScreenWriter.Write(this + " Start Loading Interstitial"); } MoPub.RequestInterstitialAd(interstitialAdUnit); } /// /// Event triggered when an interstitial failed to load /// /// /// private void InterstitialFailed(string ID, string message) { if (debug) { Debug.Log(this + " Interstitial Failed To Load " + message); ScreenWriter.Write(this + " Interstitial Failed To Load " + message); } //try again to load a rewarded video if (currentRetryInterstitial < maxRetryCount) { currentRetryInterstitial++; if (debug) { Debug.Log(this + " RETRY " + currentRetryInterstitial); ScreenWriter.Write(this + " RETRY " + currentRetryInterstitial); } StartCoroutine(ReloadInterstitial(reloadTime)); } } /// /// Event triggered when interstitial is loaded /// /// private void InterstitialLoaded(string adUnitId) { if (debug) { Debug.Log(this + " Interstitial Loaded"); ScreenWriter.Write(this + " Interstitial Loaded"); } currentRetryInterstitial = 0; } #endregion #region RewardedVideo /// /// Check if MoPub rewarded video is available /// /// true if a rewarded video is available public bool IsRewardVideoAvailable() { return rewardedVideoAvailable; } /// /// Show MoPub rewarded video /// /// callback called when user closes the rewarded video -> if true video was not skipped public void ShowRewardVideo(UnityAction CompleteMethod) { if (IsRewardVideoAvailable()) { OnCompleteMethod = CompleteMethod; MoPub.ShowRewardedVideo(rewardedVideoAdUnit); } } /// /// Show MoPub rewarded video /// /// callback called when user closes the rewarded video -> if true video was not skipped public void ShowRewardVideo(UnityAction CompleteMethod) { if (IsRewardVideoAvailable()) { OnCompleteMethodWithAdvertiser = CompleteMethod; MoPub.ShowRewardedVideo(rewardedVideoAdUnit); } } /// /// Loads a MoPub rewarded video /// private void LoadRewardedVideo() { if (debug) { Debug.Log(this + "Start Loading Rewarded Video"); ScreenWriter.Write(this + "Start Loading Rewarded Video"); } MoPub.RequestRewardedVideo(rewardedVideoAdUnit); } /// /// Triggered when a rewarded video is fully watched /// /// /// /// private void RewardedVideoWatched(string ID, string arg2, float arg3) { rewardedVideoAvailable = false; if (debug) { Debug.Log(this + " RewardedVideoWatched"); ScreenWriter.Write(this + " RewardedVideoWatched"); } rewardedWatched = true; } /// /// Triggered when a rewarded video is closed /// /// private void OnAdClosed(string ID) { rewardedVideoAvailable = false; if (debug) { Debug.Log(this + " OnAdClosed"); ScreenWriter.Write(this + " OnAdClosed"); } //reload LoadRewardedVideo(); if (OnCompleteMethod != null) { OnCompleteMethod(rewardedWatched); OnCompleteMethod = null; } if (OnCompleteMethodWithAdvertiser != null) { OnCompleteMethodWithAdvertiser(rewardedWatched, SupportedAdvertisers.MoPub.ToString()); OnCompleteMethodWithAdvertiser = null; } rewardedWatched = false; } /// /// Triggered when a rewarded video failed to load /// /// /// private void RewardedVideoFailed(string ID, string message) { rewardedVideoAvailable = false; if (debug) { Debug.Log(this + " Rewarded Video Failed " + message); ScreenWriter.Write(this + " Rewarded Video Failed " + message); } //try again to load a rewarded video if (currentRetryRewardedVideo < maxRetryCount) { currentRetryRewardedVideo++; if (debug) { Debug.Log("MoPub RETRY " + currentRetryRewardedVideo); ScreenWriter.Write("MoPub RETRY " + currentRetryRewardedVideo); } StartCoroutine(ReloadRewardedVideo(reloadTime)); } } /// /// Called to reload a video after a failed atempt /// /// time to wait /// private IEnumerator ReloadRewardedVideo(float reloadTime) { yield return new WaitForSeconds(reloadTime); LoadRewardedVideo(); } /// /// Called when a rewarded video was successfully loaded /// /// private void RewardedVideoLoaded(string ID) { rewardedVideoAvailable = true; if (debug) { Debug.Log(this + " Rewarded Video Loaded"); ScreenWriter.Write(this + " Rewarded Video Loaded"); } currentRetryRewardedVideo = 0; } #endregion private void OnApplicationFocus(bool focus) { if (focus == true) { if (IsInterstitialAvailable() == false) { if (currentRetryInterstitial == maxRetryCount) { LoadInterstitial(); } } if (IsRewardVideoAvailable() == false) { if (currentRetryRewardedVideo == maxRetryCount) { LoadRewardedVideo(); } } } } #else public bool BannerAlreadyUsed() { return false; } public void HideBanner() { } public void InitializeAds(UserConsent consent, UserConsent ccpaConsent, List platformSettings) { } public bool IsBannerAvailable() { return false; } public bool IsInterstitialAvailable() { return false; } public bool IsRewardVideoAvailable() { return false; } public void ResetBannerUsage() { } public void ShowBanner(BannerPosition position, BannerType bannerType, UnityAction DisplayResult) { } public void ShowInterstitial(UnityAction InterstitialClosed) { } public void ShowInterstitial(UnityAction InterstitialClosed) { } public void ShowRewardVideo(UnityAction CompleteMethod) { } public void ShowRewardVideo(UnityAction CompleteMethod) { } public void UpdateConsent(UserConsent consent, UserConsent ccpaConsent) { } #endif } }