Google Admob tutorial for Unity

Monetizing games with Ads has become the go to way for many indie game developers when it comes to making money with their games. Google Admob is the first preferred ad network to work with. In this tutorial, we are going to see how to add Google Admob SDK to your Unity game and get a test Ad on your device.

Importing Admob SDK to Unity

Download the latest Admob SDK from Github.

Open your Unity project

Switch your target platform to Android from File>Build settings.

Right click on the project window select Import Package>Import Custom Package.

In the import window select all and click import. If you already have any other Google SDK in your project then uncheck External dependencies before import like shown below.

SDK import window

The Android resolver should kick in if not go to Assets>External Dependency Manager> Android Resolver> Resolve.

Menu showing android resolver

You might see some platform related errors which you can ignore at this stage.

Setting up your Admob Account

Go to Google Admob website and sign in with your Google account.

Click on the Apps option on the left pane and select Add App.

You will see a window where you need to fill in your App details

New Ad mob app window

After create your App on the Admob website you need to create an Ad unit that will be displayed in your game.

For this tutorial, we will create a banner Ad.

Click on Add Ad Unit and select Banner form the add type. Give it a name and click create Ad unit.

New ad unit window

Now you will two ids.

One is your App id and the other one is your Ad unit Id.

Click on Ad unit on the left pane and you should see your Ad unit id.

Click on App settings on the left pane to get our App Id.

You will need both of these in your Unity code.

Initializing and testing Admob in Unity

Go to Assets>Google Mobile Ads> Settings

Enter your Android App Id in the inspector. If you are planning to publish to IOS then enter your IOS app Id also.

You need to initialize Google Ads when your game begins. Add the below code to your first scene in the game.

using UnityEngine;
using GoogleMobileAds.Api;

public class Ad_initialize: MonoBehaviour
{
   
    void Awake()
    {
       
        MobileAds.Initialize(initStatus => { });//You can handle the initialization here
    }
}

To display the banner ad that you created. You need to create an object of type Banner view and then load the ad.

using UnityEngine;
using GoogleMobileAds.Api;
public class Ad_show: MonoBehaviour
{
    BannerView bannerView;
   
    void Awake()
    {
       
        #if UNITY_ANDROID
            string adUnitId = "Your Ad unit ID";
        #else
            string adUnitId = "unexpected_platform";
        #endif
        
        bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
    }
  
    void Start()
    {
        AdRequest request = new AdRequest.Builder().Build();   
        bannerView.LoadAd(request); 
    }

}

Your Banner ad should be visible in the Unity editor. You can also use

bannerView.Hide() and bannerView.Show() to hide and reshow the ads when required.

If you build your app the ads won’t show on your device. This is because the App needs to be verified by Google after published in an App store. If your app is already published then you can go to your Admob account and enter your App store URL.

There is a way to test your Ads in your phone before publishing using test ADs.

Activating test ads

You need to setup a test device for this.

Go to your phone settings> Google>Ads and copy your advertising Id. (This may vary based on your Android version)

Go to Admob home> Settings> Test devices.

Click on Add test device

Add a test device window.

Your Ads should start working on the test device in 30 minutes if your Admob account is old. It will take up to 48 hours if you have just created your Admob account.

If you don’t see the Ads in your phone after 48 hrs. it might be due to the following reasons.

  1. Your App id is wrongly entered. (Many people confuse App id and Ad unit id).
  2. Check your network connection.
  3. You might have hidden the Ad using bannerview.Hide().

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.