Search Unity

Question Google AdMob rewarded ads can work in editor but not in simulator

Discussion in 'Scripting' started by Vision222114, Apr 7, 2021.

  1. Vision222114

    Vision222114

    Joined:
    Oct 14, 2014
    Posts:
    17
    Hi

    I use Google AdMob
    and follow guide
    it is show test ad in editor

    but when I export apk
    and install at bluestack
    the rewarded ads can't show

    and I found rewardedAd.IsLoaded() always return false
    but I dont kwno why
    anyone knows?

    I had test banner
    it can show editor and bluestack

    this is my code

    Code (CSharp):
    1. using UnityEngine;
    2. using GoogleMobileAds.Api;
    3. using System;
    4. using UnityEngine.UI;
    5.  
    6. public class GAdModApiManager : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         Debug.Log("Start");
    12.         ManualInit();
    13.     }
    14.  
    15.     public void ManualInit()
    16.     {
    17.         Debug.Log("ManualInit");
    18.         MobileAds.Initialize(initStatus =>
    19.         {
    20.             Debug.Log("MobAds Status : " + initStatus);
    21.             Debug.Log("AdMob Start");
    22.  
    23.             RequestBanner();
    24.             RequestRewardedAd();
    25.         });
    26.     }
    27.  
    28. #region
    29.     private RewardedAd rewardedAd;
    30.     private void RequestRewardedAd()
    31.     {
    32. #if UNITY_ANDROID
    33.         string adUnitId = "rewardedid";
    34. #else
    35.         string adUnitId = "unexpected_platform";
    36. #endif
    37.         this.rewardedAd = new RewardedAd(adUnitId);
    38.  
    39.         Debug.Log(adUnitId);
    40.         // Called when an ad request has successfully loaded.
    41.         this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
    42.         // Called when an ad request failed to load.
    43.         this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
    44.         // Called when an ad is shown.
    45.         this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
    46.         // Called when an ad request failed to show.
    47.         this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
    48.         // Called when the user should be rewarded for interacting with the ad.
    49.         this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
    50.         // Called when the ad is closed.
    51.         this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;
    52.  
    53.         Debug.Log("Request Ad");
    54.         // Create an empty ad request.
    55.         //AdRequest request = new AdRequest.Builder().Build();
    56.         AdRequest request = new AdRequest.Builder()
    57.         .AddTestDevice(AdRequest.TestDeviceSimulator)       // Simulator.
    58.         .AddTestDevice(SystemInfo.deviceUniqueIdentifier)  // My test device.
    59.         .Build();
    60.         // Load the rewarded ad with the request.
    61.         Debug.Log("Load Rewarded Ad");
    62.         this.rewardedAd.LoadAd(request);
    63.     }
    64.  
    65.     public void ShowRewardAd()
    66.     {
    67.         Debug.Log("Is Loaded : " + this.rewardedAd.IsLoaded());
    68.         if (this.rewardedAd.IsLoaded())
    69.         {
    70.             this.rewardedAd.Show();
    71.         }
    72.     }
    73.  
    74.     public void HandleRewardedAdLoaded(object sender, EventArgs args)
    75.     {
    76.         Debug.LogWarning("reward ad load");
    77.     }
    78.  
    79.     public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args)
    80.     {
    81.         Debug.LogWarning("reward ad load fail : " + args.Message);
    82.     }
    83.  
    84.     public void HandleRewardedAdOpening(object sender, EventArgs args)
    85.     {
    86.         Debug.LogWarning("reward ad opening");
    87.     }
    88.  
    89.     public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
    90.     {
    91.         Debug.LogWarning("reward ad open fail");
    92.     }
    93.  
    94.     public void HandleRewardedAdClosed(object sender, EventArgs args)
    95.     {
    96.         //RequestAd();
    97.     }
    98.  
    99.     public void HandleUserEarnedReward(object sender, Reward args)
    100.     {
    101.         string type = args.Type;
    102.         double amount = args.Amount;
    103.     }
    104. #endregion
    105.  
    106. #region Banner
    107.     private BannerView bannerView;
    108.     private void RequestBanner()
    109.     {
    110.  
    111.         Debug.LogWarning("Start Request Banner Ad");
    112. #if UNITY_ANDROID
    113.         string adUnitId = "bannerid";
    114. #else
    115.         string adUnitId = "unexpected_platform";
    116. #endif
    117.  
    118.         // Create a 320x50 banner at the top of the screen.
    119.         this.bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
    120.  
    121.         // Called when an ad request has successfully loaded.
    122.         this.bannerView.OnAdLoaded += this.HandleOnAdLoaded;
    123.         // Called when an ad request failed to load.
    124.         this.bannerView.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
    125.         // Called when an ad is clicked.
    126.         this.bannerView.OnAdOpening += this.HandleOnAdOpened;
    127.         // Called when the user returned from the app after an ad click.
    128.         this.bannerView.OnAdClosed += this.HandleOnAdClosed;
    129.         // Called when the ad click caused the user to leave the application.
    130.         this.bannerView.OnAdLeavingApplication += this.HandleOnAdLeavingApplication;
    131.  
    132.         // Create an empty ad request.
    133.         AdRequest request = new AdRequest.Builder().Build();
    134.  
    135.         // Load the banner with the request.
    136.         this.bannerView.LoadAd(request);
    137.     }
    138.  
    139.     public void HandleOnAdLoaded(object sender, EventArgs args)
    140.     {
    141.         MonoBehaviour.print("HandleAdLoaded event received");
    142.     }
    143.  
    144.     public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    145.     {
    146.         MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "
    147.                             + args.Message);
    148.     }
    149.  
    150.     public void HandleOnAdOpened(object sender, EventArgs args)
    151.     {
    152.         MonoBehaviour.print("HandleAdOpened event received");
    153.     }
    154.  
    155.     public void HandleOnAdClosed(object sender, EventArgs args)
    156.     {
    157.         MonoBehaviour.print("HandleAdClosed event received");
    158.     }
    159.  
    160.     public void HandleOnAdLeavingApplication(object sender, EventArgs args)
    161.     {
    162.         MonoBehaviour.print("HandleAdLeavingApplication event received");
    163.     }
    164. #endregion
    165. }
    166.  
    Work Environment
    unity version 2019.3.14f1
    mac os big sur 11.2.1
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Did you look in the device console logs? There might be additional information there from the Admob API, such as if it was unable to start up, or if there were no ads, stuff like that.
     
  3. Vision222114

    Vision222114

    Joined:
    Oct 14, 2014
    Posts:
    17
    Yes
    but I dont see any error
    even no trigger OnAdFailedToLoad or OnAdLoaded
     
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Hi

    It looks as it's some additional steps required to get the test ads on a device:

    1. Check the console or logcat output for a message that looks like this:

      iOS
    2. <Google> To get test ads on this device, set:
      GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers =
      @[ @"2077ef9a63d2b398840261c8221a0c9b" ];
    3. Copy your alphanumeric test device ID to your clipboard.

    4. Add the test device ID to the created list.

      deviceIds.Add("2077ef9a63d2b398840261c8221a0c9b");

    https://developers.google.com/admob/unity/test-ads
     
  5. Vision222114

    Vision222114

    Joined:
    Oct 14, 2014
    Posts:
    17

    Actually I didn't see
    To get test ads on this device, set:
    GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers =
    @[ @"2077ef9a63d2b398840261c8221a0c9b" ];
    in console

    and simulator is not release environment?
     
  6. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Guess you have to install it, and read the log from the device.
     
    Last edited: Apr 8, 2021
  7. Vision222114

    Vision222114

    Joined:
    Oct 14, 2014
    Posts:
    17
    Finally I find the problem

    I input error ad id

    thanks everyone!