using GleyMobileAds; using System; using System.Collections; /// /// Version 1.6.0 /// /// For any questions contact us at: /// gley.mobi@gmail.com /// or forum /// https://forum.unity.com/threads/mobile-ads-simple-way-to-integrate-ads-in-your-app.529292/ /// /// using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Events; //each advertiser will be setup using this class public class Advertiser { public ICustomAds advertiserScript; public SupportedAdvertisers advertiser; public MediationSettings mediationSettings; public List platformSettings; public Advertiser(ICustomAds advertiserScript, MediationSettings mediationSettings, List platformSettings) { this.advertiserScript = advertiserScript; this.mediationSettings = mediationSettings; this.platformSettings = platformSettings; advertiser = mediationSettings.advertiser; } } public enum SupportedAdTypes { None, Banner, Interstitial, Rewarded } public enum UserConsent { Unset = 0, Accept = 1, Deny = 2 } public class Advertisements : MonoBehaviour { //name of the PlayerPrefs key to save consent and show ads status private const string userConsent = "UserConsent"; private const string ccpaConsent = "CcpaConsent"; private const string removeAds = "RemoveAds"; //static instance of the class to be used anywhere in code private static Advertisements instance; public static Advertisements Instance { get { if (instance == null) { go = new GameObject(); go.name = "MobieAdsScripts"; DontDestroyOnLoad(go); instance = go.AddComponent(); } return instance; } } public static bool initialized; private static GameObject go; //independent lists for each advertiser private List allAdvertisers = new List(); private List bannerAdvertisers = new List(); private List interstitialAdvertisers = new List(); private List rewardedAdvertisers = new List(); //currently active mediation settings for each type of ad private SupportedMediation bannerMediation; private SupportedMediation interstitialMediation; private SupportedMediation rewardedMediation; private bool isBannerOnScreen; private bool hideBanners; //if true displays debug messages, enabled in Settings Window internal bool debug; //stores plugin all settings internal AdSettings adSettings; /// /// Used to set user consent that will be later forwarded to each advertiser SDK /// Should be set before initializing the SDK /// /// if true -> show personalized ads, if false show unpersonalized ads public void SetUserConsent(bool accept) { if (accept == true) { PlayerPrefs.SetInt(userConsent, (int)UserConsent.Accept); } else { PlayerPrefs.SetInt(userConsent, (int)UserConsent.Deny); } if (initialized == true) { UpdateUserConsent(); } } /// /// Used to set user consent that will be later forwarded to each advertiser SDK /// Should be set before initializing the SDK /// /// if true -> show personalized ads, if false show unpersonalized ads public void SetCCPAConsent(bool accept) { if (accept == true) { PlayerPrefs.SetInt(ccpaConsent, (int)UserConsent.Accept); } else { PlayerPrefs.SetInt(ccpaConsent, (int)UserConsent.Deny); } if (initialized == true) { UpdateUserConsent(); } } /// /// Get the previously set user consent, returns Unset if no consent was already saved /// /// private UserConsent GetConsent(string fileName) { if (!ConsentWasSet(fileName)) return UserConsent.Unset; return (UserConsent)PlayerPrefs.GetInt(fileName); } private bool ConsentWasSet(string fileName) { return PlayerPrefs.HasKey(fileName); } /// /// Get the previously set user consent, returns Unset if no consent was already saved /// /// Unset/Accept/Deny public UserConsent GetUserConsent() { return GetConsent(userConsent); } /// /// Get the previously set CCPA consent, returns Unset if no consent was already saved /// /// Unset/Accept/Deny public UserConsent GetCCPAConsent() { return GetConsent(ccpaConsent); } /// /// Returns true if user consent was already saved, false if not /// /// public bool UserConsentWasSet() { return PlayerPrefs.HasKey(userConsent); } public bool CCPAConsentWasSet() { return PlayerPrefs.HasKey(ccpaConsent); } /// /// automatically disables banner and interstitial ads /// /// if true, no banner and interstitials will be shown in your app public void RemoveAds(bool remove) { if (remove == true) { PlayerPrefs.SetInt(removeAds, 1); //if banner is active and user bought remove ads the banner will automatically hide HideBanner(); } else { PlayerPrefs.SetInt(removeAds, 0); } } /// /// check if ads are not disabled by user /// /// true if ads should be displayed public bool CanShowAds() { if (!PlayerPrefs.HasKey(removeAds)) { return true; } else { if (PlayerPrefs.GetInt(removeAds) == 0) { return true; } } return false; } /// /// Initializes all the advertisers from the plugin /// Should be called only once at the beginning of your app /// public void Initialize() { if (initialized == false) { initialized = true; #if UNITY_IOS #if USE_ATT //App Tracking Transparency var status = Unity.Advertisement.IosSupport.ATTrackingStatusBinding.GetAuthorizationTrackingStatus(); if (status == Unity.Advertisement.IosSupport.ATTrackingStatusBinding.AuthorizationTrackingStatus.NOT_DETERMINED) { Unity.Advertisement.IosSupport.ATTrackingStatusBinding.RequestAuthorizationTracking(); } #endif #endif StartCoroutine(WaitForConsent(ContinueInitialization)); } } private IEnumerator WaitForConsent(UnityAction Continue) { #if UNITY_IOS && !UNITY_EDITOR #if USE_ATT Version ver = Version.Parse(UnityEngine.iOS.Device.systemVersion); if (ver.Major >= 14) { var status = Unity.Advertisement.IosSupport.ATTrackingStatusBinding.GetAuthorizationTrackingStatus(); while (status == Unity.Advertisement.IosSupport.ATTrackingStatusBinding.AuthorizationTrackingStatus.NOT_DETERMINED) { status = Unity.Advertisement.IosSupport.ATTrackingStatusBinding.GetAuthorizationTrackingStatus(); yield return null; } switch (status) { case Unity.Advertisement.IosSupport.ATTrackingStatusBinding.AuthorizationTrackingStatus.AUTHORIZED: SetUserConsent(true); break; case Unity.Advertisement.IosSupport.ATTrackingStatusBinding.AuthorizationTrackingStatus.DENIED: SetUserConsent(false); break; default: SetUserConsent(true); break; } } #endif #endif if (Continue!=null) { Continue(); } yield return null; } private void ContinueInitialization() { adSettings = Resources.Load("AdSettingsData"); if (adSettings == null) { Debug.LogError("Gley Ads Plugin is not properly configured. Go to Window->Gley->Ads to set up the plugin. See the documentation"); return; } bannerMediation = adSettings.bannerMediation; interstitialMediation = adSettings.interstitialMediation; rewardedMediation = adSettings.rewardedMediation; debug = adSettings.debugMode; initialized = true; AdvertiserSettings currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.Admob); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.Admob), adSettings.GetPlaftormSettings(SupportedAdvertisers.Admob))); } } currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.Vungle); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.Vungle), adSettings.GetPlaftormSettings(SupportedAdvertisers.Vungle))); } } currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.AdColony); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.AdColony), adSettings.GetPlaftormSettings(SupportedAdvertisers.AdColony))); } } currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.Chartboost); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.Chartboost), adSettings.GetPlaftormSettings(SupportedAdvertisers.Chartboost))); } } currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.Unity); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.Unity), adSettings.GetPlaftormSettings(SupportedAdvertisers.Unity))); } } currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.Heyzap); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.Heyzap), adSettings.GetPlaftormSettings(SupportedAdvertisers.Heyzap))); } } currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.AppLovin); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.AppLovin), adSettings.GetPlaftormSettings(SupportedAdvertisers.AppLovin))); } } currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.Facebook); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.Facebook), adSettings.GetPlaftormSettings(SupportedAdvertisers.Facebook))); } } currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.MoPub); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.MoPub), adSettings.GetPlaftormSettings(SupportedAdvertisers.MoPub))); } } currentAdvertiser = adSettings.advertiserSettings.FirstOrDefault(cond => cond.advertiser == SupportedAdvertisers.IronSource); if (currentAdvertiser != null) { if (currentAdvertiser.useSDK) { allAdvertisers.Add(new Advertiser(go.AddComponent(), adSettings.GetAdvertiserSettings(SupportedAdvertisers.IronSource), adSettings.GetPlaftormSettings(SupportedAdvertisers.IronSource))); } } if (debug) { ScreenWriter.Write("User GDPR consent is set to: " + GetConsent(userConsent)); ScreenWriter.Write("User CCPA consent is set to: " + GetConsent(ccpaConsent)); } for (int i = 0; i < allAdvertisers.Count; i++) { allAdvertisers[i].advertiserScript.InitializeAds(GetConsent(userConsent), GetConsent(ccpaConsent), allAdvertisers[i].platformSettings); } ApplySettings(); LoadFile(); } /// /// Used to reinitialize the consent when it is changed from app settings /// reinitializes all supported advertiser SDK with new consent /// private void UpdateUserConsent() { for (int i = 0; i < allAdvertisers.Count; i++) { allAdvertisers[i].advertiserScript.UpdateConsent(GetConsent(userConsent), GetConsent(ccpaConsent)); } } /// /// Displays an interstitial video based on your mediation settings /// /// callback triggered when interstitial video is closed public void ShowInterstitial(UnityAction InterstitialClosed = null) { //if ads are disabled by user -> do nothing if (CanShowAds() == false) { return; } ICustomAds selectedAdvertiser = GetInterstitialAdvertiser(); if (selectedAdvertiser != null) { if (debug) { Debug.Log("Interstitial loaded from " + selectedAdvertiser); ScreenWriter.Write("Interstitial loaded from " + selectedAdvertiser); } selectedAdvertiser.ShowInterstitial(InterstitialClosed); } } /// /// Displays an interstitial video based on your mediation settings /// /// callback triggered when interstitial video is closed also returns the publisher public void ShowInterstitial(UnityAction InterstitialClosed) { //if ads are disabled by user -> do nothing if (CanShowAds() == false) { return; } ICustomAds selectedAdvertiser = GetInterstitialAdvertiser(); if (selectedAdvertiser != null) { if (debug) { Debug.Log("Interstitial loaded from " + selectedAdvertiser); ScreenWriter.Write("Interstitial loaded from " + selectedAdvertiser); } selectedAdvertiser.ShowInterstitial(InterstitialClosed); } } /// /// Displays an interstitial from the requested advertiser, if the requested advertiser is not available, another interstitial will be displayed based on your mediation settings /// /// advertiser from which ad will be displayed if available /// callback triggered when interstitial video is closed public void ShowInterstitial(SupportedAdvertisers advertiser, UnityAction InterstitialClosed = null) { //if ads are disabled by user -> do nothing if (CanShowAds() == false) { return; } Advertiser selected = GetInterstitialAdvertisers().First(cond => cond.advertiser == advertiser); if (selected.advertiserScript.IsInterstitialAvailable()) { if (debug) { Debug.Log("Interstitial from " + advertiser + " is available"); ScreenWriter.Write("Interstitial from " + advertiser + " is available"); } selected.advertiserScript.ShowInterstitial(InterstitialClosed); } else { if (debug) { Debug.Log("Interstitial from " + advertiser + " is NOT available"); ScreenWriter.Write("Interstitial from " + advertiser + " is NOT available"); } ShowInterstitial(InterstitialClosed); } } /// /// Get one available advertiser based on mediation settings /// /// selected advertiser private ICustomAds GetInterstitialAdvertiser() { if (interstitialMediation == SupportedMediation.OrderMediation) { return UseOrder(interstitialAdvertisers, SupportedAdTypes.Interstitial); } else { return UsePercent(interstitialAdvertisers, SupportedAdTypes.Interstitial); } } /// /// Displays a rewarded video based on your mediation settings /// /// callback triggered when video reward finished - if bool param is true => video was not skipped public void ShowRewardedVideo(UnityAction CompleteMethod) { ICustomAds selectedAdvertiser = null; if (rewardedMediation == SupportedMediation.OrderMediation) { selectedAdvertiser = UseOrder(rewardedAdvertisers, SupportedAdTypes.Rewarded); } else { selectedAdvertiser = UsePercent(rewardedAdvertisers, SupportedAdTypes.Rewarded); } if (selectedAdvertiser != null) { if (debug) { Debug.Log("Rewarded video loaded from " + selectedAdvertiser); ScreenWriter.Write("Rewarded video loaded from " + selectedAdvertiser); } selectedAdvertiser.ShowRewardVideo(CompleteMethod); } } /// /// Displays a rewarded video based on your mediation settings /// /// callback triggered when video reward finished - if bool param is true => video was not skipped, also the advertiser name is sent to callback method public void ShowRewardedVideo(UnityAction CompleteMethod) { ICustomAds selectedAdvertiser = null; if (rewardedMediation == SupportedMediation.OrderMediation) { selectedAdvertiser = UseOrder(rewardedAdvertisers, SupportedAdTypes.Rewarded); } else { selectedAdvertiser = UsePercent(rewardedAdvertisers, SupportedAdTypes.Rewarded); } if (selectedAdvertiser != null) { if (debug) { Debug.Log("Rewarded video loaded from " + selectedAdvertiser); ScreenWriter.Write("Rewarded video loaded from " + selectedAdvertiser); } selectedAdvertiser.ShowRewardVideo(CompleteMethod); } } /// /// Displays a rewarded video based on advertiser sent as parameter, if the requested advertiser is not available selected mediation settings are used /// /// the advertiser from which you want to display the rewarded video /// callback triggered when video reward finished - if bool param is true => video was not skipped public void ShowRewardedVideo(SupportedAdvertisers advertiser, UnityAction CompleteMethod) { Advertiser selected = GetRewardedAdvertisers().First(cond => cond.advertiser == advertiser); if (selected.advertiserScript.IsRewardVideoAvailable()) { if (debug) { Debug.Log("Rewarded Video from " + advertiser + " is available"); ScreenWriter.Write("Rewarded Video from " + advertiser + " is available"); } selected.advertiserScript.ShowRewardVideo(CompleteMethod); } else { if (debug) { Debug.Log("Rewarded Video from " + advertiser + " is NOT available"); ScreenWriter.Write("Rewarded Video from " + advertiser + " is NOT available"); } ShowRewardedVideo(CompleteMethod); } } /// /// Displays a banner based on your mediation settings /// /// can be Top or Bottom public void ShowBanner(BannerPosition position, BannerType bannerType = BannerType.SmartBanner) { //if ads are disabled by user -> do nothing if (CanShowAds() == false) { return; } for (int i = 0; i < bannerAdvertisers.Count; i++) { bannerAdvertisers[i].advertiserScript.ResetBannerUsage(); } hideBanners = false; LoadBanner(position, bannerType, false); } /// /// Displays a banner from advertiser used as parameter /// /// Advertiser to show banner from /// Top or Bottom /// Regular or Smart public void ShowBanner(SupportedAdvertisers advertiser, BannerPosition position, BannerType bannerType = BannerType.SmartBanner) { if (CanShowAds() == false) { return; } for (int i = 0; i < bannerAdvertisers.Count; i++) { bannerAdvertisers[i].advertiserScript.ResetBannerUsage(); } hideBanners = false; LoadBanner(position, bannerType, true, advertiser); } /// /// Loads banner for display /// /// /// private void LoadBanner(BannerPosition position, BannerType bannerType, bool specificAdvertiser, SupportedAdvertisers advertiser = SupportedAdvertisers.Admob) { if (specificAdvertiser) { Advertiser selected = GetBannerAdvertisers().FirstOrDefault(cond => cond.advertiser == advertiser); if (selected != null) { if (debug) { Debug.Log("Specific banner loaded from " + selected); ScreenWriter.Write("Specific banner loaded from " + selected); } selected.advertiserScript.ShowBanner(position, bannerType, BannerDisplayedResult); return; } else { if (debug) { Debug.Log(selected + " has no banner ads"); ScreenWriter.Write(selected + " has no banner ads"); } } } ICustomAds selectedAdvertiser = null; if (bannerMediation == SupportedMediation.OrderMediation) { selectedAdvertiser = UseOrder(bannerAdvertisers, SupportedAdTypes.Banner); } else { selectedAdvertiser = UsePercent(bannerAdvertisers, SupportedAdTypes.Banner); } if (selectedAdvertiser != null) { if (debug) { Debug.Log("Banner loaded from " + selectedAdvertiser); ScreenWriter.Write("Banner loaded from " + selectedAdvertiser); } selectedAdvertiser.ShowBanner(position, bannerType, BannerDisplayedResult); } else { isBannerOnScreen = false; if (debug) { Debug.Log("No Banners Available"); ScreenWriter.Write("No Banners Available"); } } } private void BannerDisplayedResult(bool succesfullyDisplayed, BannerPosition position, BannerType bannerType) { if (succesfullyDisplayed == false) { if (debug) { Debug.Log("Banner failed to load -> trying another advertiser"); ScreenWriter.Write("Banner failed to load -> trying another advertiser hideBanners=" + hideBanners); } if (hideBanners == false) { LoadBanner(position, bannerType, false); } else { Debug.Log("Stop Loading Banners"); ScreenWriter.Write("Stop Loading Banners"); } } else { isBannerOnScreen = true; if (debug) { Debug.Log("Banner is on screen"); ScreenWriter.Write("Banner is on screen"); } } } /// /// Hides the active banner /// public void HideBanner() { if (debug) { Debug.Log("Hide Banners"); ScreenWriter.Write("Hide banners"); } hideBanners = true; for (int i = 0; i < allAdvertisers.Count; i++) { allAdvertisers[i].advertiserScript.HideBanner(); } isBannerOnScreen = false; } /// /// Percent mediation method /// A random number between 0 and 100 is generated /// the corresponding advertiser will be displayed based on your Settings Window setup /// /// list of all advertisers /// type of advertiser that wants to be displayed /// private ICustomAds UsePercent(List advertisers, SupportedAdTypes adType) { List tempList = new List(); List thresholds = new List(); int sum = 0; for (int i = 0; i < advertisers.Count; i++) { switch (adType) { case SupportedAdTypes.Banner: if (advertisers[i].advertiserScript.IsBannerAvailable() && !advertisers[i].advertiserScript.BannerAlreadyUsed()) { tempList.Add(advertisers[i]); sum += advertisers[i].mediationSettings.bannerSettings.Weight; thresholds.Add(sum); } break; case SupportedAdTypes.Interstitial: if (advertisers[i].advertiserScript.IsInterstitialAvailable()) { tempList.Add(advertisers[i]); sum += advertisers[i].mediationSettings.interstitialSettings.Weight; thresholds.Add(sum); } break; case SupportedAdTypes.Rewarded: if (advertisers[i].advertiserScript.IsRewardVideoAvailable()) { tempList.Add(advertisers[i]); sum += advertisers[i].mediationSettings.rewardedSettings.Weight; thresholds.Add(sum); } break; } } int index = UnityEngine.Random.Range(0, sum); if (debug) { for (int i = 0; i < tempList.Count; i++) { ScreenWriter.Write(tempList[i].advertiser + " weight " + thresholds[i]); Debug.Log(tempList[i].advertiser + " weight " + thresholds[i]); } } for (int i = 0; i < thresholds.Count; i++) { if (index < thresholds[i]) { if (debug) { ScreenWriter.Write("SHOW AD FROM: " + tempList[i].advertiser + " weight " + index); Debug.Log("SHOW AD FROM: " + tempList[i].advertiser + " weight " + index); } return tempList[i].advertiserScript; } } return null; } /// /// Order mediation method /// The first available advertiser from list will be displayed based on Settings Window order /// /// /// /// private ICustomAds UseOrder(List advertisers, SupportedAdTypes adType) { for (int i = 0; i < advertisers.Count; i++) { switch (adType) { case SupportedAdTypes.Banner: if (advertisers[i].advertiserScript.IsBannerAvailable() && !advertisers[i].advertiserScript.BannerAlreadyUsed()) { return advertisers[i].advertiserScript; } break; case SupportedAdTypes.Interstitial: if (advertisers[i].advertiserScript.IsInterstitialAvailable()) { return advertisers[i].advertiserScript; } break; case SupportedAdTypes.Rewarded: if (advertisers[i].advertiserScript.IsRewardVideoAvailable()) { return advertisers[i].advertiserScript; } break; } } return null; } /// /// Starts loading an external config file /// Not mandatory for the plugin to work /// private void LoadFile() { //adSettings.externalFileUrl = "file://" + Application.dataPath + "/GleyPlugins/Ads/AdOrderFile/AdOrder.txt"; if (adSettings.externalFileUrl != "" && (adSettings.externalFileUrl.StartsWith("http") || adSettings.externalFileUrl.StartsWith("file"))) { StartCoroutine(LoadFile(adSettings.externalFileUrl)); } } /// /// Actual loading of external file /// /// the url to the config file /// private IEnumerator LoadFile(string url) { if (debug) { Debug.Log("URL: " + url); ScreenWriter.Write("URL: " + url); } FileLoader fileLoader = new FileLoader(); yield return StartCoroutine(fileLoader.LoadFile(url, debug)); try { string result = fileLoader.GetResult(); AdOrder adOrder = JsonUtility.FromJson(result); Debug.Log(adOrder.interstitialMediation); UpdateSettings(adOrder); } catch { if (debug) { Debug.LogWarning("File was not in correct format"); ScreenWriter.Write("File was not in correct format"); } } } /// /// refreshes the settings after a config file was read /// /// settings file private void UpdateSettings(AdOrder adOrder) { bannerMediation = adOrder.bannerMediation; interstitialMediation = adOrder.interstitialMediation; rewardedMediation = adOrder.rewardedMediation; for (int i = 0; i < adOrder.advertisers.Count; i++) { for (int j = 0; j < allAdvertisers.Count; j++) { if (allAdvertisers[j].mediationSettings.GetAdvertiser() == adOrder.advertisers[i].GetAdvertiser()) { allAdvertisers[j].mediationSettings = adOrder.advertisers[i]; } } } if (debug) { Debug.Log("File Config Loaded"); ScreenWriter.Write("File Config Loaded"); } ApplySettings(); } /// /// saves the new settings /// private void ApplySettings() { if (debug) { Debug.Log("Banner mediation type: " + bannerMediation); ScreenWriter.Write("Banner mediation type: " + bannerMediation); Debug.Log("Interstitial mediation type: " + interstitialMediation); ScreenWriter.Write("Interstitial mediation type: " + interstitialMediation); Debug.Log("Rewarded mediation type: " + rewardedMediation); ScreenWriter.Write("Rewarded mediation type: " + rewardedMediation); } bannerAdvertisers = new List(); interstitialAdvertisers = new List(); rewardedAdvertisers = new List(); for (int i = 0; i < allAdvertisers.Count; i++) { if (bannerMediation == SupportedMediation.OrderMediation) { if (allAdvertisers[i].mediationSettings.bannerSettings.Order != 0) { bannerAdvertisers.Add(allAdvertisers[i]); } } else { if (allAdvertisers[i].mediationSettings.bannerSettings.Weight != 0) { bannerAdvertisers.Add(allAdvertisers[i]); } } if (interstitialMediation == SupportedMediation.OrderMediation) { Debug.Log(allAdvertisers[i].mediationSettings.interstitialSettings.Order); if (allAdvertisers[i].mediationSettings.interstitialSettings.Order != 0) { interstitialAdvertisers.Add(allAdvertisers[i]); } } else { if (allAdvertisers[i].mediationSettings.interstitialSettings.Weight != 0) { interstitialAdvertisers.Add(allAdvertisers[i]); } } if (rewardedMediation == SupportedMediation.OrderMediation) { if (allAdvertisers[i].mediationSettings.rewardedSettings.Order != 0) { rewardedAdvertisers.Add(allAdvertisers[i]); } } else { if (allAdvertisers[i].mediationSettings.rewardedSettings.Weight != 0) { rewardedAdvertisers.Add(allAdvertisers[i]); } } } if (bannerMediation == SupportedMediation.OrderMediation) { bannerAdvertisers = bannerAdvertisers.OrderBy(cond => cond.mediationSettings.bannerSettings.Order).ToList(); } else { bannerAdvertisers = bannerAdvertisers.OrderByDescending(cond => cond.mediationSettings.bannerSettings.Weight).ToList(); } if (interstitialMediation == SupportedMediation.OrderMediation) { interstitialAdvertisers = interstitialAdvertisers.OrderBy(cond => cond.mediationSettings.interstitialSettings.Order).ToList(); } else { interstitialAdvertisers = interstitialAdvertisers.OrderByDescending(cond => cond.mediationSettings.interstitialSettings.Weight).ToList(); } if (rewardedMediation == SupportedMediation.OrderMediation) { rewardedAdvertisers = rewardedAdvertisers.OrderBy(cond => cond.mediationSettings.rewardedSettings.Order).ToList(); } else { rewardedAdvertisers = rewardedAdvertisers.OrderByDescending(cond => cond.mediationSettings.rewardedSettings.Weight).ToList(); } } /// /// Check if any rewarded video is available to be played /// /// true if at least one rewarded video is available public bool IsRewardVideoAvailable() { for (int i = 0; i < rewardedAdvertisers.Count; i++) { if (rewardedAdvertisers[i].advertiserScript.IsRewardVideoAvailable()) { return true; } } return false; } /// /// Check if any interstitial is available /// /// true if at least one interstitial is available public bool IsInterstitialAvailable() { //if ads are disabled by user -> interstitial is not available if (CanShowAds() == false) { return false; } for (int i = 0; i < interstitialAdvertisers.Count; i++) { if (interstitialAdvertisers[i].advertiserScript.IsInterstitialAvailable()) { return true; } } return false; } [System.Obsolete("This method will be removed in next update. Use IsBannerOnScreen() to check if banner is visible")] public bool IsBannerAvailable() { //if ads are disabled by user -> interstitial is not available if (CanShowAds() == false) { return false; } for (int i = 0; i < bannerAdvertisers.Count; i++) { if (bannerAdvertisers[i].advertiserScript.IsBannerAvailable()) { return true; } } return false; } /// /// Check if a banner is visible /// /// true if banner is visible public bool IsBannerOnScreen() { return isBannerOnScreen; } #region HelperFunctions private void DisplayAdvertisers(List advertisers) { for (int i = 0; i < advertisers.Count; i++) { Debug.Log(advertisers[i].advertiser + " banner order " + advertisers[i].mediationSettings.bannerSettings.Order + " interstitial order " + advertisers[i].mediationSettings.interstitialSettings.Order + " rewarded order " + advertisers[i].mediationSettings.interstitialSettings.Order); } } public List GetAllAdvertisers() { return allAdvertisers; } public List GetBannerAdvertisers() { return bannerAdvertisers; } public List GetInterstitialAdvertisers() { return interstitialAdvertisers; } public List GetRewardedAdvertisers() { return rewardedAdvertisers; } #endregion }