Search Unity

Question My admob ads not showing

Discussion in 'Assets and Asset Store' started by hakan471, Jun 1, 2023.

  1. hakan471

    hakan471

    Joined:
    Apr 12, 2020
    Posts:
    1
    hi there,
    My game that I use in the Play Store does not show admob ads either. Test ads were working fine on Unity and when I bought .apk.

    my AdManager code is here :
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using GoogleMobileAds.Api;
    using System;

    public class AdManager : MonoBehaviour
    {
    private InterstitialAd interstitialAd;
    public BannerView _bannerView;

    public static AdManager instance;
    public void Start()
    {
    DontDestroyOnLoad(this.gameObject);
    if(instance != null && instance != this)
    {
    Destroy(this.gameObject);
    }
    else
    {
    instance = this;
    }
    // Initialize the Google Mobile Ads SDK.
    MobileAds.Initialize((InitializationStatus initStatus) =>
    {
    LoadInterstitialAd();
    CreateBannerView();
    LoadAd();
    });
    }
    #region Interstitial
    // These ad units are configured to always serve test ads.
    #if UNITY_ANDROID
    private string _adUnitId = "ca-app-pub-9324963738813081/3804090355";
    #endif
    /// <summary>
    /// Loads the interstitial ad.
    /// </summary>
    public void LoadInterstitialAd()
    {
    // Clean up the old ad before loading a new one.
    if (interstitialAd != null)
    {
    interstitialAd.Destroy();
    interstitialAd = null;
    }

    Debug.Log("Loading the interstitial ad.");

    // create our request used to load the ad.
    var adRequest = new AdRequest();
    adRequest.Keywords.Add("unity-admob-sample");

    // send the request to load the ad.
    InterstitialAd.Load(_adUnitId, adRequest,
    (InterstitialAd ad, LoadAdError error) =>
    {
    // if error is not null, the load request failed.
    if (error != null || ad == null)
    {
    Debug.LogError("interstitial ad failed to load an ad " +
    "with error : " + error);
    return;
    }

    Debug.Log("Interstitial ad loaded with response : "
    + ad.GetResponseInfo());

    interstitialAd = ad;
    RegisterEventHandlers(interstitialAd);
    });
    }
    /// <summary>
    /// Shows the interstitial ad.
    /// </summary>
    public void ShowAd()
    {
    if (interstitialAd != null && interstitialAd.CanShowAd())
    {
    Debug.Log("Showing interstitial ad.");
    interstitialAd.Show();
    }
    else
    {
    Debug.LogError("Interstitial ad is not ready yet.");
    }
    }
    private void RegisterEventHandlers(InterstitialAd ad)
    {
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
    Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
    adValue.Value,
    adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
    Debug.Log("Interstitial ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
    Debug.Log("Interstitial ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
    Debug.Log("Interstitial ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
    Debug.Log("Interstitial ad full screen content closed.");
    LoadInterstitialAd();
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
    Debug.LogError("Interstitial ad failed to open full screen content " +
    "with error : " + error);
    LoadInterstitialAd();
    };
    }
    #endregion
    #region Banner
    // These ad units are configured to always serve test ads.
    #if UNITY_ANDROID
    private string _adUnitIdBanner = "ca-app-pub-9324963738813081/2362258004";
    #endif

    /// <summary>
    /// Creates a 320x50 banner at top of the screen.
    /// </summary>
    public void CreateBannerView()
    {
    Debug.Log("Creating banner view");

    // If we already have a banner, destroy the old one.
    if (_bannerView != null)
    {
    DestroyAd();
    }

    // Create a 320x50 banner at top of the screen
    _bannerView = new BannerView(_adUnitIdBanner, AdSize.Banner, AdPosition.Top);
    }

    /// <summary>
    /// Creates the banner view and loads a banner ad.
    /// </summary>
    public void LoadAd()
    {
    // create an instance of a banner view first.
    if (_bannerView == null)
    {
    CreateBannerView();
    }
    // create our request used to load the ad.
    var adRequest = new AdRequest.Builder()
    .AddKeyword("unity-admob-sample")
    .Build();

    // send the request to load the ad.
    Debug.Log("Loading banner ad.");
    _bannerView.LoadAd(adRequest);
    ListenToAdEvents();
    }

    /// <summary>
    /// listen to events the banner may raise.
    /// </summary>
    private void ListenToAdEvents()
    {
    // Raised when an ad is loaded into the banner view.
    _bannerView.OnBannerAdLoaded += () =>
    {
    Debug.Log("Banner view loaded an ad with response : "
    + _bannerView.GetResponseInfo());
    };
    // Raised when an ad fails to load into the banner view.
    _bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
    {
    Debug.LogError("Banner view failed to load an ad with error : "
    + error);
    CreateBannerView();
    LoadAd();
    };
    // Raised when the ad is estimated to have earned money.
    _bannerView.OnAdPaid += (AdValue adValue) =>
    {
    Debug.Log(String.Format("Banner view paid {0} {1}.",
    adValue.Value,
    adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    _bannerView.OnAdImpressionRecorded += () =>
    {
    Debug.Log("Banner view recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    _bannerView.OnAdClicked += () =>
    {
    Debug.Log("Banner view was clicked.");
    };
    // Raised when an ad opened full screen content.
    _bannerView.OnAdFullScreenContentOpened += () =>
    {
    Debug.Log("Banner view full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    _bannerView.OnAdFullScreenContentClosed += () =>
    {
    Debug.Log("Banner view full screen content closed.");
    };
    }
    /// <summary>
    /// Destroys the ad.
    /// </summary>
    public void DestroyAd()
    {
    if (_bannerView != null)
    {
    Debug.Log("Destroying banner ad.");
    _bannerView.Destroy();
    _bannerView = null;
    }
    }
    #endregion
    }

    and im get ads:
    if (AdManager.instance._bannerView != null)
    {
    AdManager.instance._bannerView.Show();
    if (adCount % 2 == 0)
    {
    AdManager.instance.ShowAd();
    }
    adCount++;
    }