how to show Admob ads in your Unity game

When you finish your game and want to make some money admob is the better choice for you.

Downlaod admob plugin from that link :

https://github.com/googleads/googleads-mobile-unity/releases/tag/v3.13.1

Run Unity3d and install admob plugin 

Cancel the selection of IOS if you make game for android

and import to your project

Create a New script name it “AdManager”

Create a New Game object name it “AdManager”

Drag “AdManager script” in “AdManager GamoeObject” ,And go for Edit Script 

Copy and Paste that code in AdManager Script

using System;
using UnityEngine;
using GoogleMobileAds.Api;

public class AdManager : MonoBehaviour
{
    private BannerView bannerView;
    private InterstitialAd interstitial;

    string bannerID = "ca-app-pub-3940256099942544/6300978111";//test
    string interstitialID = "ca-app-pub-3940256099942544/1033173712"; //test

    public void Start()
    {

    #if UNITY_ANDROID
        string appId = "ca-app-pub-3940256099942544~3347511713";
    #elif UNITY_IPHONE
        string appId = "ca-app-pub-3940256099942544~1458002511";
    #else
        string appId = "unexpected_platform";
    #endif

        MobileAds.SetiOSAppPauseOnBackground(true);

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

    }

    private AdRequest CreateAdRequest()
    {
        return new AdRequest.Builder()
            .AddKeyword("game")
            .SetGender(Gender.Male)
            .SetBirthday(new DateTime(1985, 1, 1))
            .TagForChildDirectedTreatment(false)
            .AddExtra("color_bg", "9B30FF")
            .Build();
    }

    public void DestroyBanner()
    {
        if (this.bannerView != null)
        {
            this.bannerView.Destroy();
        }
    }

    public void RequestBanner()
    {
        // These ad units are configured to always serve test ads.
    #if UNITY_EDITOR
        string adUnitId = bannerID;
    #elif UNITY_ANDROID
        string adUnitId = bannerID;
    #elif UNITY_IPHONE
        string adUnitId = bannerID;
    #else
        string adUnitId = "unexpected_platform";
    #endif

        // Clean up banner ad before creating a new one.
        if (this.bannerView != null)
        {
            this.bannerView.Destroy();
        }

        // Create a 320x50 banner at the top of the screen.
        this.bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);

        // Register for ad events.
        this.bannerView.OnAdLoaded += this.HandleAdLoaded;
        this.bannerView.OnAdFailedToLoad += this.HandleAdFailedToLoad;
        this.bannerView.OnAdOpening += this.HandleAdOpened;
        this.bannerView.OnAdClosed += this.HandleAdClosed;
        this.bannerView.OnAdLeavingApplication += this.HandleAdLeftApplication;

        // Load a banner ad.
        this.bannerView.LoadAd(this.CreateAdRequest());
    }

    public void RequestInterstitial()
    {
        // These ad units are configured to always serve test ads.
    #if UNITY_EDITOR
        string adUnitId = interstitialID;
    #elif UNITY_ANDROID
        string adUnitId = interstitialID;
    #elif UNITY_IPHONE
        string adUnitId = interstitialID;
    #else
        string adUnitId = "unexpected_platform";
    #endif

        // Clean up interstitial ad before creating a new one.
        if (this.interstitial != null)
        {
            this.interstitial.Destroy();
        }

        // Create an interstitial.
        this.interstitial = new InterstitialAd(adUnitId);

        // Register for ad events.
        this.interstitial.OnAdLoaded += this.HandleInterstitialLoaded;
        this.interstitial.OnAdFailedToLoad += this.HandleInterstitialFailedToLoad;
        this.interstitial.OnAdOpening += this.HandleInterstitialOpened;
        this.interstitial.OnAdClosed += this.HandleInterstitialClosed;
        this.interstitial.OnAdLeavingApplication += this.HandleInterstitialLeftApplication;

        // Load an interstitial ad.
        this.interstitial.LoadAd(this.CreateAdRequest());
    }

    public void ShowInterstitial()
    {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
        else
        {
            MonoBehaviour.print("Interstitial is not ready yet");
        }
    }

    #region Banner callback handlers

    public void HandleAdLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLoaded event received");
    }

    public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print("HandleFailedToReceiveAd event received with message: " + args.Message);
    }

    public void HandleAdOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdOpened event received");
    }

    public void HandleAdClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdClosed event received");
    }

    public void HandleAdLeftApplication(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLeftApplication event received");
    }

    #endregion
    #region Interstitial callback handlers

    public void HandleInterstitialLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleInterstitialLoaded event received");
    }

    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print(
            "HandleInterstitialFailedToLoad event received with message: " + args.Message);
    }

    public void HandleInterstitialOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleInterstitialOpened event received");
    }

    public void HandleInterstitialClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleInterstitialClosed event received");
    }

    public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleInterstitialLeftApplication event received");
    }

    #endregion

}

This IDs for Test when the ads work with no problem change them with your IDs

With this codes you can Show Ads in your game

FindObjectOfType<AdManager>().RequestBanner;//Show Banner
FindObjectOfType<AdManager>().DestroyBanner();//Destroy Banner
FindObjectOfType<AdManager>().RequestInterstitial();//Request Inter..
FindObjectOfType<AdManager>().ShowInterstitial();//Show Inter..

Put that code only in “Start( )” and “Awake( )” methodes

And other codes any where you want except “Update()” methode

If you face any problem contact me and I will help you

Leave a Reply

Your email address will not be published. Required fields are marked *