namespace GleyMobileAds { #if USE_IRONSOURCE using System.Linq; using System.Collections; #endif using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class CustomIronSource : MonoBehaviour, ICustomAds { #if USE_IRONSOURCE private const float reloadTime = 30; private readonly int maxRetryCount = 10; private bool debug; private bool initialized; private string appKey; private string bannerAdUnit; private string interstitialAdUnit; private string rewardedVideoAdUnit; private bool bannerUsed; private UnityAction DisplayResult; private BannerPosition position; private BannerType bannerType; private UnityAction OnInterstitialClosed; private UnityAction OnInterstitialClosedWithAdvertiser; private int currentRetryInterstitial; private UnityAction OnCompleteMethod; private UnityAction OnCompleteMethodWithAdvertiser; private bool rewardedWatched; #region Initialization /// /// Initializing IronSource /// /// 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; #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 appKey = settings.appId.id; bannerAdUnit = settings.idBanner.id; interstitialAdUnit = settings.idInterstitial.id; rewardedVideoAdUnit = settings.idRewarded.id; //verify settings if (debug) { Debug.Log(this + " Initialize"); ScreenWriter.Write(this + " Initialize"); Debug.Log(this + " App Key: " + appKey); ScreenWriter.Write(this + " App Key: " + appKey); 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); } UpdateConsent(consent, ccpaConsent); if(settings.directedForChildren) { IronSource.Agent.setMetaData("is_child_directed", "true"); } else { IronSource.Agent.setMetaData("is_child_directed", "false"); } if (!string.IsNullOrEmpty(bannerAdUnit)) { IronSourceEvents.onBannerAdLoadedEvent += BannerAdLoadedEvent; IronSourceEvents.onBannerAdLoadFailedEvent += BannerAdLoadFailedEvent; IronSourceEvents.onBannerAdClickedEvent += BannerAdClickedEvent; IronSourceEvents.onBannerAdScreenPresentedEvent += BannerAdScreenPresentedEvent; IronSourceEvents.onBannerAdScreenDismissedEvent += BannerAdScreenDismissedEvent; IronSourceEvents.onBannerAdLeftApplicationEvent += BannerAdLeftApplicationEvent; IronSource.Agent.init(appKey, IronSourceAdUnits.BANNER); } if (!string.IsNullOrEmpty(interstitialAdUnit)) { IronSourceEvents.onInterstitialAdReadyEvent += InterstitialAdReadyEvent; IronSourceEvents.onInterstitialAdLoadFailedEvent += InterstitialAdLoadFailedEvent; IronSourceEvents.onInterstitialAdShowSucceededEvent += InterstitialAdShowSucceededEvent; IronSourceEvents.onInterstitialAdShowFailedEvent += InterstitialAdShowFailedEvent; IronSourceEvents.onInterstitialAdClickedEvent += InterstitialAdClickedEvent; IronSourceEvents.onInterstitialAdOpenedEvent += InterstitialAdOpenedEvent; IronSourceEvents.onInterstitialAdClosedEvent += InterstitialAdClosedEvent; IronSource.Agent.init(appKey, IronSourceAdUnits.INTERSTITIAL); LoadInterstitial(); } if (!string.IsNullOrEmpty(rewardedVideoAdUnit)) { IronSourceEvents.onRewardedVideoAdOpenedEvent += RewardedVideoAdOpenedEvent; IronSourceEvents.onRewardedVideoAdClosedEvent += RewardedVideoAdClosedEvent; IronSourceEvents.onRewardedVideoAvailabilityChangedEvent += RewardedVideoAvailabilityChangedEvent; IronSourceEvents.onRewardedVideoAdStartedEvent += RewardedVideoAdStartedEvent; IronSourceEvents.onRewardedVideoAdEndedEvent += RewardedVideoAdEndedEvent; IronSourceEvents.onRewardedVideoAdRewardedEvent += RewardedVideoAdRewardedEvent; IronSourceEvents.onRewardedVideoAdShowFailedEvent += RewardedVideoAdShowFailedEvent; IronSource.Agent.init(appKey, IronSourceAdUnits.REWARDED_VIDEO); } //IronSource.Agent.validateIntegration(); } } /// /// Updates consent at runtime /// /// the new consent public void UpdateConsent(UserConsent consent, UserConsent ccpaConsent) { if(consent == UserConsent.Unset || consent == UserConsent.Accept) { IronSource.Agent.setConsent(true); } else { IronSource.Agent.setConsent(false); } if(ccpaConsent == UserConsent.Unset || ccpaConsent == UserConsent.Accept) { IronSource.Agent.setMetaData("do_not_sell", "false"); } else { IronSource.Agent.setMetaData("do_not_sell", "true"); } } #endregion #region Banner /// /// Check if IronSource banner is available /// /// true if a banner is available public bool IsBannerAvailable() { return true; } /// /// Show IronSource 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(); IronSourceBannerPosition bannerPosition; if (position == BannerPosition.BOTTOM) { bannerPosition = IronSourceBannerPosition.BOTTOM; } else { bannerPosition = IronSourceBannerPosition.TOP; } IronSourceBannerSize bannerSize; if (bannerType == BannerType.Banner) { bannerSize = IronSourceBannerSize.BANNER; } else { bannerSize = IronSourceBannerSize.SMART; } IronSource.Agent.loadBanner(bannerSize, bannerPosition, bannerAdUnit); } /// /// Hides IronSource banner /// public void HideBanner() { IronSource.Agent.destroyBanner(); } /// /// Used for mediation purpose /// /// true if current banner failed to load public bool BannerAlreadyUsed() { return bannerUsed; } /// /// Used for mediation purpose /// public void ResetBannerUsage() { bannerUsed = false; } //Invoked once the banner has loaded void BannerAdLoadedEvent() { if (debug) { Debug.Log(this + " BannerAdLoadedEvent"); ScreenWriter.Write(this + " BannerAdLoadedEvent"); } if (DisplayResult != null) { DisplayResult(true, position, bannerType); DisplayResult = null; } } //Invoked when the banner loading process has failed. //@param description - string - contains information about the failure. void BannerAdLoadFailedEvent(IronSourceError error) { if (debug) { Debug.Log(this + " BannerAdLoadFailedEvent " + error); ScreenWriter.Write(this + " BannerAdLoadFailedEvent " + error); } if (DisplayResult != null) { DisplayResult(false, position, bannerType); DisplayResult = null; } } // Invoked when end user clicks on the banner ad void BannerAdClickedEvent() { if (debug) { Debug.Log(this + " BannerAdClickedEvent"); ScreenWriter.Write(this + " BannerAdClickedEvent"); } } //Notifies the presentation of a full screen content following user click void BannerAdScreenPresentedEvent() { if (debug) { Debug.Log(this + " BannerAdScreenPresentedEvent"); ScreenWriter.Write(this + " BannerAdScreenPresentedEvent"); } } //Notifies the presented screen has been dismissed void BannerAdScreenDismissedEvent() { if (debug) { Debug.Log(this + " BannerAdScreenDismissedEvent"); ScreenWriter.Write(this + " BannerAdScreenDismissedEvent"); } } //Invoked when the user leaves the app void BannerAdLeftApplicationEvent() { if (debug) { Debug.Log(this + " BannerAdLeftApplicationEvent"); ScreenWriter.Write(this + " BannerAdLeftApplicationEvent"); } } #endregion #region Interstitial /// /// Check if IronSource interstitial is available /// /// true if an interstitial is available public bool IsInterstitialAvailable() { return IronSource.Agent.isInterstitialReady(); } /// /// Show IronSource interstitial /// /// callback called when user closes interstitial public void ShowInterstitial(UnityAction InterstitialClosed) { if (IsInterstitialAvailable()) { OnInterstitialClosed = InterstitialClosed; IronSource.Agent.showInterstitial(interstitialAdUnit); } } /// /// Show IronSource interstitial /// /// callback called when user closes interstitial public void ShowInterstitial(UnityAction InterstitialClosed) { if (IsInterstitialAvailable()) { OnInterstitialClosedWithAdvertiser = InterstitialClosed; IronSource.Agent.showInterstitial(interstitialAdUnit); } } /// /// Loads IronSource interstitial /// private void LoadInterstitial() { if (debug) { Debug.Log(this + " Start Loading Interstitial"); ScreenWriter.Write(this + " Start Loading Interstitial"); } IronSource.Agent.loadInterstitial(); } /// /// Coroutine to reload an interstitial after a fail /// /// time to wait /// private IEnumerator ReloadInterstitial(float reloadTime) { yield return new WaitForSeconds(reloadTime); LoadInterstitial(); } //Invoked when the initialization process has failed. //@param description - string - contains information about the failure. void InterstitialAdLoadFailedEvent(IronSourceError error) { if (debug) { Debug.Log(this + " Interstitial Failed To Load " + error); ScreenWriter.Write(this + " Interstitial Failed To Load " + error); } if (currentRetryInterstitial < maxRetryCount) { currentRetryInterstitial++; if (debug) { Debug.Log(this + " RETRY " + currentRetryInterstitial); ScreenWriter.Write(this + " RETRY " + currentRetryInterstitial); } StartCoroutine(ReloadInterstitial(reloadTime)); } } //Invoked right before the Interstitial screen is about to open. void InterstitialAdShowSucceededEvent() { if (debug) { Debug.Log(this + " Interstitial Showed"); ScreenWriter.Write(this + " Interstitial Showed"); } currentRetryInterstitial = 0; } //Invoked when the ad fails to show. //@param description - string - contains information about the failure. void InterstitialAdShowFailedEvent(IronSourceError error) { if (debug) { Debug.Log(this + " Interstitial Failed To Show " + error); ScreenWriter.Write(this + " Interstitial Failed To Show " + error); } StartCoroutine(ReloadInterstitial(reloadTime)); } // Invoked when end user clicked on the interstitial ad void InterstitialAdClickedEvent() { if (debug) { Debug.Log(this + " InterstitialAdClickedEvent"); ScreenWriter.Write(this + " InterstitialAdClickedEvent"); } } //Invoked when the interstitial ad closed and the user goes back to the application screen. void InterstitialAdClosedEvent() { 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.IronSource.ToString()); OnInterstitialClosedWithAdvertiser = null; } } //Invoked when the Interstitial is Ready to shown after load function is called void InterstitialAdReadyEvent() { if (debug) { Debug.Log(this + " InterstitialAdReadyEvent"); ScreenWriter.Write(this + " InterstitialAdReadyEvent"); } } //Invoked when the Interstitial Ad Unit has opened void InterstitialAdOpenedEvent() { if (debug) { Debug.Log(this + " InterstitialAdOpenedEvent"); ScreenWriter.Write(this + " InterstitialAdOpenedEvent"); } } #endregion #region Rewarded /// /// Check if IronSource rewarded video is available /// /// true if a rewarded video is available public bool IsRewardVideoAvailable() { return IronSource.Agent.isRewardedVideoAvailable(); } /// /// Show IronSource 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; IronSource.Agent.showRewardedVideo(rewardedVideoAdUnit); } } /// /// Show IronSource 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; IronSource.Agent.showRewardedVideo(rewardedVideoAdUnit); } } //Invoked when the RewardedVideo ad view has opened. //Your Activity will lose focus. Please avoid performing heavy //tasks till the video ad will be closed. void RewardedVideoAdOpenedEvent() { if (debug) { Debug.Log(this + " RewardedVideoAdOpenedEvent"); ScreenWriter.Write(this + " RewardedVideoAdOpenedEvent"); } } //Invoked when the RewardedVideo ad view is about to be closed. //Your activity will now regain its focus. void RewardedVideoAdClosedEvent() { if (debug) { Debug.Log(this + " RewardedVideoAdClosedEvent"); ScreenWriter.Write(this + " RewardedVideoAdClosedEvent"); } if (OnCompleteMethod != null) { OnCompleteMethod(rewardedWatched); OnCompleteMethod = null; } if (OnCompleteMethodWithAdvertiser != null) { OnCompleteMethodWithAdvertiser(rewardedWatched, SupportedAdvertisers.IronSource.ToString()); OnCompleteMethodWithAdvertiser = null; } rewardedWatched = false; } //Invoked when there is a change in the ad availability status. //@param - available - value will change to true when rewarded videos are available. //You can then show the video by calling showRewardedVideo(). //Value will change to false when no videos are available. void RewardedVideoAvailabilityChangedEvent(bool available) { if (debug) { Debug.Log(this + " RewardedVideoAvailabilityChangedEvent " + available); ScreenWriter.Write(this + " RewardedVideoAvailabilityChangedEvent " + available); } } // Note: the events below are not available for all supported rewarded video // ad networks. Check which events are available per ad network you choose // to include in your build. // We recommend only using events which register to ALL ad networks you // include in your build. //Invoked when the video ad starts playing. void RewardedVideoAdStartedEvent() { if (debug) { Debug.Log(this + " RewardedVideoAdStartedEvent"); ScreenWriter.Write(this + " RewardedVideoAdStartedEvent"); } } //Invoked when the video ad finishes playing. void RewardedVideoAdEndedEvent() { if (debug) { Debug.Log(this + " RewardedVideoAdEndedEvent"); ScreenWriter.Write(this + " RewardedVideoAdEndedEvent"); } } //Invoked when the user completed the video and should be rewarded. //If using server-to-server callbacks you may ignore this events and wait for the callback from the ironSource server. // //@param - placement - placement object which contains the reward data // void RewardedVideoAdRewardedEvent(IronSourcePlacement placement) { if (debug) { Debug.Log(this + " RewardedVideoAdRewardedEvent " + placement); ScreenWriter.Write(this + " RewardedVideoAdRewardedEvent " + placement); } rewardedWatched = true; } //Invoked when the Rewarded Video failed to show //@param description - string - contains information about the failure. void RewardedVideoAdShowFailedEvent(IronSourceError error) { if (debug) { Debug.Log(this + " RewardedVideoAdShowFailedEvent " + error); ScreenWriter.Write(this + " RewardedVideoAdShowFailedEvent " + error); } } #endregion private void OnApplicationFocus(bool focus) { if (focus == true) { if (IsInterstitialAvailable() == false) { if (currentRetryInterstitial == maxRetryCount) { LoadInterstitial(); } } } } void OnApplicationPause(bool isPaused) { IronSource.Agent.onApplicationPause(isPaused); } #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 } }