Search Unity

Admob ads either don't display or take too much time.

Discussion in 'Scripting' started by cmyd, Nov 9, 2019.

  1. cmyd

    cmyd

    Joined:
    Oct 29, 2017
    Posts:
    98
    Here is my AdsManager class,
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using GoogleMobileAds.Api;
    6. using System;
    7.  
    8. public class AdsManager : MonoBehaviour
    9. {
    10.  
    11.     public static AdsManager instance;
    12.     [HideInInspector]
    13.     public bool rewardedVideoLoaded;
    14.  
    15.     private BannerView bannerView;
    16.     private RewardedAd rewardedAd;
    17.  
    18.     [SerializeField]
    19.     string appId = "ca-app-pub-3940256099942544~3347511713";
    20.  
    21.     [SerializeField]
    22.     string bannerAdId = "ca-app-pub-3940256099942544/6300978111";
    23.     [SerializeField]
    24.     string rewardedVideoId = "ca-app-pub-3940256099942544/5224354917";
    25.  
    26.     [SerializeField]
    27.     [Tooltip("If test mode is enabled it will use dummy IDs")]
    28.     bool testMode;
    29.  
    30.  
    31.     string requestType;
    32.  
    33.     private void OnEnable()
    34.     {
    35.         if (instance == null) instance = this;
    36.     }
    37.  
    38.     // Start is called before the first frame update
    39.     void Start()
    40.     {
    41.  
    42.  
    43.         if (testMode)
    44.         {
    45.             appId = "ca-app-pub-3940256099942544/6300978111";
    46.             bannerAdId = "ca-app-pub-3940256099942544/6300978111";
    47.             rewardedVideoId = "ca-app-pub-3940256099942544/5224354917";
    48.         }
    49.  
    50.         // Initialize the Google Mobile Ads SDK.
    51.         MobileAds.Initialize(appId);
    52.  
    53.         //RequestBanner();
    54.         //RequestRewardedVideo();
    55.     }
    56.  
    57.     /// <summary>
    58.     /// Load and Display banner ad
    59.     /// </summary>
    60.     public void RequestBanner()
    61.     {
    62.         // Create a 320x50 banner at the top of the screen.
    63.         bannerView = new BannerView(bannerAdId, AdSize.Banner, AdPosition.Bottom);
    64.  
    65.         // Create an empty ad request.
    66.         AdRequest request = new AdRequest.Builder().Build();
    67.  
    68.         // Load the banner with the request.
    69.         bannerView.LoadAd(request);
    70.     }
    71.  
    72.     /// <summary>
    73.     /// Load Rewarded video
    74.     /// </summary>
    75.     public void RequestRewardedVideo()
    76.     {
    77.         rewardedAd = new RewardedAd(rewardedVideoId);
    78.  
    79.         // Create an empty ad request.
    80.         AdRequest request = new AdRequest.Builder().Build();
    81.  
    82.         // Called when an ad request has successfully loaded.
    83.         rewardedAd.OnAdLoaded           += RewadedVideoLoaded;
    84.         rewardedAd.OnUserEarnedReward   += RewardedVideoComplete;
    85.         //rewardedAd.OnAdClosed           += LoadRewardedVideo;
    86.  
    87.         // Load the rewarded ad with the request.
    88.         rewardedAd.LoadAd(request);
    89.  
    90.      
    91.        
    92.     }
    93.  
    94.     private void LoadRewardedVideo(object sender, EventArgs e)
    95.     {
    96.         RequestRewardedVideo();
    97.     }
    98.  
    99.     void RewadedVideoLoaded(object sender, EventArgs args)
    100.     {
    101.         if (rewardedAd.IsLoaded())
    102.         {
    103.             rewardedVideoLoaded = true;
    104.         }
    105.     }
    106.  
    107.     public void ShowRewardedVideo(string requestType)
    108.     {
    109.         this.requestType = requestType;
    110.         rewardedAd.Show();
    111.     }
    112.  
    113.     public void RewardedVideoComplete(object sender, EventArgs args)
    114.     {
    115.         switch (requestType)
    116.         {
    117.             case "bonus":
    118.                 Player.instance.AddLive();
    119.                 UIManager.instance.PlayerLivesUIUpdate();
    120.                 GameManager.instance.ResumeGame();
    121.                 break;
    122.  
    123.             case "hint":
    124.                 UIManager.instance.DisplayHintPanel();
    125.                 break;
    126.         }
    127.     }
    128.  
    129.  
    130. }
    131.  
    In GameManager class I'm calling these methods

    Code (CSharp):
    1.  void Start()
    2.     {
    3.        AdsManager.instance.RequestBanner();
    4.         AdsManager.instance.RequestRewardedVideo();
    5.         StartCoroutine(LoadRewadedAds());
    6.     }
    7.  
    8. public void DisplayHintAd()
    9.     {
    10.         PauseGame();
    11.         AdsManager.instance.ShowRewardedVideo("hint");
    12.     }
    13.  
    14.  
    15.  
    16.     IEnumerator LoadRewadedAds()
    17.     {
    18.         while (true)
    19.         {
    20.  
    21.             if (!AdsManager.instance.rewardedVideoLoaded)
    22.             {
    23.                 AdsManager.instance.RequestRewardedVideo();
    24.             }
    25.  
    26.             yield return new WaitForSeconds(10f);
    27.  
    28.         }
    29.     }
    In the "start" method I'm calling the banner which takes time to load too and I load the rewarded video ad,
    In "DisplayHintAd" I'm calling "ShowRewardedVideo" from AdsManagerClass, this method is responsible for displaying the ad.

    In the IEnumerator "LoadRewadedAds" I'm checking if the ad isn't loaded than load it.

    What am I doing wrong here?