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 ads are not showing in my Android game

Discussion in 'Android' started by subzer0, Apr 6, 2016.

  1. subzer0

    subzer0

    Joined:
    Dec 10, 2010
    Posts:
    94
    Hello,

    I am trying to integrate AdMob to my Android Game.

    I am not getting any errors in the editor, but when I try to open the game in my phone, I am getting:

    Unfortunately yourGame has stopped.

    I am using Unity 5.3.4f1 Personal, and also updated my SDK.

    This is the demo script that comes with the Google Mobile Ads Sample project found on GitHub.

    I created an empty scene and attached this script to the Main camera and then build just this scene.

    I am sorry if this is a very long script, but I thought it would be better if I add it here.:D

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using GoogleMobileAds;
    4. using GoogleMobileAds.Api;
    5.  
    6. public class GoogleMobileAdsDemoHandler : IDefaultInAppPurchaseProcessor
    7. {
    8.     private readonly string[] validSkus = { "android.test.purchased" };
    9.  
    10.     //Will only be sent on a success.
    11.     public void ProcessCompletedInAppPurchase(IInAppPurchaseResult result)
    12.     {
    13.         result.FinishPurchase();
    14.         GoogleMobileAdsDemoScript.OutputMessage = "Purchase Succeeded! Credit user here.";
    15.     }
    16.  
    17.     //Check SKU against valid SKUs.
    18.     public bool IsValidPurchase(string sku)
    19.     {
    20.         foreach(string validSku in validSkus)
    21.         {
    22.             if (sku == validSku)
    23.             {
    24.                 return true;
    25.             }
    26.         }
    27.         return false;
    28.     }
    29.  
    30.     //Return the app's public key.
    31.     public string AndroidPublicKey
    32.     {
    33.         //In a real app, return public key instead of null.
    34.         get { return null; }
    35.     }
    36. }
    37.  
    38. // Example script showing how to invoke the Google Mobile Ads Unity plugin.
    39. public class GoogleMobileAdsDemoScript : MonoBehaviour
    40. {
    41.  
    42.     private BannerView bannerView;
    43.     private InterstitialAd interstitial;
    44.     private RewardBasedVideoAd rewardBasedVideo;
    45.     private float deltaTime = 0.0f;
    46.     private static string outputMessage = "";
    47.  
    48.     public static string OutputMessage
    49.     {
    50.         set { outputMessage = value; }
    51.     }
    52.  
    53.     void Start()
    54.     {
    55.         // Get singleton reward based video ad reference.
    56.         rewardBasedVideo = RewardBasedVideoAd.Instance;
    57.  
    58.         // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
    59.         rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
    60.         rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
    61.         rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
    62.         rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
    63.         rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
    64.         rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
    65.         rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
    66.     }
    67.  
    68.     void Update()
    69.     {
    70.         // Calculate simple moving average for time to render screen. 0.1 factor used as smoothing
    71.         // value.
    72.         deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
    73.     }
    74.  
    75.     void OnGUI()
    76.     {
    77.         GUIStyle style = new GUIStyle();
    78.  
    79.         Rect rect = new Rect(0, 0, Screen.width, Screen.height);
    80.         style.alignment = TextAnchor.LowerRight;
    81.         style.fontSize = (int)(Screen.height * 0.06);
    82.         style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
    83.         float fps = 1.0f / deltaTime;
    84.         string text = string.Format("{0:0.} fps", fps);
    85.         GUI.Label(rect, text, style);
    86.  
    87.         // Puts some basic buttons onto the screen.
    88.         GUI.skin.button.fontSize = (int)(0.03f * Screen.height);
    89.  
    90.         Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
    91.                                      0.8f * Screen.width, 0.1f * Screen.height);
    92.         if (GUI.Button(requestBannerRect, "Request Banner"))
    93.         {
    94.             RequestBanner();
    95.         }
    96.  
    97.         Rect showBannerRect = new Rect(0.1f * Screen.width, 0.175f * Screen.height,
    98.                                   0.8f * Screen.width, 0.1f * Screen.height);
    99.         if (GUI.Button(showBannerRect, "Show Banner"))
    100.         {
    101.             bannerView.Show();
    102.         }
    103.  
    104.         Rect destroyBannerRect = new Rect(0.1f * Screen.width, 0.3f * Screen.height,
    105.                                      0.8f * Screen.width, 0.1f * Screen.height);
    106.         if (GUI.Button(destroyBannerRect, "Destroy Banner"))
    107.         {
    108.             bannerView.Destroy();
    109.         }
    110.  
    111.         Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.425f * Screen.height,
    112.                                            0.8f * Screen.width, 0.1f * Screen.height);
    113.         if (GUI.Button(requestInterstitialRect, "Request Interstitial"))
    114.         {
    115.             RequestInterstitial();
    116.         }
    117.  
    118.         Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.55f * Screen.height,
    119.                                         0.8f * Screen.width, 0.1f * Screen.height);
    120.         if (GUI.Button(showInterstitialRect, "Show Interstitial"))
    121.         {
    122.             ShowInterstitial();
    123.         }
    124.  
    125.         Rect requestRewardedRect = new Rect(0.1f * Screen.width, 0.675f * Screen.height,
    126.                                        0.8f * Screen.width, 0.1f * Screen.height);
    127.         if (GUI.Button(requestRewardedRect, "Request Rewarded Video"))
    128.         {
    129.             RequestRewardBasedVideo();
    130.         }
    131.  
    132.         Rect showRewardedRect = new Rect(0.1f * Screen.width, 0.8f * Screen.height,
    133.                                     0.8f * Screen.width, 0.1f * Screen.height);
    134.         if (GUI.Button(showRewardedRect, "Show Rewarded Video"))
    135.         {
    136.             ShowRewardBasedVideo();
    137.         }
    138.  
    139.         Rect textOutputRect = new Rect(0.1f * Screen.width, 0.925f * Screen.height,
    140.                                   0.8f * Screen.width, 0.05f * Screen.height);
    141.         GUI.Label(textOutputRect, outputMessage);
    142.     }
    143.  
    144.     private void RequestBanner()
    145.     {
    146.         #if UNITY_EDITOR
    147.             string adUnitId = "unused";
    148.         #elif UNITY_ANDROID
    149.             string adUnitId = "INSERT_ANDROID_BANNER_AD_UNIT_ID_HERE";
    150.         #elif UNITY_IPHONE
    151.             string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    152.         #else
    153.             string adUnitId = "unexpected_platform";
    154.         #endif
    155.  
    156.         // Create a 320x50 banner at the top of the screen.
    157.         bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
    158.         // Register for ad events.
    159.         bannerView.OnAdLoaded += HandleAdLoaded;
    160.         bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
    161.         bannerView.OnAdLoaded += HandleAdOpened;
    162.         bannerView.OnAdClosed += HandleAdClosed;
    163.         bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
    164.         // Load a banner ad.
    165.         bannerView.LoadAd(createAdRequest());
    166.     }
    167.  
    168.     private void RequestInterstitial()
    169.     {
    170.         #if UNITY_EDITOR
    171.             string adUnitId = "unused";
    172.         #elif UNITY_ANDROID
    173.             string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxxx";
    174.         #elif UNITY_IPHONE
    175.             string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
    176.         #else
    177.             string adUnitId = "unexpected_platform";
    178.         #endif
    179.  
    180.         // Create an interstitial.
    181.         interstitial = new InterstitialAd(adUnitId);
    182.         // Register for ad events.
    183.         interstitial.OnAdLoaded += HandleInterstitialLoaded;
    184.         interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
    185.         interstitial.OnAdOpening += HandleInterstitialOpened;
    186.         interstitial.OnAdClosed += HandleInterstitialClosed;
    187.         interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
    188.         // Load an interstitial ad.
    189.         interstitial.LoadAd(createAdRequest());
    190.     }
    191.  
    192.     // Returns an ad request with custom ad targeting.
    193.     private AdRequest createAdRequest()
    194.     {
    195.         return new AdRequest.Builder()
    196.                 .AddTestDevice(AdRequest.TestDeviceSimulator)
    197.                 .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
    198.                 .AddKeyword("game")
    199.                 .SetGender(Gender.Male)
    200.                 .SetBirthday(new DateTime(1985, 1, 1))
    201.                 .TagForChildDirectedTreatment(false)
    202.                 .AddExtra("color_bg", "9B30FF")
    203.                 .Build();
    204.     }
    205.  
    206.     private void RequestRewardBasedVideo()
    207.     {
    208.         #if UNITY_EDITOR
    209.             string adUnitId = "unused";
    210.         #elif UNITY_ANDROID
    211.             string adUnitId = "INSERT_ANDROID_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
    212.         #elif UNITY_IPHONE
    213.             string adUnitId = "INSERT_IOS_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
    214.         #else
    215.             string adUnitId = "unexpected_platform";
    216.         #endif
    217.  
    218.         rewardBasedVideo.LoadAd(createAdRequest(), adUnitId);
    219.     }
    220.  
    221.     private void ShowInterstitial()
    222.     {
    223.         if (interstitial.IsLoaded())
    224.         {
    225.             interstitial.Show();
    226.         }
    227.         else
    228.         {
    229.             print("Interstitial is not ready yet.");
    230.         }
    231.     }
    232.  
    233.     private void ShowRewardBasedVideo()
    234.     {
    235.         if (rewardBasedVideo.IsLoaded())
    236.         {
    237.             rewardBasedVideo.Show();
    238.         } else
    239.         {
    240.             print("Reward based video ad is not ready yet.");
    241.         }
    242.     }
    243.  
    244.     #region Banner callback handlers
    245.  
    246.     public void HandleAdLoaded(object sender, EventArgs args)
    247.     {
    248.         print("HandleAdLoaded event received.");
    249.     }
    250.  
    251.     public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    252.     {
    253.         print("HandleFailedToReceiveAd event received with message: " + args.Message);
    254.     }
    255.  
    256.     public void HandleAdOpened(object sender, EventArgs args)
    257.     {
    258.         print("HandleAdOpened event received");
    259.     }
    260.  
    261.     void HandleAdClosing(object sender, EventArgs args)
    262.     {
    263.         print("HandleAdClosing event received");
    264.     }
    265.  
    266.     public void HandleAdClosed(object sender, EventArgs args)
    267.     {
    268.         print("HandleAdClosed event received");
    269.     }
    270.  
    271.     public void HandleAdLeftApplication(object sender, EventArgs args)
    272.     {
    273.         print("HandleAdLeftApplication event received");
    274.     }
    275.  
    276.     #endregion
    277.  
    278.     #region Interstitial callback handlers
    279.  
    280.     public void HandleInterstitialLoaded(object sender, EventArgs args)
    281.     {
    282.         print("HandleInterstitialLoaded event received.");
    283.     }
    284.  
    285.     public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    286.     {
    287.         print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
    288.     }
    289.  
    290.     public void HandleInterstitialOpened(object sender, EventArgs args)
    291.     {
    292.         print("HandleInterstitialOpened event received");
    293.     }
    294.  
    295.     void HandleInterstitialClosing(object sender, EventArgs args)
    296.     {
    297.         print("HandleInterstitialClosing event received");
    298.     }
    299.  
    300.     public void HandleInterstitialClosed(object sender, EventArgs args)
    301.     {
    302.         print("HandleInterstitialClosed event received");
    303.     }
    304.  
    305.     public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    306.     {
    307.         print("HandleInterstitialLeftApplication event received");
    308.     }
    309.  
    310.     #endregion
    311.  
    312.     #region RewardBasedVideo callback handlers
    313.  
    314.     public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    315.     {
    316.         print("HandleRewardBasedVideoLoaded event received.");
    317.     }
    318.  
    319.     public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    320.     {
    321.         print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
    322.     }
    323.  
    324.     public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
    325.     {
    326.         print("HandleRewardBasedVideoOpened event received");
    327.     }
    328.  
    329.     public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
    330.     {
    331.         print("HandleRewardBasedVideoStarted event received");
    332.     }
    333.  
    334.     public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    335.     {
    336.         print("HandleRewardBasedVideoClosed event received");
    337.     }
    338.  
    339.     public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    340.     {
    341.         string type = args.Type;
    342.         double amount = args.Amount;
    343.         print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " +
    344.                 type);
    345.     }
    346.  
    347.     public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
    348.     {
    349.         print("HandleRewardBasedVideoLeftApplication event received");
    350.     }
    351.  
    352.     #endregion
    353. }
    354.  
     
    Last edited: Apr 7, 2016
  2. subzer0

    subzer0

    Joined:
    Dec 10, 2010
    Posts:
    94
    Ok I fixed it by deleting the code inside Start(), and now my game opens without errors.

    But when I try to show an Interstitial ad, it is not showing. The adUnit in my code is the same as the one in my AdMob account.

    What could be the problem ?
     
  3. subzer0

    subzer0

    Joined:
    Dec 10, 2010
    Posts:
    94
    Can anyone help me please ? :(
     
  4. maiko

    maiko

    Joined:
    Sep 18, 2012
    Posts:
    2
    Have you ever got this working? It's amazing how this just doesn't work properly.
     
  5. subzer0

    subzer0

    Joined:
    Dec 10, 2010
    Posts:
    94
    @maiko
    Sorry for the late reply, honestly I didn't find a solution, and I shifted to Unity Ads:D