Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

AdMob - how to display ads on different scenes?

Discussion in 'Scripting' started by groch, May 21, 2015.

  1. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Hello there,

    I am implementing AdMob into my game now and I want to display an Interstitial ad only in specific scenes.

    First thing - I want to display an Interstitial ad when the player will click "Quit" button in Main Menu. There will pop up a question with answers Yes/No and an Interstitial ad above that question.

    Second thing - I want to display also an Interstitial ad, but this time in the High Scores scene.

    How can I do that? I have added this in the script applied to High Scores scene, but nothing shows up:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         RequestInterstitial();
    4.     }
    My 3 scenes:

    1. Main Menu:



    2. Actual game:



    3. High Scores:



    The script looks like that, it's the demo script form the google admob plugin (I have commented out test buttons):

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using GoogleMobileAds;
    4. using GoogleMobileAds.Api;
    5.  
    6. // Example script showing how to invoke the Google Mobile Ads Unity plugin.
    7. public class GoogleMobileAdsDemoScript1 : MonoBehaviour
    8. {
    9.  
    10.     private BannerView bannerView;
    11.     private InterstitialAd interstitial;
    12.  
    13.  
    14.     //void OnGUI()
    15.     //{
    16.     //    // Puts some basic buttons onto the screen.
    17.     //    GUI.skin.button.fontSize = (int)(0.05f * Screen.height);
    18.  
    19.     //    Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
    20.     //                               0.8f * Screen.width, 0.1f * Screen.height);
    21.     //    if (GUI.Button(requestBannerRect, "Request Banner"))
    22.     //    {
    23.     //        RequestBanner();
    24.     //    }
    25.  
    26.     //    Rect showBannerRect = new Rect(0.1f * Screen.width, 0.175f * Screen.height,
    27.     //                                   0.8f * Screen.width, 0.1f * Screen.height);
    28.     //    if (GUI.Button(showBannerRect, "Show Banner"))
    29.     //    {
    30.     //        bannerView.Show();
    31.     //    }
    32.  
    33.     //    Rect hideBannerRect = new Rect(0.1f * Screen.width, 0.3f * Screen.height,
    34.     //                                   0.8f * Screen.width, 0.1f * Screen.height);
    35.     //    if (GUI.Button(hideBannerRect, "Hide Banner"))
    36.     //    {
    37.     //        bannerView.Hide();
    38.     //    }
    39.  
    40.     //    Rect destroyBannerRect = new Rect(0.1f * Screen.width, 0.425f * Screen.height,
    41.     //                                      0.8f * Screen.width, 0.1f * Screen.height);
    42.     //    if (GUI.Button(destroyBannerRect, "Destroy Banner"))
    43.     //    {
    44.     //        bannerView.Destroy();
    45.     //    }
    46.  
    47.     //    Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.55f * Screen.height,
    48.     //                                            0.8f * Screen.width, 0.1f * Screen.height);
    49.     //    if (GUI.Button(requestInterstitialRect, "Request Interstitial"))
    50.     //    {
    51.     //        RequestInterstitial();
    52.     //    }
    53.  
    54.     //    Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.675f * Screen.height,
    55.     //                                         0.8f * Screen.width, 0.1f * Screen.height);
    56.     //    if (GUI.Button(showInterstitialRect, "Show Interstitial"))
    57.     //    {
    58.     //        ShowInterstitial();
    59.     //    }
    60.  
    61.     //    Rect destroyInterstitialRect = new Rect(0.1f * Screen.width, 0.8f * Screen.height,
    62.     //                                            0.8f * Screen.width, 0.1f * Screen.height);
    63.     //    if (GUI.Button(destroyInterstitialRect, "Destroy Interstitial"))
    64.     //    {
    65.     //        interstitial.Destroy();
    66.     //    }
    67.     //}
    68.  
    69.     private void RequestBanner()
    70.     {
    71. #if UNITY_EDITOR
    72.         string adUnitId = "unused";
    73. #elif UNITY_ANDROID
    74.             string adUnitId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    75. #elif UNITY_IPHONE
    76.             string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    77. #else
    78.             string adUnitId = "unexpected_platform";
    79. #endif
    80.  
    81.         // Create a 320x50 banner at the top of the screen.
    82.         bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
    83.         // Register for ad events.
    84.         bannerView.AdLoaded += HandleAdLoaded;
    85.         bannerView.AdFailedToLoad += HandleAdFailedToLoad;
    86.         bannerView.AdOpened += HandleAdOpened;
    87.         bannerView.AdClosing += HandleAdClosing;
    88.         bannerView.AdClosed += HandleAdClosed;
    89.         bannerView.AdLeftApplication += HandleAdLeftApplication;
    90.         // Load a banner ad.
    91.         bannerView.LoadAd(createAdRequest());
    92.     }
    93.  
    94.     private void RequestInterstitial()
    95.     {
    96. #if UNITY_EDITOR
    97.         string adUnitId = "unused";
    98. #elif UNITY_ANDROID
    99.             string adUnitId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    100. #elif UNITY_IPHONE
    101.             string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
    102. #else
    103.             string adUnitId = "unexpected_platform";
    104. #endif
    105.  
    106.         // Create an interstitial.
    107.         interstitial = new InterstitialAd(adUnitId);
    108.         // Register for ad events.
    109.         interstitial.AdLoaded += HandleInterstitialLoaded;
    110.         interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad;
    111.         interstitial.AdOpened += HandleInterstitialOpened;
    112.         interstitial.AdClosing += HandleInterstitialClosing;
    113.         interstitial.AdClosed += HandleInterstitialClosed;
    114.         interstitial.AdLeftApplication += HandleInterstitialLeftApplication;
    115.         // Load an interstitial ad.
    116.         interstitial.LoadAd(createAdRequest());
    117.     }
    118.  
    119.     // Returns an ad request with custom ad targeting.
    120.     private AdRequest createAdRequest()
    121.     {
    122.         return new AdRequest.Builder()
    123.                 .AddTestDevice(AdRequest.TestDeviceSimulator)
    124.                 .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
    125.                 .AddKeyword("game")
    126.                 .SetGender(Gender.Male)
    127.                 .SetBirthday(new DateTime(1985, 1, 1))
    128.                 .TagForChildDirectedTreatment(false)
    129.                 .AddExtra("color_bg", "9B30FF")
    130.                 .Build();
    131.  
    132.     }
    133.  
    134.     private void ShowInterstitial()
    135.     {
    136.         if (interstitial.IsLoaded())
    137.         {
    138.             interstitial.Show();
    139.         }
    140.         else
    141.         {
    142.             print("Interstitial is not ready yet.");
    143.         }
    144.     }
    145.  
    146.     #region Banner callback handlers
    147.  
    148.     public void HandleAdLoaded(object sender, EventArgs args)
    149.     {
    150.         print("HandleAdLoaded event received.");
    151.     }
    152.  
    153.     public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    154.     {
    155.         print("HandleFailedToReceiveAd event received with message: " + args.Message);
    156.     }
    157.  
    158.     public void HandleAdOpened(object sender, EventArgs args)
    159.     {
    160.         print("HandleAdOpened event received");
    161.     }
    162.  
    163.     void HandleAdClosing(object sender, EventArgs args)
    164.     {
    165.         print("HandleAdClosing event received");
    166.     }
    167.  
    168.     public void HandleAdClosed(object sender, EventArgs args)
    169.     {
    170.         print("HandleAdClosed event received");
    171.     }
    172.  
    173.     public void HandleAdLeftApplication(object sender, EventArgs args)
    174.     {
    175.         print("HandleAdLeftApplication event received");
    176.     }
    177.  
    178.     #endregion
    179.  
    180.     #region Interstitial callback handlers
    181.  
    182.     public void HandleInterstitialLoaded(object sender, EventArgs args)
    183.     {
    184.         print("HandleInterstitialLoaded event received.");
    185.     }
    186.  
    187.     public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    188.     {
    189.         print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
    190.     }
    191.  
    192.     public void HandleInterstitialOpened(object sender, EventArgs args)
    193.     {
    194.         print("HandleInterstitialOpened event received");
    195.     }
    196.  
    197.     void HandleInterstitialClosing(object sender, EventArgs args)
    198.     {
    199.         print("HandleInterstitialClosing event received");
    200.     }
    201.  
    202.     public void HandleInterstitialClosed(object sender, EventArgs args)
    203.     {
    204.         print("HandleInterstitialClosed event received");
    205.     }
    206.  
    207.     public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    208.     {
    209.         print("HandleInterstitialLeftApplication event received");
    210.     }
    211.  
    212.     #endregion
    213. }
     
    Last edited: May 21, 2015
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    I guess you missed this part of your script, where the script tries to get an ad, but you did not put in any:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.         string adUnitId = "unused";
    3. #elif UNITY_ANDROID
    4.             string adUnitId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    5. #elif UNITY_IPHONE
    6.             string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
    7. #else
    8.             string adUnitId = "unexpected_platform";
    9. #endif
     
  3. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Heh, I've just hid my ads ID by typing "xxxx..." just in case. But I've just fixed the whole problem from my first post by mixing ad script with main menu buttons etc., so on the start I'm requesting ads and showing them after clicking High Scores from main menu and then hiding and destroying.

    But now I have another problem - when fullscreen ad shows up and when you clikc "x" to close it, then you can see only black screen and can't do anything.
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Maybe you can provide a screenshot or something of your scene and hirarchy view, so we know, if the ad script is disabling more than just itself or if there is still something on the adscript that is not destroyed? :)
     
  5. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Thank you for replying, it's all good now, I have changed the moment when that ad shows up and now it's fine and even without fps drop. :)