Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using GoogleMobileAds;
- using GoogleMobileAds.Api;
- public class ActionStateChecks
- {
- public bool State { get; set; }
- public string LogMessage { get; private set; }
- public ActionStateChecks (bool inState, string inLogMessage)
- {
- State = inState;
- LogMessage = inLogMessage;
- }
- }
- public class ActionStateItems
- {
- public List<ActionStateChecks> items = new List<ActionStateChecks> ();
- public void UpdateItems (List<bool> inItems)
- {
- if (items.Count != inItems.Count)
- Debug.Log ("Invalid inItem count!");
- for (int i = 0; i < items.Count; i++)
- items [i].State = inItems [i];
- }
- }
- public class AdmobController : MonoBehaviour
- {
- public static AdmobController Instance;
- public static bool AdMobAndroidReady = false;
- // Your interstitial and banner IDs, you will be given unique IDs per project (Set these in the inspector!)
- public string interstitialID = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
- public string bannerID = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
- // Is admob enabled? Will any banners or interstitials be triggered?
- public bool EnableAdMob = true;
- // Should we be in test mode for testing ads?
- public bool EnableTestMode = false;
- public string TestDeviceID = "SIMULATOR";
- // Should an interstitial be LOADED (Not shown) when this script starts?
- public bool IntLoadOnStart = false;
- // Shown an interstitial be LOADED (Not shown) as soon as a previous interstitial is closed (always keeping an interstitial ready in memory)
- public bool IntAutoReload = false;
- // Should a banner be LOADED (Not shown) when this script starts?
- public bool BannerLoadOnStart = false;
- public bool BannerShowOnStart = false;
- // Banner type which will be loaded on start
- //public AdSize BannerLoadOnStartType;
- // Position of banner which will be loaded on start
- public AdPosition BannerLoadOnStartPos;
- // Information about the interstitial state
- public bool IntIsReady { get; private set; }
- public bool IntIsLoading { get; private set; }
- public bool IntIsVisible { get; private set; }
- public bool IntWantedVisible { get; private set; }
- // Information about the banner state
- public bool BannerIsReady { get; private set; }
- public bool BannerIsLoading { get; private set; }
- public bool BannerIsVisible { get; private set; }
- public bool BannerWantedVisible { get; private set; }
- // Cache the type of the current banner in memory so we can process calls to LoadBanner again to change certain things without needing to actually request another banner
- private AdSize BannerInMemoryType;
- private AdPosition BannerInMemoryLayout;
- // If we're hiding a banner due to an overlay (popup box or backscreen) then we want to remember the ad state when that is closed
- public bool BannerPrevState { get; private set; }
- // Sometimes we like to overlay our overlays but still want to remember our original banner state
- public int BannerOverlayDepth { get; set; }
- public Dictionary<string, ActionStateItems> ActionState = new Dictionary<string, ActionStateItems> ();
- // Should debug messages be logged?
- public bool DebugLogging = false;
- public bool InterstitialWaitScreenEnabled = true;
- // Screen to show before an interstitial pops
- public GameObject InterstitialWaitScreen;
- // Time to wait before displaying interstitial after InterstitialWaitScreen has appeared
- public float InterstitialWaitTime = 1f;
- // An Instance to the AdmobAd Instance (AdmobAd.Instance())
- //private AdmobAd AdIns;
- public BannerView AdMobBanner { get; private set; }
- public InterstitialAd AdMobInterstitial { get; private set; }
- int rectangleAdX = 200;
- int rectangleAdY = 150;
- float scaleFactor = 0;
- void Awake ()
- {
- if (Instance) {
- DebugLog ("You have duplicate AdMob_Manager.cs scripts in your scene! Admob might not work as expected!");
- Destroy (this);
- return;
- }
- Instance = this;
- #if !AdMob
- //EnableAdMob = false;
- #endif
- ActionState.Add ("LoadInterstitial", new ActionStateItems ());
- ActionState ["LoadInterstitial"].items.Add (new ActionStateChecks (IntIsLoading, "Already loading!")); // Don't load interstitial if one is already loading
- ActionState ["LoadInterstitial"].items.Add (new ActionStateChecks (IntIsReady, "Already ready!")); // Don't load interstitial if one is already loaded
- ActionState ["LoadInterstitial"].items.Add (new ActionStateChecks (IntIsVisible, "Already visible!")); // Don't load interstitial if one is already visible
- ActionState.Add ("LoadBanner", new ActionStateItems ());
- ActionState ["LoadBanner"].items.Add (new ActionStateChecks (BannerIsLoading, "Already loading!")); // Don't load banner if one is already loading
- ActionState ["LoadBanner"].items.Add (new ActionStateChecks (BannerIsReady, "Already ready!")); // Don't load banner if one is already loaded
- ActionState ["LoadBanner"].items.Add (new ActionStateChecks (BannerIsVisible, "Already visible!")); // Don't load banner if one is already visible
- ActionState.Add ("RepositionBanner", new ActionStateItems ());
- ActionState ["RepositionBanner"].items.Add (new ActionStateChecks (BannerIsReady, "Needs to be visible!")); // Don't reposition banner if it's not yet ready
- ActionState.Add ("ShowInterstitial", new ActionStateItems ());
- ActionState ["ShowInterstitial"].items.Add (new ActionStateChecks (IntIsVisible, "Already visible!")); // Don't show interstitial if one is already visible
- ActionState.Add ("ShowBanner", new ActionStateItems ());
- ActionState ["ShowBanner"].items.Add (new ActionStateChecks (BannerIsVisible, "Already visible!")); // Don't show banner if one is already visible
- if (!EnableAdMob) {
- DebugLog ("AdMob is NOT enabled! No adverts will be triggered!", true);
- return;
- }
- if (EnableTestMode)
- DebugLog ("This build has admob set to debug mode! Remember to disable before release!", true);
- // Load an interstitial if the option is selected
- if (IntLoadOnStart)
- LoadInterstitial (false);
- // Load a banner if the option is selected
- rectangleAdX = Screen.width / 5;
- rectangleAdY = Screen.height / 4;
- Debug.Log ("AD SIZE: [" + rectangleAdX + "," + rectangleAdY + "]");
- if (BannerLoadOnStart)
- {
- int adWidth = (int)(Screen.width / 2f * GetScaleFactor());
- AdSize rectangleAd = new AdSize(250, 200);
- if (adWidth > 300) rectangleAd = new AdSize(300, 250);
- LoadBanner (rectangleAd, AdPosition.TopLeft, false);
- //LoadBanner(new AdSize(rectangleAdX, rectangleAdY), BannerLoadOnStartPos, BannerShowOnStart);
- }
- //LoadBanner(AdSize.SmartBanner, BannerLoadOnStartPos, BannerShowOnStart);
- }
- private void DebugLog (string Message, bool IgnoreDebugSetting = false)
- {
- if (!DebugLogging && !IgnoreDebugSetting)
- return;
- // Prepend AdMobManager to the debug output to make it easier to filter in logcat
- Debug.Log ("AdMobManager " + Message);
- }
- public bool isAdMini ()
- {
- return false;
- }
- private bool CanPerformAction (string ActionName, List<ActionStateChecks> ActionChecks)
- {
- // Iterate through the checks in the current item
- for (int i = 0; i < ActionChecks.Count; i++) {
- // Return false and log the event if any of the check states are false
- if (!ActionChecks [i].State) {
- //GoogleAnalytics.Instance.LogEvent("AdMob", ActionName + " failed", ActionChecks[i].LogMessage);
- DebugLog (ActionName + " failed - " + ActionChecks [i].LogMessage);
- return false;
- }
- }
- return true;
- }
- private AdRequest GenerateAdRequest ()
- {
- AdRequest.Builder AdBuilder = new AdRequest.Builder ();
- AdBuilder.AddTestDevice (AdRequest.TestDeviceSimulator); // Emulators are always marked as testers
- if (EnableTestMode) AdBuilder.AddTestDevice (TestDeviceID);
- AdBuilder.TagForChildDirectedTreatment (false);
- return AdBuilder.Build ();
- }
- /// <summary>
- /// Loads an interstitial advert into memory.
- /// </summary>
- /// <param name="DisplayImmediately">If set to <c>true</c> display the interstitial immediately when it has finished loading.</param>
- public void LoadInterstitial (bool DisplayImmediately = false, bool UseWaitScreen = false)
- {
- if (!EnableAdMob)
- return;
- // Get the name of the current method
- string MethodName = "LoadInterstitial";
- Debug.Log (MethodName + " called - DisplayImmediately: " + DisplayImmediately);
- // Update the state items (Values used to determine if the action in this method should be ran)
- ActionState [MethodName].UpdateItems (new List<bool> () { !IntIsLoading, !IntIsReady, !IntIsVisible });
- // Check if we can perform the action for the current method
- if (CanPerformAction (MethodName, ActionState [MethodName].items)) {
- // Mark the interstitial as loading
- IntIsLoading = true;
- // If we want to display the interstitial as soon as it's loaded then mark the wanted visible variable as true
- IntWantedVisible = DisplayImmediately;
- // Load an interstitial ad marking it as hidden, this script will handle showing the interstitial
- AdMobInterstitial = new InterstitialAd (interstitialID);
- // Register the interstitial ad events
- AdMobInterstitial.OnAdLoaded += OnReceiveInterstitial;
- AdMobInterstitial.OnAdFailedToLoad += OnFailedReceiveInterstitial;
- AdMobInterstitial.OnAdOpening += OnInterstitialVisible;
- AdMobInterstitial.OnAdClosed += OnInterstitialHidden;
- AdMobInterstitial.OnAdLeavingApplication += OnInterstitialClick;
- AdMobInterstitial.LoadAd (GenerateAdRequest ());
- } else {
- if (DisplayImmediately)
- ShowInterstitial (UseWaitScreen);
- }
- }
- private void LoadInterstitial (bool DisplayImmediately, bool UseWaitScreen, bool ForcedInternalCall)
- {
- if (ForcedInternalCall) {
- LoadInterstitial (false, UseWaitScreen);
- IntWantedVisible = DisplayImmediately;
- } else {
- LoadInterstitial (DisplayImmediately, UseWaitScreen);
- }
- }
- /// <summary>
- /// Shows an interstitial if one is loaded in memory.
- /// </summary>
- public void ShowInterstitial (bool UseWaitScreen = false)
- {
- if (!EnableAdMob)
- return;
- // Get the name of the current method
- string MethodName = "ShowInterstitial";
- Debug.Log (MethodName + " called");
- // Update the state items (Values used to determine if the action in this method should be ran)
- ActionState [MethodName].UpdateItems (new List<bool> () { !IntIsVisible });
- // Check if we can perform the action for the current method
- if (CanPerformAction (MethodName, ActionState [MethodName].items)) {
- if (IntIsReady) {
- if (UseWaitScreen && InterstitialWaitScreenEnabled) {
- // We're ready to show the interstitial but first a message from our sponsors err I mean a black screen wait wait text on it
- if (InterstitialWaitScreen != null) {
- StartCoroutine (ShowInterstitialAfterDelay ());
- } else {
- DebugLog ("Wait screen was enabled but no gameobject was set! Interstitial will not be delayed..", true);
- // Show the interstitial
- AdMobInterstitial.Show ();
- }
- } else {
- // Show the interstitial
- AdMobInterstitial.Show ();
- }
- } else {
- LoadInterstitial (true, UseWaitScreen, true);
- }
- }
- }
- private float TimescalePrePause;
- private IEnumerator ShowInterstitialAfterDelay ()
- {
- TimescalePrePause = Time.timeScale;
- Time.timeScale = 0f;
- // Enable the wait screen
- InterstitialWaitScreen.SetActive (true);
- yield return new WaitForSeconds (InterstitialWaitTime);
- // Show the interstitial
- AdMobInterstitial.Show ();
- // Wait again (this is to fix a bug with some devices closing the loading advert screen before the interstitial has actually popped)
- yield return new WaitForSeconds (0.5f);
- float WaitTime = 0f;
- // This script shouldn't have been ran unless the advert was ready but just incase lets force close after 3 seconds
- while (IntIsReady && WaitTime < 3f) {
- WaitTime += Time.unscaledDeltaTime;
- yield return null;
- }
- CancelInterstitial (); // If an interstitial still hasn't popped cancel the showing of it!
- // Disable the wait screen
- InterstitialWaitScreen.SetActive (false);
- // Unpause the game
- //Time.timeScale = TimescalePrePause;
- }
- /// <summary>
- /// Cancels an interstitial from loading, useful if you wanted to show an interstitial on menu x but it didn't load in time,
- /// you might want to cancel the interstitial from showing once the player enters the main game for example.
- /// </summary>
- public void CancelInterstitial ()
- {
- if (!EnableAdMob)
- return;
- // Mark the interstitial as not wanted to show
- IntWantedVisible = false;
- DebugLog ("Cancelling interstitial!");
- }
- public void DestroyInterstitial ()
- {
- if (!EnableAdMob)
- return;
- IntWantedVisible = false;
- IntIsReady = false;
- IntIsVisible = false;
- IntIsLoading = false;
- AdMobInterstitial.Destroy ();
- DebugLog ("Destroying interstitial!");
- }
- public float GetPixelToPointFactor()
- {
- BannerView banner = new BannerView(bannerID, AdSize.Banner, AdPosition.TopLeft);
- return AdSize.Banner.Width / banner.GetWidthInPixels();
- }
- public float GetScaleFactor()
- {
- if (scaleFactor == 0) scaleFactor = GetPixelToPointFactor();
- return scaleFactor;
- }
- /// <summary>
- /// Loads a banner advert into memory.
- /// </summary>
- /// <param name="AdType">Admob banner ad type.</param>
- /// <param name="AdLayout">Admob ad position.</param>
- /// <param name="DisplayImmediately">If set to <c>true</c> display the banner immediately when it has finished loading.</param>
- public void LoadBanner (AdSize AdType, AdPosition AdLayout, bool DisplayImmediately = false)
- {
- if (!EnableAdMob)
- return;
- // Get the name of the current method
- string MethodName = "LoadBanner";
- Debug.Log (MethodName + " called for " + AdType + " - DisplayImmediately: " + DisplayImmediately);
- // Update the state items (Values used to determine if the action in this method should be ran)
- ActionState [MethodName].UpdateItems (new List<bool> () { !BannerIsLoading, !BannerIsReady, !BannerIsVisible });
- // Check if we can perform the action for the current method
- if (CanPerformAction (MethodName, ActionState [MethodName].items)) {
- // Mark the banner as loading
- BannerIsLoading = true;
- // If we want to display the banner as soon as it's loaded then mark the wanted visible variable as true
- BannerWantedVisible = DisplayImmediately;
- // Load a banner ad marking it as hidden, this script will handle showing the banner
- //int adX = Screen.width / 17;//(int)(85);
- //int adY = Screen.width / 13;//85;
- //float dp = 72f / Screen.dpi;
- //int adX = Mathf.RoundToInt(((((float)Screen.width / 2f)) / dp));
- //int adY = Mathf.RoundToInt(((((float)Screen.height / 2f)) / dp));
- int xLocation = (int)(((float)Screen.width * 0.3125f) * GetScaleFactor());
- int yLocation = (int)(((float)Screen.height / 2f) * GetScaleFactor());
- if (xLocation < 0) xLocation = 0;
- //int halfAdWidthInPt = (int)((AdType.width) * PT);
- int adX = xLocation - (AdType.width/2);
- int adY = yLocation - (AdType.height/2);
- AdMobBanner = new BannerView(bannerID, AdType, adX, adY);
- // Register the banner ad events
- AdMobBanner.OnAdLoaded += OnReceiveBanner;
- AdMobBanner.OnAdFailedToLoad += OnFailReceiveBanner;
- AdMobBanner.OnAdOpening += OnBannerVisible;
- AdMobBanner.OnAdClosed += OnBannerHidden;
- AdMobBanner.OnAdLeavingApplication += OnBannerClick;
- AdMobBanner.LoadAd (GenerateAdRequest ());
- BannerInMemoryType = AdType;
- BannerInMemoryLayout = AdLayout;
- } else {
- // We already have a banner loading, ready or visible.. if we tried to load the same banner type maybe reposition or simply ShowBanner would be better here
- if (AdType == BannerInMemoryType) {
- if (AdLayout != BannerInMemoryLayout) {
- // Repositioning the banner directly is not supported by this plugin so we need to destroy the banner and create a new one
- DestroyBanner ();
- LoadBanner (AdType, AdLayout, DisplayImmediately);
- //RepositionBanner(0, 0, AdLayout);
- }
- if (DisplayImmediately)
- ShowBanner ();
- }
- }
- }
- private void LoadBanner (bool DisplayImmediately, bool ForcedInternalCall)
- {
- int adWidth = (int)(Screen.width / 2f * GetScaleFactor());
- AdSize rectangleAd = new AdSize(250, 200);
- if (adWidth > 300) rectangleAd = new AdSize(300, 250);
- if (ForcedInternalCall) {
- LoadBanner (rectangleAd, AdPosition.TopLeft, false);
- BannerWantedVisible = true;//DisplayImmediately;
- } else {
- LoadBanner (rectangleAd, AdPosition.TopLeft, DisplayImmediately);
- }
- }
- /// <summary>
- /// Repositions the banner.
- /// </summary>
- /// <param name="xPos">X position.</param>
- /// <param name="yPos">Y position.</param>
- /// <param name="AdLayout">Admob ad position.</param>
- public void RepositionBanner (int xPos, int yPos, AdPosition AdLayout = AdPosition.TopLeft)
- {
- DebugLog ("RepositionBanner not supported by this plugin!");
- return;
- }
- public static Rect RectTransformToScreenSpace (RectTransform transform)
- {
- Vector2 size = Vector2.Scale (transform.rect.size, transform.lossyScale);
- return new Rect (((Vector2)transform.position - (size * 0.5f)).x, ((Vector2)transform.position - (size * 0.5f)).y, size.x, size.y);
- }
- public static Rect RectTransformToScreenSpace2(RectTransform transform)
- {
- Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
- Rect rect = new Rect(transform.position.x, Screen.height - transform.position.y, size.x, size.y);
- rect.x -= (transform.pivot.x * size.x);
- rect.y -= ((1.0f - transform.pivot.y) * size.y);
- return rect;
- }
- public void showAdOld ()
- {
- if (GameObject.Find ("AdPlaceholder") == true) {
- RectTransform adPlaceholder = GameObject.Find ("AdPlaceholder").GetComponent<RectTransform> ();
- Rect adRect = RectTransformToScreenSpace (adPlaceholder);
- Debug.Log ("ad size1: " + (int)adRect.x + ", " + (int)adRect.y + ", " + (int)adRect.width + ", " + (int)adRect.height);
- Debug.Log ("ad size2: " + rectangleAdX + ", " + rectangleAdY);
- //LoadBanner((int)adRect.x, (int)adRect.y, (int)adRect.width, (int)adRect.height, true);
- Debug.Log ("Placeholder Found");
- } else {
- AdSize rectangleAd = new AdSize ((int)(Screen.width/2f * GetScaleFactor()), (int)(Screen.height / 2f * GetScaleFactor()));
- LoadBanner (rectangleAd, AdPosition.TopLeft, true);
- Debug.Log ("No Placeholder");
- }
- //ShowBanner();
- }
- public void showAd ()
- {
- if (Config.NOADS) return;
- if (false){//GameObject.Find ("AdPlaceholder") == true) {
- RectTransform adPlaceholder = GameObject.Find ("AdPlaceholder").GetComponent<RectTransform> ();
- Rect adRect = RectTransformToScreenSpace(adPlaceholder);
- Debug.Log(adRect.x + ", " + adRect.y);
- /*AdSize rectangleAd = new AdSize ((int)((adPlaceholder.rect.width * FindObjectOfType<Canvas> ().scaleFactor) / 2f), (int)((adPlaceholder.rect.height * FindObjectOfType<Canvas> ().scaleFactor) / 2f));
- int x = (int)Screen.width / 4;
- int y = (int)Screen.height / 2;
- int width = (int)((adPlaceholder.rect.width * FindObjectOfType<Canvas> ().scaleFactor) / 2f);
- int height = (int)((adPlaceholder.rect.height * FindObjectOfType<Canvas> ().scaleFactor) / 2f);
- LoadBanner (new AdSize(width, height), AdPosition.TopLeft, true);
- //LoadBanner(x - width/2, y - height/2, width, height, true);
- Debug.Log ("Placeholder Found");*/
- } else
- {
- ShowBanner();
- }
- //ShowBanner();
- }
- public void hideAd ()
- {
- HideBanner ();
- DestroyBanner();
- }
- public void loadInterstitial ()
- {
- }
- public void pauseAd ()
- {
- }
- public void resumeAd ()
- {
- }
- public bool isInterstitielLoaded ()
- {
- return true;
- }
- public void showInterstitial ()
- {
- ShowInterstitial ();
- }
- /// <summary>
- /// Shows a banner advert if one is loaded in memory.
- /// </summary>
- public void ShowBanner ()
- {
- if (!EnableAdMob)
- return;
- if (!BannerIsReady && !BannerIsLoading) {
- LoadBanner (true, true);
- return;
- }
- // Get the name of the current method
- string MethodName = "ShowBanner";
- Debug.Log (MethodName + " called");
- // Check if we're calling ShowBanner because we're returning from an overlay screen which hid the banner
- if (BannerOverlayDepth > 0) {
- // Decrease the overlay depth by 1
- BannerOverlayDepth--;
- // If the overlay depth is still above 0 then there must still be some overlays open
- if (BannerOverlayDepth > 0)
- return;
- // There isn't any more overlaying menus open, return to the previous banner ad state
- BannerWantedVisible = BannerPrevState;
- Debug.Log ("Banner wanted set to prev state: " + BannerPrevState);
- } else {
- BannerWantedVisible = true;
- }
- if (!BannerWantedVisible)
- return;
- // Update the state items (Values used to determine if the action in this method should be ran)
- ActionState [MethodName].UpdateItems (new List<bool> () { !BannerIsVisible });
- // Check if we can perform the action for the current method
- if (CanPerformAction (MethodName, ActionState [MethodName].items)) {
- if (BannerIsReady) {
- // Show the banner
- AdMobBanner.Show ();
- } else {
- LoadBanner (true, true);
- }
- }
- }
- /// <summary>
- /// Hides a banner advert, will also cancel a banner advert from showing if one is loaded.
- /// </summary>
- /// <param name="IsOverlay">Set to <c>true</c> if you want to hide the banner while opening an overlaying screen (such as the backscreen) and want to revert the banner ad status later.</param>
- public void HideBanner (bool IsOverlay = false)
- {
- if (!EnableAdMob)
- return;
- // If this is an overlaying screen (e.g backscreen) then we'll want to return to the previous banner state when we close it
- if (IsOverlay) {
- BannerOverlayDepth++;
- if (BannerOverlayDepth == 1)
- BannerPrevState = BannerWantedVisible;
- }
- DebugLog ("Hiding banner!");
- // Mark wanted visible as false so if the banner ad hasn't loaded yet it'll make sure it isn't shown when loaded
- BannerWantedVisible = false;
- BannerIsVisible = false;
- // Hide the banner advert from view (This does not unload it from memory)
- if (AdMobBanner != default (BannerView))
- AdMobBanner.Hide ();
- }
- /// <summary>
- /// Remove the banner from memory. (Required if you want to load a new banner ad type)
- /// </summary>
- public void DestroyBanner ()
- {
- if (!EnableAdMob)
- return;
- // Mark the banner properties as false as the banner is now destroyed
- BannerWantedVisible = false;
- BannerIsLoading = false;
- BannerIsReady = false;
- BannerIsVisible = false;
- BannerInMemoryType = default (AdSize);
- BannerInMemoryLayout = default (AdPosition);
- DebugLog ("Destroying banner!");
- // Unload the banner from memory
- AdMobBanner.Destroy ();
- }
- // Everything past this point is ad listener events //
- #region Banner callback handlers
- public void OnReceiveBanner (object Sender, EventArgs Args)
- {
- BannerIsReady = true;
- BannerIsLoading = false;
- DebugLog ("New banner loaded!");
- if (BannerWantedVisible) {
- ShowBanner ();
- } else {
- HideBanner ();
- }
- }
- public void OnFailReceiveBanner (object Sender, AdFailedToLoadEventArgs Args)
- {
- BannerIsReady = false;
- DebugLog ("Banner failed to load - Error: " + Args.Message);
- GoogleAnalytics.Instance.LogEvent ("AdMob", "Banner Load Failure", "Error: " + Args.Message);
- StartCoroutine (RetryBannerLoad ());
- }
- private IEnumerator RetryBannerLoad ()
- {
- yield return new WaitForSeconds (5f);
- DebugLog ("Retrying banner load!");
- if (!BannerIsLoading) {
- BannerInMemoryType = default (AdSize);
- BannerInMemoryLayout = default (AdPosition);
- yield break;
- }
- LoadBanner (BannerInMemoryType, BannerInMemoryLayout, false);
- }
- public void OnBannerVisible (object Sender, EventArgs Args)
- {
- BannerIsVisible = true;
- if (!BannerWantedVisible)
- HideBanner ();
- }
- public void OnBannerHidden (object Sender, EventArgs Args)
- {
- BannerIsVisible = false;
- if (BannerWantedVisible)
- ShowBanner ();
- }
- public void OnBannerClick (object Sender, EventArgs Args)
- {
- GoogleAnalytics.Instance.LogEvent ("AdMob", "Banner Clicked", "CLIENT_ID");
- }
- #endregion
- #region Interstitial callback handlers
- public void OnReceiveInterstitial (object Sender, EventArgs Args)
- {
- IntIsReady = true;
- IntIsLoading = false;
- if (IntWantedVisible)
- ShowInterstitial ();
- }
- public void OnFailedReceiveInterstitial (object Sender, AdFailedToLoadEventArgs Args)
- {
- IntIsReady = false;
- IntIsLoading = false;
- DebugLog ("Interstitial failed to load - Error: " + Args.Message);
- GoogleAnalytics.Instance.LogEvent ("AdMob", "Interstitial Load Failure", "Error: " + Args.Message);
- }
- public void OnInterstitialVisible (object Sender, EventArgs Args)
- {
- Time.timeScale = 0f;
- IntIsReady = false;
- IntIsVisible = true;
- // The interstitial auto hides ads but lets call the usual functions so the variables know what's happening
- HideBanner (true);
- }
- public void OnInterstitialHidden (object Sender, EventArgs Args)
- {
- IntIsVisible = false;
- if (IntAutoReload)
- LoadInterstitial (false);
- Time.timeScale = 1f;
- }
- public void OnInterstitialClick (object Sender, EventArgs Args)
- {
- Time.timeScale = 1f;
- GoogleAnalytics.Instance.LogEvent ("AdMob", "Interstitial Clicked", "CLIENT_ID");
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement