namespace GleyMobileAds { using UnityEngine.Events; using System.Collections.Generic; using UnityEngine; #if USE_ADCOLONY using AdColony; using System.Linq; #endif public class CustomAdColony : MonoBehaviour, ICustomAds { #if USE_ADCOLONY private const float reloadTime = 30; private UnityAction OnCompleteMethod; private UnityAction OnCompleteMethodWithAdvertiser; private UnityAction OnInterstitialClosed; private UnityAction OnInterstitialClosedWithAdvertiser; private UnityAction DisplayResult; private AdColonyAdView bannerAd; private InterstitialAd interstitialAd; private InterstitialAd rewardedAd; private string appId; private string bannerZoneId; private string interstitialZoneId; private string rewardedZoneId; private readonly int maxRetryCount = 10; private int currentRetryRewardedVideo; private int currentRetryInterstitial; private bool debug; private bool bannerUsed; private BannerPosition position; private bool canShowBanner; /// /// Initializing AdColony /// /// 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; //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 appId = settings.appId.id; bannerZoneId = settings.idBanner.id; interstitialZoneId = settings.idInterstitial.id; rewardedZoneId = settings.idRewarded.id; //add listeners Ads.OnConfigurationCompleted += OnConfigurationCompleted; Ads.OnRequestInterstitial += OnRequestInterstitial; Ads.OnRequestInterstitialFailedWithZone += OnRequestInterstitialFailed; Ads.OnClosed += OnClosed; Ads.OnRewardGranted += OnRewardGranted; Ads.OnAdViewLoaded += BannerLoaded; Ads.OnAdViewFailedToLoad += BannerLoadFailed; //preparing AdColony SDK for initialization AppOptions appOptions = new AppOptions(); appOptions.SetPrivacyFrameworkRequired(AppOptions.GDPR, true); if (consent == UserConsent.Unset || consent == UserConsent.Accept) { appOptions.SetPrivacyConsentString(AppOptions.GDPR, "1"); } else { appOptions.SetPrivacyConsentString(AppOptions.GDPR, "0"); } appOptions.SetPrivacyFrameworkRequired(AppOptions.CCPA, true); if(ccpaConsent == UserConsent.Unset || consent == UserConsent.Accept) { appOptions.SetPrivacyConsentString(AppOptions.CCPA, "1"); } else { appOptions.SetPrivacyConsentString(AppOptions.CCPA, "0"); } if(settings.directedForChildren==true) { appOptions.SetPrivacyFrameworkRequired(AppOptions.COPPA, true); } List zoneIDs = new List(); if(!string.IsNullOrEmpty(bannerZoneId)) { zoneIDs.Add(bannerZoneId); } if(!string.IsNullOrEmpty(interstitialZoneId)) { zoneIDs.Add(interstitialZoneId); } if(!string.IsNullOrEmpty(rewardedZoneId)) { zoneIDs.Add(rewardedZoneId); } if(zoneIDs.Count==0) { Debug.LogError("Please add your IDs in SettingsWindow"); return; } //Apply configuration Ads.Configure(appId, appOptions, zoneIDs.ToArray()); //verify settings if (debug) { Debug.Log(this + " Initialize"); ScreenWriter.Write(this + " Initialize"); Debug.Log(this + " App ID: " + appId); ScreenWriter.Write(this + " App ID: " + appId); Debug.Log(this + " Banner Zone ID: " + bannerZoneId); ScreenWriter.Write(this + " Banner Zone ID: " + bannerZoneId); Debug.Log(this + " Interstitial Zone ID: " + interstitialZoneId); ScreenWriter.Write(this + " Interstitial Zone ID: " + interstitialZoneId); Debug.Log(this + " Rewarded Zone ID: " + rewardedZoneId); ScreenWriter.Write(this + " Rewarded Zone ID: " + rewardedZoneId); } } private void BannerLoadFailed(AdColonyAdView obj) { if (debug) { Debug.Log(this + " Banner Load Failed "); ScreenWriter.Write(this + " Banner Load Failed "); } if (DisplayResult != null) { DisplayResult(false, position, BannerType.Banner); DisplayResult = null; } } private void BannerLoaded(AdColonyAdView ad) { bannerAd = ad; if (canShowBanner) { //bannerAd.ShowAdView(); if (debug) { Debug.Log(this + " Banner Loaded"); ScreenWriter.Write(this + " Banner Loaded"); } if (DisplayResult != null) { DisplayResult(true, position, BannerType.Banner); DisplayResult = null; } } else { if (debug) { Debug.Log(this + " Banner closed before loading"); ScreenWriter.Write(this + " Banner closed before loading"); } if (DisplayResult != null) { DisplayResult(false, position, BannerType.Banner); DisplayResult = null; } bannerAd.DestroyAdView(); } } /// /// Updates consent at runtime /// /// the new consent public void UpdateConsent(UserConsent consent, UserConsent ccpaConsent) { AppOptions appOptions = Ads.GetAppOptions(); appOptions.SetPrivacyFrameworkRequired(AppOptions.GDPR, true); if (consent == UserConsent.Unset || consent == UserConsent.Accept) { appOptions.SetPrivacyConsentString(AppOptions.GDPR, "1"); } else { appOptions.SetPrivacyConsentString(AppOptions.GDPR, "0"); } appOptions.SetPrivacyFrameworkRequired(AppOptions.CCPA, true); if (ccpaConsent == UserConsent.Unset || consent == UserConsent.Accept) { appOptions.SetPrivacyConsentString(AppOptions.CCPA, "1"); } else { appOptions.SetPrivacyConsentString(AppOptions.CCPA, "0"); } if (debug) { Debug.Log(this + " Update consent to " + consent); ScreenWriter.Write(this + " Update consent to " + consent); } Ads.SetAppOptions(appOptions); } /// /// Check if AdColony interstitial is available /// /// true if an interstitial is available public bool IsInterstitialAvailable() { if (interstitialAd != null) { if (interstitialAd.ZoneId == interstitialZoneId) { if (interstitialAd.Expired == false) { return true; } } } return false; } /// /// Show AdColony interstitial /// /// callback called when user closes interstitial public void ShowInterstitial(UnityAction InterstitialClosed) { if (interstitialAd != null) { OnInterstitialClosed = InterstitialClosed; Ads.ShowAd(interstitialAd); } } /// /// Show AdColony interstitial /// /// callback called when user closes interstitial also returns advertiser public void ShowInterstitial(UnityAction InterstitialClosed) { if (interstitialAd != null) { OnInterstitialClosedWithAdvertiser = InterstitialClosed; Ads.ShowAd(interstitialAd); } } /// /// Check if AdColony rewarded video is available /// /// true if a rewarded video is available public bool IsRewardVideoAvailable() { if (rewardedAd != null) { if (rewardedAd.ZoneId == rewardedZoneId) { if (rewardedAd.Expired == false) { return true; } } } return false; } /// /// Show AdColony rewarded video /// /// callback called when user closes the rewarded video -> if true video was not skipped public void ShowRewardVideo(UnityAction CompleteMethod) { if (rewardedAd != null) { OnCompleteMethod = CompleteMethod; Ads.ShowAd(rewardedAd); } } /// /// Show AdColony rewarded video /// /// callback called when user closes the rewarded video -> if true video was not skipped public void ShowRewardVideo(UnityAction CompleteMethod) { if (rewardedAd != null) { OnCompleteMethodWithAdvertiser = CompleteMethod; Ads.ShowAd(rewardedAd); } } /// /// AdColony specific event triggered after initialization is done /// /// private void OnConfigurationCompleted(List zones_) { if (debug) { Debug.Log(this + " OnConfigurationCompleted called"); ScreenWriter.Write(this + " OnConfigurationCompleted called"); } if (zones_ == null || zones_.Count <= 0) { if (debug) { Debug.Log(this + " Configure Failed"); ScreenWriter.Write(this + " Configure Failed"); } } else { if (debug) { Debug.Log(this + " Configure Succeeded."); ScreenWriter.Write(this + " Configure Succeeded."); } RequestInterstitial(); RequestRewarded(); } } /// /// Request a banner /// private void RequestBanner(BannerPosition position) { bannerUsed = true; if (string.IsNullOrEmpty(bannerZoneId)) { return; } if (debug) { Debug.Log(this + " Request Banner"); ScreenWriter.Write(this + " Request banner"); } AdOptions adOptions = new AdOptions(); adOptions.ShowPrePopup = false; adOptions.ShowPostPopup = false; if (position == BannerPosition.BOTTOM) { Ads.RequestAdView(bannerZoneId, AdSize.Banner, AdPosition.Bottom, null); } else { Ads.RequestAdView(bannerZoneId, AdSize.Banner, AdPosition.Top, null); } } /// /// Request an interstitial /// private void RequestInterstitial() { if (string.IsNullOrEmpty(interstitialZoneId)) { return; } if (debug) { Debug.Log(this + " Request Interstitial"); ScreenWriter.Write(this + " Request Interstitial"); } AdOptions adOptions = new AdOptions(); adOptions.ShowPrePopup = false; adOptions.ShowPostPopup = false; Ads.RequestInterstitialAd(interstitialZoneId, adOptions); } /// /// Request a rewarded video /// private void RequestRewarded() { if (string.IsNullOrEmpty(rewardedZoneId)) { return; } if (debug) { Debug.Log(this + " Request Rewarded"); ScreenWriter.Write(this + " Request Rewarded"); } AdOptions adOptions = new AdOptions(); adOptions.ShowPrePopup = false; adOptions.ShowPostPopup = false; Ads.RequestInterstitialAd(rewardedZoneId, adOptions); } /// /// AdColony specific event triggered after a rewarded video is closed /// /// /// /// /// private void OnRewardGranted(string zoneId, bool success, string name, int amount) { if (zoneId == rewardedZoneId) { if (debug) { Debug.Log(this + string.Format(" OnRewardGranted called\n\tzoneId: {0}\n\tsuccess: {1}\n\tname: {2}\n\tamount: {3}", zoneId, success, name, amount)); ScreenWriter.Write(this + string.Format(" OnRewardGranted called\n\tzoneId: {0}\n\tsuccess: {1}\n\tname: {2}\n\tamount: {3}", zoneId, success, name, amount)); } if (success) { if (OnCompleteMethod != null) { OnCompleteMethod(true); OnCompleteMethod = null; } if (OnCompleteMethodWithAdvertiser != null) { OnCompleteMethodWithAdvertiser(true, SupportedAdvertisers.AdColony.ToString()); OnCompleteMethodWithAdvertiser = null; } } else { if (OnCompleteMethod != null) { OnCompleteMethod(false); OnCompleteMethod = null; } if (OnCompleteMethodWithAdvertiser != null) { OnCompleteMethodWithAdvertiser(false, SupportedAdvertisers.AdColony.ToString()); OnCompleteMethodWithAdvertiser = null; } } } } /// /// AdColony specific event triggered after an ad is closed /// /// private void OnClosed(InterstitialAd ad_) { if (debug) { Debug.Log(this + " OnClosed called, expired: " + ad_.Expired); ScreenWriter.Write(this + "OnClosed called, expired: " + ad_.Expired); } if (ad_.ZoneId == interstitialZoneId) { if (OnInterstitialClosed != null) { OnInterstitialClosed(); OnInterstitialClosed = null; } if (OnInterstitialClosedWithAdvertiser != null) { OnInterstitialClosedWithAdvertiser(SupportedAdvertisers.AdColony.ToString()); OnInterstitialClosedWithAdvertiser = null; } interstitialAd = ad_; if (interstitialAd.Expired) { interstitialAd = null; RequestInterstitial(); } } if (ad_.ZoneId == rewardedZoneId) { rewardedAd = ad_; if (rewardedAd.Expired) { rewardedAd = null; RequestRewarded(); } } } /// /// AdColony specific event triggered when an AdColony video failed to load /// /// private void OnRequestInterstitialFailed(string zoneID) { if (debug) { Debug.Log(this + " Load Ad Failed"); ScreenWriter.Write(this + " Load Ad Failed"); } if (zoneID == interstitialZoneId) { if (currentRetryInterstitial < maxRetryCount) { currentRetryInterstitial++; if (debug) { Debug.Log(this + " Interstitial Failed->Retry " + currentRetryInterstitial); ScreenWriter.Write(this + " Interstitial Failed->Retry " + currentRetryInterstitial); } Invoke("RequestInterstitial", reloadTime); } } if (zoneID == rewardedZoneId) { if (currentRetryRewardedVideo < maxRetryCount) { currentRetryRewardedVideo++; if (debug) { Debug.Log(this + " Rewarded Video Failed->Retry " + currentRetryRewardedVideo); ScreenWriter.Write(this + " Rewarded Video Failed->Retry " + currentRetryRewardedVideo); } Invoke("RequestRewarded", reloadTime); } } } /// /// AdColony specific event triggered when an ad was loaded /// /// private void OnRequestInterstitial(InterstitialAd ad_) { if (debug) { Debug.Log(this + " OnRequestInterstitial called id: " + ad_.ZoneId); ScreenWriter.Write(this + " OnRequestInterstitial called id: " + ad_.ZoneId); } if (ad_.ZoneId == interstitialZoneId) { currentRetryInterstitial = 0; interstitialAd = ad_; } if (ad_.ZoneId == rewardedZoneId) { currentRetryRewardedVideo = 0; rewardedAd = ad_; } } //AdColony does not support banner ads public bool IsBannerAvailable() { return true; } public void ShowBanner(BannerPosition position, BannerType type, UnityAction DisplayResult) { HideBanner(); this.DisplayResult = DisplayResult; this.position = position; canShowBanner = true; RequestBanner(position); } public void HideBanner() { canShowBanner = false; if(bannerAd!=null) { bannerAd.DestroyAdView(); } } public void ResetBannerUsage() { bannerUsed = false; } public bool BannerAlreadyUsed() { return bannerUsed; } private void OnApplicationFocus(bool focus) { if (focus == true) { if (IsInterstitialAvailable() == false) { if (currentRetryInterstitial == maxRetryCount) { RequestInterstitial(); } } if (IsRewardVideoAvailable() == false) { if (currentRetryRewardedVideo == maxRetryCount) { RequestRewarded(); } } } } #else //dummy interface implementation, used when AdColony is not enabled 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 bool BannerAlreadyUsed() { return false; } public void ShowBanner(BannerPosition position, BannerType type, UnityAction DisplayResult) { } public void ShowInterstitial(UnityAction InterstitialClosed = null) { } public void ShowInterstitial(UnityAction InterstitialClosed) { } public void ShowRewardVideo(UnityAction CompleteMethod) { } public void ShowRewardVideo(UnityAction CompleteMethod) { } public void UpdateConsent(UserConsent consent, UserConsent ccpaConsent) { } #endif } }