SHARE
TWEET

Untitled

a guest Jun 25th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. using System;
  6.  
  7. using GoogleMobileAds;
  8. using GoogleMobileAds.Api;
  9.  
  10. public class ActionStateChecks
  11. {
  12.     public bool State { get; set; }
  13.     public string LogMessage { get; private set; }
  14.  
  15.     public ActionStateChecks (bool inState, string inLogMessage)
  16.     {
  17.         State = inState;
  18.         LogMessage = inLogMessage;
  19.     }
  20. }
  21.  
  22. public class ActionStateItems
  23. {
  24.     public List<ActionStateChecks> items = new List<ActionStateChecks> ();
  25.  
  26.     public void UpdateItems (List<bool> inItems)
  27.     {
  28.         if (items.Count != inItems.Count)
  29.             Debug.Log ("Invalid inItem count!");
  30.  
  31.         for (int i = 0; i < items.Count; i++)
  32.             items [i].State = inItems [i];
  33.     }
  34. }
  35.  
  36. public class AdmobController : MonoBehaviour
  37. {
  38.     public static AdmobController Instance;
  39.     public static bool AdMobAndroidReady = false;
  40.  
  41.     // Your interstitial and banner IDs, you will be given unique IDs per project (Set these in the inspector!)
  42.     public string interstitialID = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
  43.     public string bannerID = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
  44.     // Is admob enabled? Will any banners or interstitials be triggered?
  45.     public bool EnableAdMob = true;
  46.  
  47.     // Should we be in test mode for testing ads?
  48.     public bool EnableTestMode = false;
  49.  
  50.     public string TestDeviceID = "SIMULATOR";
  51.  
  52.     // Should an interstitial be LOADED (Not shown) when this script starts?
  53.     public bool IntLoadOnStart = false;
  54.  
  55.     // Shown an interstitial be LOADED (Not shown) as soon as a previous interstitial is closed (always keeping an interstitial ready in memory)
  56.     public bool IntAutoReload = false;
  57.  
  58.     // Should a banner be LOADED (Not shown) when this script starts?
  59.     public bool BannerLoadOnStart = false;
  60.  
  61.     public bool BannerShowOnStart = false;
  62.  
  63.     // Banner type which will be loaded on start
  64.     //public AdSize BannerLoadOnStartType;
  65.  
  66.     // Position of banner which will be loaded on start
  67.     public AdPosition BannerLoadOnStartPos;
  68.  
  69.     // Information about the interstitial state
  70.     public bool IntIsReady { get; private set; }
  71.     public bool IntIsLoading { get; private set; }
  72.     public bool IntIsVisible { get; private set; }
  73.     public bool IntWantedVisible { get; private set; }
  74.  
  75.     // Information about the banner state
  76.     public bool BannerIsReady { get; private set; }
  77.     public bool BannerIsLoading { get; private set; }
  78.     public bool BannerIsVisible { get; private set; }
  79.     public bool BannerWantedVisible { get; private set; }
  80.  
  81.     // 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
  82.     private AdSize BannerInMemoryType;
  83.     private AdPosition BannerInMemoryLayout;
  84.  
  85.     // 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
  86.     public bool BannerPrevState { get; private set; }
  87.  
  88.     // Sometimes we like to overlay our overlays but still want to remember our original banner state
  89.     public int BannerOverlayDepth { get; set; }
  90.  
  91.     public Dictionary<string, ActionStateItems> ActionState = new Dictionary<string, ActionStateItems> ();
  92.  
  93.     // Should debug messages be logged?
  94.     public bool DebugLogging = false;
  95.  
  96.     public bool InterstitialWaitScreenEnabled = true;
  97.  
  98.     // Screen to show before an interstitial pops
  99.     public GameObject InterstitialWaitScreen;
  100.  
  101.     // Time to wait before displaying interstitial after InterstitialWaitScreen has appeared
  102.     public float InterstitialWaitTime = 1f;
  103.  
  104.     // An Instance to the AdmobAd Instance (AdmobAd.Instance())
  105.     //private AdmobAd AdIns;
  106.     public BannerView AdMobBanner { get; private set; }
  107.     public InterstitialAd AdMobInterstitial { get; private set; }
  108.     int rectangleAdX = 200;
  109.     int rectangleAdY = 150;
  110.     float scaleFactor = 0;
  111.  
  112.     void Awake ()
  113.     {
  114.         if (Instance) {
  115.             DebugLog ("You have duplicate AdMob_Manager.cs scripts in your scene! Admob might not work as expected!");
  116.             Destroy (this);
  117.             return;
  118.         }
  119.  
  120.         Instance = this;
  121.  
  122. #if !AdMob
  123.         //EnableAdMob = false;
  124. #endif
  125.  
  126.         ActionState.Add ("LoadInterstitial", new ActionStateItems ());
  127.         ActionState ["LoadInterstitial"].items.Add (new ActionStateChecks (IntIsLoading, "Already loading!")); // Don't load interstitial if one is already loading
  128.         ActionState ["LoadInterstitial"].items.Add (new ActionStateChecks (IntIsReady, "Already ready!"));     // Don't load interstitial if one is already loaded
  129.         ActionState ["LoadInterstitial"].items.Add (new ActionStateChecks (IntIsVisible, "Already visible!")); // Don't load interstitial if one is already visible
  130.  
  131.         ActionState.Add ("LoadBanner", new ActionStateItems ());
  132.         ActionState ["LoadBanner"].items.Add (new ActionStateChecks (BannerIsLoading, "Already loading!"));    // Don't load banner if one is already loading
  133.         ActionState ["LoadBanner"].items.Add (new ActionStateChecks (BannerIsReady, "Already ready!"));        // Don't load banner if one is already loaded
  134.         ActionState ["LoadBanner"].items.Add (new ActionStateChecks (BannerIsVisible, "Already visible!"));    // Don't load banner if one is already visible
  135.  
  136.         ActionState.Add ("RepositionBanner", new ActionStateItems ());
  137.         ActionState ["RepositionBanner"].items.Add (new ActionStateChecks (BannerIsReady, "Needs to be visible!"));    // Don't reposition banner if it's not yet ready
  138.  
  139.         ActionState.Add ("ShowInterstitial", new ActionStateItems ());
  140.         ActionState ["ShowInterstitial"].items.Add (new ActionStateChecks (IntIsVisible, "Already visible!")); // Don't show interstitial if one is already visible
  141.  
  142.         ActionState.Add ("ShowBanner", new ActionStateItems ());
  143.         ActionState ["ShowBanner"].items.Add (new ActionStateChecks (BannerIsVisible, "Already visible!"));    // Don't show banner if one is already visible
  144.  
  145.         if (!EnableAdMob) {
  146.             DebugLog ("AdMob is NOT enabled! No adverts will be triggered!", true);
  147.             return;
  148.         }
  149.  
  150.         if (EnableTestMode)
  151.             DebugLog ("This build has admob set to debug mode! Remember to disable before release!", true);
  152.  
  153.         // Load an interstitial if the option is selected
  154.         if (IntLoadOnStart)
  155.             LoadInterstitial (false);
  156.  
  157.         // Load a banner if the option is selected
  158.         rectangleAdX = Screen.width / 5;
  159.         rectangleAdY = Screen.height / 4;
  160.         Debug.Log ("AD SIZE: [" + rectangleAdX + "," + rectangleAdY + "]");
  161.         if (BannerLoadOnStart)
  162.         {
  163.             int adWidth = (int)(Screen.width / 2f * GetScaleFactor());
  164.             AdSize rectangleAd = new AdSize(250, 200);
  165.             if (adWidth > 300) rectangleAd = new AdSize(300, 250);
  166.             LoadBanner (rectangleAd, AdPosition.TopLeft, false);
  167.             //LoadBanner(new AdSize(rectangleAdX, rectangleAdY), BannerLoadOnStartPos, BannerShowOnStart);
  168.         }
  169.         //LoadBanner(AdSize.SmartBanner, BannerLoadOnStartPos, BannerShowOnStart);
  170.     }
  171.  
  172.     private void DebugLog (string Message, bool IgnoreDebugSetting = false)
  173.     {
  174.         if (!DebugLogging && !IgnoreDebugSetting)
  175.             return;
  176.  
  177.         // Prepend AdMobManager to the debug output to make it easier to filter in logcat
  178.         Debug.Log ("AdMobManager " + Message);
  179.     }
  180.  
  181.     public bool isAdMini ()
  182.     {
  183.         return false;
  184.     }
  185.  
  186.     private bool CanPerformAction (string ActionName, List<ActionStateChecks> ActionChecks)
  187.     {
  188.         // Iterate through the checks in the current item
  189.         for (int i = 0; i < ActionChecks.Count; i++) {
  190.             // Return false and log the event if any of the check states are false
  191.             if (!ActionChecks [i].State) {
  192.                 //GoogleAnalytics.Instance.LogEvent("AdMob", ActionName + " failed", ActionChecks[i].LogMessage);
  193.                 DebugLog (ActionName + " failed - " + ActionChecks [i].LogMessage);
  194.                 return false;
  195.             }
  196.         }
  197.  
  198.         return true;
  199.     }
  200.  
  201.     private AdRequest GenerateAdRequest ()
  202.     {
  203.         AdRequest.Builder AdBuilder = new AdRequest.Builder ();
  204.  
  205.         AdBuilder.AddTestDevice (AdRequest.TestDeviceSimulator); // Emulators are always marked as testers
  206.         if (EnableTestMode) AdBuilder.AddTestDevice (TestDeviceID);
  207.         AdBuilder.TagForChildDirectedTreatment (false);
  208.  
  209.         return AdBuilder.Build ();
  210.     }
  211.  
  212.     /// <summary>
  213.     /// Loads an interstitial advert into memory.
  214.     /// </summary>
  215.     /// <param name="DisplayImmediately">If set to <c>true</c> display the interstitial immediately when it has finished loading.</param>
  216.     public void LoadInterstitial (bool DisplayImmediately = false, bool UseWaitScreen = false)
  217.     {
  218.         if (!EnableAdMob)
  219.             return;
  220.  
  221.         // Get the name of the current method
  222.         string MethodName = "LoadInterstitial";
  223.  
  224.         Debug.Log (MethodName + " called - DisplayImmediately: " + DisplayImmediately);
  225.  
  226.         // Update the state items (Values used to determine if the action in this method should be ran)
  227.         ActionState [MethodName].UpdateItems (new List<bool> () { !IntIsLoading, !IntIsReady, !IntIsVisible });
  228.  
  229.         // Check if we can perform the action for the current method
  230.         if (CanPerformAction (MethodName, ActionState [MethodName].items)) {
  231.             // Mark the interstitial as loading
  232.             IntIsLoading = true;
  233.  
  234.             // If we want to display the interstitial as soon as it's loaded then mark the wanted visible variable as true
  235.             IntWantedVisible = DisplayImmediately;
  236.  
  237.             // Load an interstitial ad marking it as hidden, this script will handle showing the interstitial
  238.                 AdMobInterstitial = new InterstitialAd (interstitialID);
  239.  
  240.             // Register the interstitial ad events
  241.             AdMobInterstitial.OnAdLoaded += OnReceiveInterstitial;
  242.             AdMobInterstitial.OnAdFailedToLoad += OnFailedReceiveInterstitial;
  243.             AdMobInterstitial.OnAdOpening += OnInterstitialVisible;
  244.             AdMobInterstitial.OnAdClosed += OnInterstitialHidden;
  245.             AdMobInterstitial.OnAdLeavingApplication += OnInterstitialClick;
  246.  
  247.             AdMobInterstitial.LoadAd (GenerateAdRequest ());
  248.         } else {
  249.             if (DisplayImmediately)
  250.                 ShowInterstitial (UseWaitScreen);
  251.         }
  252.     }
  253.  
  254.     private void LoadInterstitial (bool DisplayImmediately, bool UseWaitScreen, bool ForcedInternalCall)
  255.     {
  256.         if (ForcedInternalCall) {
  257.             LoadInterstitial (false, UseWaitScreen);
  258.             IntWantedVisible = DisplayImmediately;
  259.         } else {
  260.             LoadInterstitial (DisplayImmediately, UseWaitScreen);
  261.         }
  262.     }
  263.  
  264.     /// <summary>
  265.     /// Shows an interstitial if one is loaded in memory.
  266.     /// </summary>
  267.     public void ShowInterstitial (bool UseWaitScreen = false)
  268.     {
  269.         if (!EnableAdMob)
  270.             return;
  271.  
  272.         // Get the name of the current method
  273.         string MethodName = "ShowInterstitial";
  274.  
  275.         Debug.Log (MethodName + " called");
  276.  
  277.         // Update the state items (Values used to determine if the action in this method should be ran)
  278.         ActionState [MethodName].UpdateItems (new List<bool> () { !IntIsVisible });
  279.  
  280.         // Check if we can perform the action for the current method
  281.         if (CanPerformAction (MethodName, ActionState [MethodName].items)) {
  282.             if (IntIsReady) {
  283.                 if (UseWaitScreen && InterstitialWaitScreenEnabled) {
  284.                     // 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
  285.                     if (InterstitialWaitScreen != null) {
  286.                         StartCoroutine (ShowInterstitialAfterDelay ());
  287.                     } else {
  288.                         DebugLog ("Wait screen was enabled but no gameobject was set! Interstitial will not be delayed..", true);
  289.  
  290.                         // Show the interstitial
  291.                         AdMobInterstitial.Show ();
  292.                     }
  293.                 } else {
  294.                     // Show the interstitial
  295.                     AdMobInterstitial.Show ();
  296.                 }
  297.             } else {
  298.                 LoadInterstitial (true, UseWaitScreen, true);
  299.             }
  300.         }
  301.     }
  302.  
  303.     private float TimescalePrePause;
  304.  
  305.     private IEnumerator ShowInterstitialAfterDelay ()
  306.     {
  307.         TimescalePrePause = Time.timeScale;
  308.  
  309.         Time.timeScale = 0f;
  310.  
  311.         // Enable the wait screen
  312.         InterstitialWaitScreen.SetActive (true);
  313.  
  314.         yield return new WaitForSeconds (InterstitialWaitTime);
  315.  
  316.         // Show the interstitial
  317.         AdMobInterstitial.Show ();
  318.  
  319.         // Wait again (this is to fix a bug with some devices closing the loading advert screen before the interstitial has actually popped)
  320.         yield return new WaitForSeconds (0.5f);
  321.  
  322.         float WaitTime = 0f;
  323.  
  324.         // This script shouldn't have been ran unless the advert was ready but just incase lets force close after 3 seconds
  325.         while (IntIsReady && WaitTime < 3f) {
  326.             WaitTime += Time.unscaledDeltaTime;
  327.             yield return null;
  328.         }
  329.  
  330.         CancelInterstitial (); // If an interstitial still hasn't popped cancel the showing of it!
  331.  
  332.         // Disable the wait screen
  333.         InterstitialWaitScreen.SetActive (false);
  334.  
  335.         // Unpause the game
  336.         //Time.timeScale = TimescalePrePause;
  337.     }
  338.  
  339.     /// <summary>
  340.     /// Cancels an interstitial from loading, useful if you wanted to show an interstitial on menu x but it didn't load in time,
  341.     /// you might want to cancel the interstitial from showing once the player enters the main game for example.
  342.     /// </summary>
  343.     public void CancelInterstitial ()
  344.     {
  345.         if (!EnableAdMob)
  346.             return;
  347.  
  348.         // Mark the interstitial as not wanted to show
  349.         IntWantedVisible = false;
  350.  
  351.         DebugLog ("Cancelling interstitial!");
  352.     }
  353.  
  354.     public void DestroyInterstitial ()
  355.     {
  356.         if (!EnableAdMob)
  357.             return;
  358.  
  359.         IntWantedVisible = false;
  360.         IntIsReady = false;
  361.         IntIsVisible = false;
  362.         IntIsLoading = false;
  363.  
  364.         AdMobInterstitial.Destroy ();
  365.  
  366.         DebugLog ("Destroying interstitial!");
  367.     }
  368.  
  369.     public float GetPixelToPointFactor()
  370.     {
  371.         BannerView banner = new BannerView(bannerID, AdSize.Banner, AdPosition.TopLeft);
  372.  
  373.         return AdSize.Banner.Width / banner.GetWidthInPixels();
  374.     }
  375.  
  376.     public float GetScaleFactor()
  377.     {
  378.         if (scaleFactor == 0) scaleFactor = GetPixelToPointFactor();
  379.         return scaleFactor;
  380.     }
  381.  
  382.     /// <summary>
  383.     /// Loads a banner advert into memory.
  384.     /// </summary>
  385.     /// <param name="AdType">Admob banner ad type.</param>
  386.     /// <param name="AdLayout">Admob ad position.</param>
  387.     /// <param name="DisplayImmediately">If set to <c>true</c> display the banner immediately when it has finished loading.</param>
  388.     public void LoadBanner (AdSize AdType, AdPosition AdLayout, bool DisplayImmediately = false)
  389.     {
  390.         if (!EnableAdMob)
  391.             return;
  392.  
  393.         // Get the name of the current method
  394.         string MethodName = "LoadBanner";
  395.  
  396.         Debug.Log (MethodName + " called for " + AdType + " - DisplayImmediately: " + DisplayImmediately);
  397.  
  398.         // Update the state items (Values used to determine if the action in this method should be ran)
  399.         ActionState [MethodName].UpdateItems (new List<bool> () { !BannerIsLoading, !BannerIsReady, !BannerIsVisible });
  400.  
  401.         // Check if we can perform the action for the current method
  402.         if (CanPerformAction (MethodName, ActionState [MethodName].items)) {
  403.             // Mark the banner as loading
  404.             BannerIsLoading = true;
  405.  
  406.             // If we want to display the banner as soon as it's loaded then mark the wanted visible variable as true
  407.             BannerWantedVisible = DisplayImmediately;
  408.  
  409.             // Load a banner ad marking it as hidden, this script will handle showing the banner
  410.  
  411.             //int adX = Screen.width / 17;//(int)(85);
  412.             //int adY = Screen.width / 13;//85;
  413.  
  414.             //float dp = 72f / Screen.dpi;
  415.  
  416.             //int adX = Mathf.RoundToInt(((((float)Screen.width / 2f)) / dp));
  417.             //int adY = Mathf.RoundToInt(((((float)Screen.height / 2f)) / dp));
  418.  
  419.             int xLocation = (int)(((float)Screen.width * 0.3125f) * GetScaleFactor());
  420.             int yLocation = (int)(((float)Screen.height / 2f) * GetScaleFactor());
  421.             if (xLocation < 0) xLocation = 0;
  422.             //int halfAdWidthInPt = (int)((AdType.width) * PT);
  423.  
  424.             int adX = xLocation - (AdType.width/2);
  425.             int adY = yLocation - (AdType.height/2);
  426.  
  427.             AdMobBanner = new BannerView(bannerID, AdType, adX, adY);
  428.  
  429.             // Register the banner ad events
  430.             AdMobBanner.OnAdLoaded += OnReceiveBanner;
  431.             AdMobBanner.OnAdFailedToLoad += OnFailReceiveBanner;
  432.             AdMobBanner.OnAdOpening += OnBannerVisible;
  433.             AdMobBanner.OnAdClosed += OnBannerHidden;
  434.             AdMobBanner.OnAdLeavingApplication += OnBannerClick;
  435.  
  436.             AdMobBanner.LoadAd (GenerateAdRequest ());
  437.  
  438.             BannerInMemoryType = AdType;
  439.             BannerInMemoryLayout = AdLayout;
  440.         } else {
  441.             // 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
  442.             if (AdType == BannerInMemoryType) {
  443.                 if (AdLayout != BannerInMemoryLayout) {
  444.                     // Repositioning the banner directly is not supported by this plugin so we need to destroy the banner and create a new one
  445.                     DestroyBanner ();
  446.  
  447.                     LoadBanner (AdType, AdLayout, DisplayImmediately);
  448.  
  449.                     //RepositionBanner(0, 0, AdLayout);
  450.                 }
  451.  
  452.                 if (DisplayImmediately)
  453.                     ShowBanner ();
  454.             }
  455.         }
  456.     }
  457.  
  458.     private void LoadBanner (bool DisplayImmediately, bool ForcedInternalCall)
  459.     {
  460.         int adWidth = (int)(Screen.width / 2f * GetScaleFactor());
  461.         AdSize rectangleAd = new AdSize(250, 200);
  462.         if (adWidth > 300) rectangleAd = new AdSize(300, 250);
  463.  
  464.         if (ForcedInternalCall) {
  465.             LoadBanner (rectangleAd, AdPosition.TopLeft, false);
  466.             BannerWantedVisible = true;//DisplayImmediately;
  467.         } else {
  468.             LoadBanner (rectangleAd, AdPosition.TopLeft, DisplayImmediately);
  469.         }
  470.     }
  471.  
  472.     /// <summary>
  473.     /// Repositions the banner.
  474.     /// </summary>
  475.     /// <param name="xPos">X position.</param>
  476.     /// <param name="yPos">Y position.</param>
  477.     /// <param name="AdLayout">Admob ad position.</param>
  478.     public void RepositionBanner (int xPos, int yPos, AdPosition AdLayout = AdPosition.TopLeft)
  479.     {
  480.         DebugLog ("RepositionBanner not supported by this plugin!");
  481.         return;
  482.     }
  483.  
  484.     public static Rect RectTransformToScreenSpace (RectTransform transform)
  485.     {
  486.         Vector2 size = Vector2.Scale (transform.rect.size, transform.lossyScale);
  487.         return new Rect (((Vector2)transform.position - (size * 0.5f)).x, ((Vector2)transform.position - (size * 0.5f)).y, size.x, size.y);
  488.     }
  489.  
  490.     public static Rect RectTransformToScreenSpace2(RectTransform transform)
  491.     {
  492.         Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
  493.         Rect rect = new Rect(transform.position.x, Screen.height - transform.position.y, size.x, size.y);
  494.         rect.x -= (transform.pivot.x * size.x);
  495.         rect.y -= ((1.0f - transform.pivot.y) * size.y);
  496.         return rect;
  497.     }
  498.  
  499.     public void showAdOld ()
  500.     {
  501.         if (GameObject.Find ("AdPlaceholder") == true) {
  502.             RectTransform adPlaceholder = GameObject.Find ("AdPlaceholder").GetComponent<RectTransform> ();
  503.             Rect adRect = RectTransformToScreenSpace (adPlaceholder);
  504.             Debug.Log ("ad size1: " + (int)adRect.x + ", " + (int)adRect.y + ", " + (int)adRect.width + ", " + (int)adRect.height);
  505.             Debug.Log ("ad size2: " + rectangleAdX + ", " + rectangleAdY);
  506.             //LoadBanner((int)adRect.x, (int)adRect.y, (int)adRect.width, (int)adRect.height, true);
  507.             Debug.Log ("Placeholder Found");
  508.         } else {
  509.             AdSize rectangleAd = new AdSize ((int)(Screen.width/2f * GetScaleFactor()), (int)(Screen.height / 2f * GetScaleFactor()));
  510.             LoadBanner (rectangleAd, AdPosition.TopLeft, true);
  511.             Debug.Log ("No Placeholder");
  512.         }
  513.         //ShowBanner();
  514.     }
  515.  
  516.     public void showAd ()
  517.     {
  518.         if (Config.NOADS) return;
  519.         if (false){//GameObject.Find ("AdPlaceholder") == true) {
  520.             RectTransform adPlaceholder = GameObject.Find ("AdPlaceholder").GetComponent<RectTransform> ();
  521.             Rect adRect = RectTransformToScreenSpace(adPlaceholder);
  522.             Debug.Log(adRect.x + ", " + adRect.y);
  523.             /*AdSize rectangleAd = new AdSize ((int)((adPlaceholder.rect.width * FindObjectOfType<Canvas> ().scaleFactor) / 2f), (int)((adPlaceholder.rect.height * FindObjectOfType<Canvas> ().scaleFactor) / 2f));
  524.  
  525.             int x = (int)Screen.width / 4;
  526.             int y = (int)Screen.height / 2;
  527.             int width = (int)((adPlaceholder.rect.width * FindObjectOfType<Canvas> ().scaleFactor) / 2f);
  528.             int height = (int)((adPlaceholder.rect.height * FindObjectOfType<Canvas> ().scaleFactor) / 2f);
  529.  
  530.             LoadBanner (new AdSize(width, height), AdPosition.TopLeft, true);
  531.             //LoadBanner(x - width/2, y - height/2, width, height, true);
  532.             Debug.Log ("Placeholder Found");*/
  533.         } else
  534.         {
  535.             ShowBanner();
  536.         }
  537.         //ShowBanner();
  538.     }
  539.  
  540.     public void hideAd ()
  541.     {
  542.         HideBanner ();
  543.         DestroyBanner();
  544.     }
  545.  
  546.     public void loadInterstitial ()
  547.     {
  548.  
  549.     }
  550.  
  551.     public void pauseAd ()
  552.     {
  553.  
  554.     }
  555.  
  556.     public void resumeAd ()
  557.     {
  558.  
  559.     }
  560.  
  561.     public bool isInterstitielLoaded ()
  562.     {
  563.         return true;
  564.     }
  565.  
  566.     public void showInterstitial ()
  567.     {
  568.         ShowInterstitial ();
  569.     }
  570.  
  571.     /// <summary>
  572.     /// Shows a banner advert if one is loaded in memory.
  573.     /// </summary>
  574.     public void ShowBanner ()
  575.     {
  576.         if (!EnableAdMob)
  577.             return;
  578.  
  579.         if (!BannerIsReady && !BannerIsLoading) {
  580.             LoadBanner (true, true);
  581.             return;
  582.         }
  583.  
  584.         // Get the name of the current method
  585.         string MethodName = "ShowBanner";
  586.  
  587.         Debug.Log (MethodName + " called");
  588.  
  589.         // Check if we're calling ShowBanner because we're returning from an overlay screen which hid the banner
  590.         if (BannerOverlayDepth > 0) {
  591.             // Decrease the overlay depth by 1
  592.             BannerOverlayDepth--;
  593.  
  594.             // If the overlay depth is still above 0 then there must still be some overlays open
  595.             if (BannerOverlayDepth > 0)
  596.                 return;
  597.  
  598.             // There isn't any more overlaying menus open, return to the previous banner ad state
  599.             BannerWantedVisible = BannerPrevState;
  600.  
  601.             Debug.Log ("Banner wanted set to prev state: " + BannerPrevState);
  602.         } else {
  603.             BannerWantedVisible = true;
  604.         }
  605.  
  606.         if (!BannerWantedVisible)
  607.             return;
  608.  
  609.         // Update the state items (Values used to determine if the action in this method should be ran)
  610.         ActionState [MethodName].UpdateItems (new List<bool> () { !BannerIsVisible });
  611.  
  612.         // Check if we can perform the action for the current method
  613.         if (CanPerformAction (MethodName, ActionState [MethodName].items)) {
  614.             if (BannerIsReady) {
  615.                 // Show the banner
  616.                 AdMobBanner.Show ();
  617.             } else {
  618.                 LoadBanner (true, true);
  619.             }
  620.         }
  621.     }
  622.  
  623.     /// <summary>
  624.     /// Hides a banner advert, will also cancel a banner advert from showing if one is loaded.
  625.     /// </summary>
  626.     /// <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>
  627.     public void HideBanner (bool IsOverlay = false)
  628.     {
  629.         if (!EnableAdMob)
  630.             return;
  631.  
  632.         // If this is an overlaying screen (e.g backscreen) then we'll want to return to the previous banner state when we close it
  633.         if (IsOverlay) {
  634.             BannerOverlayDepth++;
  635.  
  636.             if (BannerOverlayDepth == 1)
  637.                 BannerPrevState = BannerWantedVisible;
  638.         }
  639.  
  640.         DebugLog ("Hiding banner!");
  641.  
  642.         // Mark wanted visible as false so if the banner ad hasn't loaded yet it'll make sure it isn't shown when loaded
  643.         BannerWantedVisible = false;
  644.         BannerIsVisible = false;
  645.  
  646.         // Hide the banner advert from view (This does not unload it from memory)
  647.         if (AdMobBanner != default (BannerView))
  648.             AdMobBanner.Hide ();
  649.  
  650.     }
  651.  
  652.     /// <summary>
  653.     /// Remove the banner from memory. (Required if you want to load a new banner ad type)
  654.     /// </summary>
  655.     public void DestroyBanner ()
  656.     {
  657.         if (!EnableAdMob)
  658.             return;
  659.  
  660.         // Mark the banner properties as false as the banner is now destroyed
  661.         BannerWantedVisible = false;
  662.         BannerIsLoading = false;
  663.         BannerIsReady = false;
  664.         BannerIsVisible = false;
  665.         BannerInMemoryType = default (AdSize);
  666.         BannerInMemoryLayout = default (AdPosition);
  667.  
  668.         DebugLog ("Destroying banner!");
  669.  
  670.         // Unload the banner from memory
  671.         AdMobBanner.Destroy ();
  672.     }
  673.  
  674.     // Everything past this point is ad listener events //
  675.  
  676.     #region Banner callback handlers
  677.  
  678.     public void OnReceiveBanner (object Sender, EventArgs Args)
  679.     {
  680.         BannerIsReady = true;
  681.         BannerIsLoading = false;
  682.  
  683.         DebugLog ("New banner loaded!");
  684.  
  685.         if (BannerWantedVisible) {
  686.             ShowBanner ();
  687.         } else {
  688.             HideBanner ();
  689.         }
  690.     }
  691.  
  692.     public void OnFailReceiveBanner (object Sender, AdFailedToLoadEventArgs Args)
  693.     {
  694.         BannerIsReady = false;
  695.  
  696.         DebugLog ("Banner failed to load - Error: " + Args.Message);
  697.         GoogleAnalytics.Instance.LogEvent ("AdMob", "Banner Load Failure", "Error: " + Args.Message);
  698.  
  699.         StartCoroutine (RetryBannerLoad ());
  700.     }
  701.  
  702.     private IEnumerator RetryBannerLoad ()
  703.     {
  704.         yield return new WaitForSeconds (5f);
  705.  
  706.         DebugLog ("Retrying banner load!");
  707.  
  708.         if (!BannerIsLoading) {
  709.             BannerInMemoryType = default (AdSize);
  710.             BannerInMemoryLayout = default (AdPosition);
  711.             yield break;
  712.         }
  713.  
  714.         LoadBanner (BannerInMemoryType, BannerInMemoryLayout, false);
  715.     }
  716.  
  717.     public void OnBannerVisible (object Sender, EventArgs Args)
  718.     {
  719.         BannerIsVisible = true;
  720.  
  721.         if (!BannerWantedVisible)
  722.             HideBanner ();
  723.     }
  724.  
  725.     public void OnBannerHidden (object Sender, EventArgs Args)
  726.     {
  727.         BannerIsVisible = false;
  728.  
  729.         if (BannerWantedVisible)
  730.             ShowBanner ();
  731.  
  732.     }
  733.  
  734.     public void OnBannerClick (object Sender, EventArgs Args)
  735.     {
  736.         GoogleAnalytics.Instance.LogEvent ("AdMob", "Banner Clicked", "CLIENT_ID");
  737.     }
  738.  
  739.     #endregion
  740.  
  741.     #region Interstitial callback handlers
  742.  
  743.     public void OnReceiveInterstitial (object Sender, EventArgs Args)
  744.     {
  745.         IntIsReady = true;
  746.         IntIsLoading = false;
  747.  
  748.         if (IntWantedVisible)
  749.             ShowInterstitial ();
  750.     }
  751.  
  752.     public void OnFailedReceiveInterstitial (object Sender, AdFailedToLoadEventArgs Args)
  753.     {
  754.         IntIsReady = false;
  755.         IntIsLoading = false;
  756.  
  757.         DebugLog ("Interstitial failed to load - Error: " + Args.Message);
  758.         GoogleAnalytics.Instance.LogEvent ("AdMob", "Interstitial Load Failure", "Error: " + Args.Message);
  759.     }
  760.  
  761.     public void OnInterstitialVisible (object Sender, EventArgs Args)
  762.     {
  763.         Time.timeScale = 0f;
  764.         IntIsReady = false;
  765.         IntIsVisible = true;
  766.  
  767.         // The interstitial auto hides ads but lets call the usual functions so the variables know what's happening
  768.         HideBanner (true);
  769.     }
  770.  
  771.     public void OnInterstitialHidden (object Sender, EventArgs Args)
  772.     {
  773.         IntIsVisible = false;
  774.  
  775.         if (IntAutoReload)
  776.             LoadInterstitial (false);
  777.         Time.timeScale = 1f;
  778.     }
  779.  
  780.     public void OnInterstitialClick (object Sender, EventArgs Args)
  781.     {
  782.         Time.timeScale = 1f;
  783.         GoogleAnalytics.Instance.LogEvent ("AdMob", "Interstitial Clicked", "CLIENT_ID");
  784.     }
  785.  
  786.     #endregion
  787.  
  788. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top