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

Resolved Admob Ads Don't working

Discussion in '2D' started by robisoftware, Jun 3, 2020.

  1. robisoftware

    robisoftware

    Joined:
    Jan 22, 2020
    Posts:
    23
    Hello . I have a problem with Admob Ads , when I build the project and install my on my android device, ads not appearing . But in editor console i receive the following message .


    In game , when i press button , i receive the following message "The add is not available for the moment " ,
    from OnClick () . I try all my best before posting here , i already try to update GoogleMobileAds plugin .


    Also i make a lot of test on my android device , but it's seems i have only request , and the ads don't show . Banner , interstitial , and rewarded
    Thank you for you help .


    Code (CSharp):
    1. using GoogleMobileAds.Api;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class RewardedVideoButton : MonoBehaviour
    7. {
    8.     private const string ACTION_NAME = "rewarded_video";
    9.  
    10.     private void Start()
    11.     {
    12.         Timer.Schedule(this, 0.1f, AddEvents);
    13.     }
    14.  
    15.     private void AddEvents()
    16.     {
    17. #if UNITY_ANDROID || UNITY_IOS
    18.         if (AdmobController.instance.rewardBasedVideo != null)
    19.         {
    20.             AdmobController.instance.rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
    21.         }
    22. #endif
    23.     }
    24.  
    25.     public void OnClick()
    26.     {
    27.         if (IsAvailableToShow())
    28.             AdmobController.instance.ShowRewardBasedVideo();
    29.         else if (!IsAdAvailable())
    30.             Toast.instance.ShowMessage("Ad is not available at the moment");
    31.         else
    32.         {
    33.             int remainTime = (int)(GameConfig.instance.rewardedVideoPeriod - CUtils.GetActionDeltaTime(ACTION_NAME));
    34.             Toast.instance.ShowMessage("Please wait " + remainTime + " seconds for the next ad");
    35.         }
    36.     }
    37.  
    38.     public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    39.     {
    40.  
    41.     }
    42.  
    43.     public bool IsAvailableToShow()
    44.     {
    45.         return IsActionAvailable() && IsAdAvailable();
    46.     }
    47.  
    48.     private bool IsActionAvailable()
    49.     {
    50.         return CUtils.IsActionAvailable(ACTION_NAME, GameConfig.instance.rewardedVideoPeriod);
    51.     }
    52.  
    53.     private bool IsAdAvailable()
    54.     {
    55.         if (AdmobController.instance.rewardBasedVideo == null) return false;
    56.         bool isLoaded = AdmobController.instance.rewardBasedVideo.IsLoaded();
    57.         return isLoaded;
    58.     }
    59.  
    60.     private void OnDestroy()
    61.     {
    62. #if UNITY_ANDROID || UNITY_IOS
    63.         if (AdmobController.instance.rewardBasedVideo != null)
    64.         {
    65.             AdmobController.instance.rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
    66.         }
    67. #endif
    68.     }
    69. }
    70.  
    I will try to show a few code .
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameConfig : MonoBehaviour
    6. {
    7.     [Header("Android")]
    8.     public string admobAndroidInterstitial;
    9.     public string admobAndroidRewardedVideo;
    10.  
    11.     [Header("iOS")]
    12.     public string admobIOSInterstitial;
    13.     public string admobIOSRewardedVideo;
    14.  
    15.     [Header("Other Settings")]
    16.     public int rewardedVideoPeriod;
    17.     public int rewardedVideoAmount;
    18.  
    19.     public int adPeriod;
    20.  
    21.     public static GameConfig instance;
    22.  
    23.     private void Awake()
    24.     {
    25.         if (instance == null) instance = this;
    26.     }
    27. }
    28.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using GoogleMobileAds;
    4. using GoogleMobileAds.Api;
    5.  
    6. [System.Serializable]
    7. public class AdmobController : MonoBehaviour
    8. {
    9.     private BannerView bannerView;
    10.     public InterstitialAd interstitial;
    11.     public RewardBasedVideoAd rewardBasedVideo;
    12.  
    13.     public static AdmobController instance;
    14.  
    15.     private void Awake()
    16.     {
    17.         if (instance == null) instance = this;
    18.     }
    19.  
    20.     private void Start()
    21.     {
    22.         RequestBanner();
    23.         RequestInterstitial();
    24.  
    25.         InitRewardedVideo();
    26.         RequestRewardBasedVideo();
    27.     }
    28.  
    29.     private void InitRewardedVideo()
    30.     {
    31.         // Get singleton reward based video ad reference.
    32.         this.rewardBasedVideo = RewardBasedVideoAd.Instance;
    33.  
    34.         // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
    35.         this.rewardBasedVideo.OnAdLoaded += this.HandleRewardBasedVideoLoaded;
    36.         this.rewardBasedVideo.OnAdFailedToLoad += this.HandleRewardBasedVideoFailedToLoad;
    37.         this.rewardBasedVideo.OnAdOpening += this.HandleRewardBasedVideoOpened;
    38.         this.rewardBasedVideo.OnAdStarted += this.HandleRewardBasedVideoStarted;
    39.         this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
    40.         this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
    41.         this.rewardBasedVideo.OnAdLeavingApplication += this.HandleRewardBasedVideoLeftApplication;
    42.     }
    43.  
    44.     public void RequestBanner()
    45.     {
    46.         // These ad units are configured to always serve test ads.
    47. #if UNITY_EDITOR
    48.         string adUnitId = "unused";
    49. #elif UNITY_ANDROID
    50.         string adUnitId = "";
    51. #elif UNITY_IPHONE
    52.         string adUnitId = "");
    53. #else
    54.         string adUnitId = "unexpected_platform";
    55. #endif
    56.         // Create a 320x50 banner at the top of the screen.
    57.         this.bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
    58.  
    59.         // Register for ad events.
    60.         this.bannerView.OnAdLoaded += this.HandleAdLoaded;
    61.         this.bannerView.OnAdFailedToLoad += this.HandleAdFailedToLoad;
    62.         this.bannerView.OnAdOpening += this.HandleAdOpened;
    63.         this.bannerView.OnAdClosed += this.HandleAdClosed;
    64.         this.bannerView.OnAdLeavingApplication += this.HandleAdLeftApplication;
    65.  
    66.         // Load a banner ad.
    67.         this.bannerView.LoadAd(this.CreateAdRequest());
    68.     }
    69.  
    70.     public void RequestInterstitial()
    71.     {
    72.         // These ad units are configured to always serve test ads.
    73. #if UNITY_EDITOR
    74.         string adUnitId = "unused";
    75. #elif UNITY_ANDROID
    76.         string adUnitId = GameConfig.instance.admobAndroidInterstitial.Trim();
    77. #elif UNITY_IPHONE
    78.         string adUnitId = GameConfig.instance.admobIOSInterstitial.Trim();
    79. #else
    80.         string adUnitId = "unexpected_platform";
    81. #endif
    82.  
    83.         // Create an interstitial.
    84.         this.interstitial = new InterstitialAd(adUnitId);
    85.  
    86.         // Register for ad events.
    87.         this.interstitial.OnAdLoaded += this.HandleInterstitialLoaded;
    88.         this.interstitial.OnAdFailedToLoad += this.HandleInterstitialFailedToLoad;
    89.         this.interstitial.OnAdOpening += this.HandleInterstitialOpened;
    90.         this.interstitial.OnAdClosed += this.HandleInterstitialClosed;
    91.         this.interstitial.OnAdLeavingApplication += this.HandleInterstitialLeftApplication;
    92.  
    93.         // Load an interstitial ad.
    94.         this.interstitial.LoadAd(this.CreateAdRequest());
    95.     }
    96.  
    97.     public void RequestRewardBasedVideo()
    98.     {
    99. #if UNITY_EDITOR
    100.         string adUnitId = "unused";
    101. #elif UNITY_ANDROID
    102.         string adUnitId = GameConfig.instance.admobAndroidRewardedVideo.Trim();
    103. #elif UNITY_IPHONE
    104.         string adUnitId = GameConfig.instance.admobIOSRewardedVideo.Trim();
    105. #else
    106.         string adUnitId = "unexpected_platform";
    107. #endif
    108.  
    109.         this.rewardBasedVideo.LoadAd(this.CreateAdRequest(), adUnitId);
    110.     }
    111.  
    112.     // Returns an ad request with custom ad targeting.
    113.     private AdRequest CreateAdRequest()
    114.     {
    115.         return new AdRequest.Builder()
    116.                 .AddTestDevice(AdRequest.TestDeviceSimulator)
    117.                 .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
    118.                 .AddKeyword("game")
    119.                 .TagForChildDirectedTreatment(false)
    120.                 .AddExtra("color_bg", "9B30FF")
    121.                 .Build();
    122.     }
    123.  
    124.     public void ShowInterstitial(InterstitialAd ad)
    125.     {
    126.         if (ad != null && ad.IsLoaded())
    127.         {
    128.             ad.Show();
    129.         }
    130.     }
    131.  
    132.     public void ShowBanner()
    133.     {
    134.         if (bannerView != null)
    135.         {
    136.             bannerView.Show();
    137.         }
    138.     }
    139.  
    140.     public void HideBanner()
    141.     {
    142.         if (bannerView != null)
    143.         {
    144.             bannerView.Hide();
    145.         }
    146.     }
    147.  
    148.     public bool ShowInterstitial()
    149.     {
    150.         if (interstitial != null && interstitial.IsLoaded())
    151.         {
    152.             interstitial.Show();
    153.             return true;
    154.         }
    155.         return false;
    156.     }
    157.  
    158.     public void ShowRewardBasedVideo()
    159.     {
    160.         if (this.rewardBasedVideo.IsLoaded())
    161.         {
    162.             this.rewardBasedVideo.Show();
    163.         }
    164.         else
    165.         {
    166.             MonoBehaviour.print("Reward based video ad is not ready yet");
    167.         }
    168.     }
    169.  
    170.     #region Banner callback handlers
    171.  
    172.     public void HandleAdLoaded(object sender, EventArgs args)
    173.     {
    174.         HideBanner();
    175.         print("HandleAdLoaded event received.");
    176.     }
    177.  
    178.     public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    179.     {
    180.         print("HandleFailedToReceiveAd event received with message: " + args.Message);
    181.     }
    182.  
    183.     public void HandleAdOpened(object sender, EventArgs args)
    184.     {
    185.         print("HandleAdOpened event received");
    186.     }
    187.  
    188.     public void HandleAdClosed(object sender, EventArgs args)
    189.     {
    190.         print("HandleAdClosed event received");
    191.     }
    192.  
    193.     public void HandleAdLeftApplication(object sender, EventArgs args)
    194.     {
    195.         print("HandleAdLeftApplication event received");
    196.     }
    197.  
    198.     #endregion
    199.  
    200.     #region Interstitial callback handlers
    201.  
    202.     public void HandleInterstitialLoaded(object sender, EventArgs args)
    203.     {
    204.         print("HandleInterstitialLoaded event received.");
    205.     }
    206.  
    207.     public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    208.     {
    209.         print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
    210.     }
    211.  
    212.     public void HandleInterstitialOpened(object sender, EventArgs args)
    213.     {
    214.         print("HandleInterstitialOpened event received");
    215.     }
    216.  
    217.     public void HandleInterstitialClosed(object sender, EventArgs args)
    218.     {
    219.         print("HandleInterstitialClosed event received");
    220.         RequestInterstitial();
    221.     }
    222.  
    223.     public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    224.     {
    225.         print("HandleInterstitialLeftApplication event received");
    226.     }
    227.  
    228.     #endregion
    229.  
    230.     #region RewardBasedVideo callback handlers
    231.  
    232.     public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    233.     {
    234.         MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
    235.     }
    236.  
    237.     public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    238.     {
    239.         MonoBehaviour.print(
    240.             "HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
    241.     }
    242.  
    243.     public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
    244.     {
    245.         MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
    246.     }
    247.  
    248.     public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
    249.     {
    250.         MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
    251.     }
    252.  
    253.     public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    254.     {
    255.         RequestRewardBasedVideo();
    256.         MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
    257.     }
    258.  
    259.     public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    260.     {
    261.         string type = args.Type;
    262.         double amount = args.Amount;
    263.         MonoBehaviour.print(
    264.             "HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
    265.     }
    266.  
    267.     public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
    268.     {
    269.         MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
    270.     }
    271.  
    272.     #endregion
    273. }
    274.  
     
  2. robisoftware

    robisoftware

    Joined:
    Jan 22, 2020
    Posts:
    23
    Also i try to delete googleMobile ads plugin and reinstal , but i have the same problem
     
  3. robisoftware

    robisoftware

    Joined:
    Jan 22, 2020
    Posts:
    23
    I'm sorry for 3x post , but i don't find the up button. Please if you had same problem in the past , tell me what solution do you found ..
     
  4. AbdulrahmanaAbs

    AbdulrahmanaAbs

    Joined:
    Oct 30, 2020
    Posts:
    1
    hi
    did you find solve to your problem ?