Implement Admob Interstitial Ad in Unity3d

 We are going to cover how we can implement Admob Interstitial ads in your unity game.
I’ll assume that you already have a game that you just want to implement ads to generate revenue but if you are new and just want to learn how to implement Admob then you can just create a new project.
Make sure it has Android as its platform.


There are few things that you will require before implementing the Admob in your game.
You can download the GoogleMobileAds.unitypackage file from here.
Once you have it downloaded you can just click on it or go to unity > under the project explorer.
Right-Click on Assets > Import > Custom Package and select GoogleMobileads.unitypackage file.
Make sure everything is selected and click on the import button.


It will then load up all the files in your project and will probably do some loading to resolve android dependencies.


Once you have all those files imported and all the android dependencies resolved you are now good to go with the implementation.
Create a C# script for the implementation.
We are going to call this script as AdmobScript.
It is always a good practice to organize your project, therefore, I have created a folder and named it as scripts and created the AdmobScript inside it.

img (Note for newbies) Before you do anything, make sure you save the scene file as it will load on the device once you start your game.

Double click on the AdmobScript to open it up in the editor.

There are two functions in this blank script by default.

  • Start()
  • Update()


Start function will be called when the scene gets loaded.
Update function will be called on every frame.

Before you can call any function to load the ads, you would want to initialize the ads for your project.
So, let's create a function to initialize the ads.
We are going to name this function “RequestInterstitial “.
We need a variable to hold the Admob Interstitial ID as well.
(Newbie tips) If you don’t have an ID you can create it by going to and under “Apps” and create a new one and get the ID.

Just before the start function add the following line of code.


string adUnitId;

We have created a variable to hold the Admob Interstitial ID.
Next step is to initialize the ads.
Just for being on the safe side we are going to add a check on which platform our game is currently on.
To do that what we can do is write the following line of code.


adUnitId = "ca-app-pub-5910292471488572/1874822714"; 

adUnitId = "ca-app-pub-3940256099942544/4411468910";

string adUnitId = "unexpected_platform"; 


It is pretty self-explanatory but still, I am going to explain.
The #if is to check on which platform is currently set on unity editor.
The line of code which contains adUnitId will automatically get black if the editor is not on UNITY_ANDROID or UNITY_IPHONE.
Next step is to create a variable to hold the loaded Ad in the cache.
To do that we can create a variable of type “InterstitialAd” and can name it anything we want.
I will name it “_interstitial”
If you don’t get “InterstitialAd” while trying to create a variable then what you need to do is add the following line of code on the start of your script where it is referencing the assemblies.


(To add on top of the script) 
using GoogleMobileAds.Api;
(Variable of type InterstitialAd) 
InterstitialAd interstitial;


next step is to initialize the interstitial ad and store it in the _interstitial variable.
To do that we can just write the code:

_interstitial = new InterstitialAd (adUnitId);

we are giving our ad id in the argument of the InterstitialAd method so that it can be able to load the ad request in the cache.
Let's create that request and load that in our _interstitial variable.


AdRequest request = new AdRequest.Builder ().Build ();
// Load the interstitial with the request.
interstitial.LoadAd (request);

The whole method for initialization will look like this.


private void RequestInterstitial()
string adUnitId = "unexpected_platform";
// Initialize an InterstitialAd.
interstitial = new InterstitialAd (adUnitId);
AdRequest request = new AdRequest.Builder ().Build ();
// Load the interstitial with the request.
interstitial.LoadAd (request);
// Create an empty ad request.

The ad has been initialized with the request in the cache.
We can now show that ad from the cache in our game.
Calling the ad is so much simple that we can just call the ".Show()" function of _interstitial variable.
Just to be on the safe side we are going to create a method to show Admob interstitial ad.
Let's create a new method.
We are going to name it ShowAdMob().
Write the following lines of code inside your ShowAdmob() function.


if (_interstitial.IsLoaded ()) {
_interstitial.Show ();

we are just checking to make sure our _interstitial variable has the ad loaded in the cache.
If it has the ad loaded we can just use the .Show() function to show the ad.


For testing an editor what I have done is set up a button and named it as ShowAdmobBtn, dragged this script on my main camera. Under the Button component of ShowAdmobBtn, there is an event called On_click event.
Added that method on the OnClick event.


Final step:

Don’t forget to add the ID in the adUnitID variable inside the script.
When you are done with it, build the project on Android.
The same implementation will be done for IOS as well.


In this lesson, we have covered how we can implement Admob interstitial ad in our unity game.
We have learned how we can load our ad in the cache and call it at runtime using a single button.
We also looked at one of the practices to organize our project.



Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?