2D Mobile Game: Unity Ads


In this article, we will cover how to add Unity ads to your mobile game that rewards the player with in-game gems.

Ad Button

We are going to create a button for the user to click if they want 100 gems, which will trigger the ad video.

In the Shop UI, we can duplicate a previous buttons and change the text to “Click Here for 100 G”.

We reposition the buttons and change the color of the button to make it stand out.

One thing we want to watch out for is removing the On Click function on this new button. Since we duplicated the button, we want to remove any functions from the previous button.

Unity Ads Setup

To enable Unity Ads, you need to go into Windows > General > Services.

This will open up a new tab next to the Inspector called “Services”.

You will need to select and/or create an organization with a Unity ID. This will allow you to receive analytics of the ads.

The next step will be to enable Ads. Make sure to enable test mode and built-in Ads extension to help us develop the implementation the of ad in our game.

The “Go to Dashboard” button will bring you to the analytics page of Unity Ads, where you can see a multitude of data points on ad.

Scripting Unity Ads

We create empty game object and attach a script, Ads Manager, that will handle the behavior(s) of the ad.

For now we will create a public method, ShowRewardedAd, and log a message to test if the button is working.

In the Shop UI, we will want to add the Ads Manager game object since it has the script attached to it and On Click method from the inspector can call the ShowRewardedAd method we made on the script.

Back in the AdsManager script, we need to access the UnityEngine.Advertisements library in order to make use of Unity Ads. Also, an interface will be added IUnityAdsListener.

With the addition of this Interface it is required to add the following methods:

  • OnUnityAdsReady
  • OnUnityAdsDidError
  • OnUnityAdsDidStart
  • OnUnityAdsDidFinish

Each of these method, will have logs to alert us what has happen with the Unity Ad.

We are going to declared a few variables that will require information that can be found from Unity Services Dashboard.

The game ID can be found on the game project dashboard and since we are making this game for Android, we want that ID but you can grab the Apple Game ID too.

In the placement settings, you want to grab the string for the certain kind of ad that will be playing. For this project, the ad is a reward ad for the Android. Copy the exact link from the Dashboard.

Lastly, we want to be able to test the ad in the Unity Editor so we will create a bool for test mode.

Next, we add three methods that will add the listener, remove it and initialize which will need the game ID and put into test mode.

In the ShowRewardedAd method, that is called when the ad button is pressed in the Shop UI, we want to check if the reward video is ready to be played. If it is, we will play the reward video and if not, we will log it.

The interface methods we are going to be using the Ads Did Finish that is called when the Ad finishes. One of the parameters is the placementID, which is the Andriod_Reward string and the other is a ShowResult event.

This will let us know if the video failed, was skipped (which is not a possible for rewarded ads, but regular ads) and finished. If the finished case is true then we will reward the player 100 gems.

Since we are in test mode, we can see if this works in the editor.

When we close out of the ad and check the console, we do see the video finished play result was triggered so we can move ahead with actually awarded the player the 100 gems.

Player Reward Logic

We have to be able to give the player 100 gems after the video ad has finished playing and be able to update it on the UI.

The player script has a public method with an int parameter that allow us to pass in any amount to be added to the current amount of gems. We also get the instance of the UI Manager to call the Update Gem Count and pass in the newly added gems.

We want to try to keep Manager scripts to communicate with only other Manager scripts, but for the Game Manager we will get a handle on the Player script since the Add Gems method is located there.

In the Ads Manager, when the Show Result finished case is true, we can now access the Add Game Player method through the Game Manager instance and pass in the gem amount we want to reward the player for watching the ad.

The las thing is to update the player’s gem count in the Shop UI.

When the payer enters the shop, we call the Open Shop method from the instance of the UI Manager which passes in the current player gem count.

We want to modify the Ad Manager script, accessing this Open Shop method through the UI Manager and pass in the Player Gem count through the Game Manager.

We can see the Player is awarded 100 gems after the video ad and it is updated on the UI and shop UI.

Android Studio

It is great we can see it working in the Unity Editor, but we want to test in on an Android device.

This requires Android Studio to be installed.

In the Studio we want to Debug the APK file, which is what is export from Unity for Android devices.

You can either connect an Android device and run the exported file or use a simulator. If you are connecting an Android device, you can simply upload the APK file to cloud storage (Dropbox, Google Drive, etc) and download it on your device from the cloud storage.

I will be using the built in simulator which allows you to run multiple Android on your computer so you don’t need to purchase them.

Select the Create device.

This will bring you to the different devices the Anroid Studio can simulate. I will be using simulating the Pixel 2.

We want to see the logs from our game, so we want to open up Logcat. Now this will show ALL logs from the device.

So we need to create a Filter to only show us Logs from our Unity game.

We select + to create a new filter, give it a name (Unity) and log the tag Unity, so it will only show Unity logs.

You can see the logs running as we use the simulator and everything is running as it should.

Android Studio allows you to just run the device simulator. This is an great way to test you game on hardware you don’t own.

Until next time…Cheers!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store