Working with Unity Ads

Ad Initialization and Ad Management

Simon Truong
6 min readJan 8, 2022

--

Following up with our previous article on Enabling Unity Ads, we will continue to work with Unity Ads by linking the Shop UI button to work with the Unity Ads feature.

Scripting with Unity Ads:

Understanding how Unity Ad works:

Within the Unity Dashboard that we can access within the Ads Services, we can see that under the Monetization tab that we have two types of Ads available. One is the skippable Ad that can be cancelled after 5 seconds called “Interstitial” and the second is the Reward Ads that can not be skipped and is able to reward the player for watching the whole Ad, this method is called “Rewarded”.

Banner is also another type of reward, but we shall skip that since our game is not designed for that type of ad.

When we click on the Android Reward link, we are directed to a page which allows you to customize the settings regarding the Android reward function. The most important thing to pay attention here is the “Ad Unit ID” which is the ID that controls the reward action.

Within the Ads Manager script, since we are using the Advertising library, we will need to add that library at the very top of the script.

We know that looking through the Unity api that the Advertisement has several static methods that we can use, most noticeably the “Is Ready” method and the “Show” method.

If we clicked the Show method and look at the examples, we can see that there is indeed the ability to use an “action callback” feature which allows Unity to keep track of what happen to the displayed Ad. For example, if the player finished watching through the entire ad, or skipped or even failed to load. This useful feature will allow us to reward the player whenever the player has finished watching the whole ad.

Scripting with Unity Ads:

To understand how we can access the Unity Ads via script, we first need to understand that there are two parts that puts everything together. First is the the Ad Initializer, this basically allows the Ad to preload at the start of the game. Then is the Actual Ad Manager which controls when to toggle the ad and when to reward the player.

Ad Initializer:

Create a new C# script, then to access the Unity Ads function we first need to gain access to the Unity Library.

We will be working with 3 Unity Ad interfaces, the IUnityAdsInitializerlistener, the IUnityAdsShowlistener and finally the IUnityAdsLoadListener. For the Ad Initializer we will just be using the Initializer interface.

Then we are also going to get some local variables for the Android Game ID, the Test Mode for the Unity Editor. Another local variable granting access from the (future) script of Ad Manager and finally a private string called _gameID.

First we are going to create a new public method called “Initialized Ads”, this will control whenever the Ads get Initialized.

Since we are using the IUnityAdsInitializerListener interface we will need to satisfy several conditions before it is error free. It is going to ask for an public On Initialization Complete method, and also On Initialization Failed method.

the Ad manager . load Ad is a public method that is part of the Ads Manager script that we will create later.

Finally we are going to finish everything by creating an void Awake method that starts the entire process of initializing the Ads.

Ad Manager:

In the second C# script which is name “AdsManager” we are going to create the functionality to control what happens to the preloaded Ads. First we are going to gain access to the Advertisement library along with the UI library.

Then we are going to insert the two remaining Unity Ad Listener interfaces.

For the Ads Manager, I also want it to control the button so that it dims when the preloaded Ad has issues, and only shows when the Ad is ready. The “Android Ad Unit ID” is the same ID name that we have gotten from the Unity Ads Dashboard.

In a new void Awake function, we are basically going to state that the Ad Unit ID is the same as the Android ID. (This would all change if I were to be working with both iOS and Android together). Then the Button is also going to be set to be deactivated and only be reactivated when the preloaded Ad is ready.

The next public method is the “Load Ad” method which loads the initialized Ads.

At the same time, we are going to create another public method that controls the visibility of the Button.

Finally we are going to create another public method to show the Ad. At the same time we are going to deactivate the button.

Finally to satisfy the two Unity Ad listeners Interfaces, we will need several conditions.

As you can see, during the Ads show completion, that is where we can reward the players with the desired set amount of gems.

I also created a coroutine to stop the players from spamming the watch add button. But for most of the required methods that the interfaces needs, we can leave the contents empty.

Now back in Unity, we are going to drag all the required components into the Ad Manager script and also the Initialized Ad script.

When that is complete hopefully the Unity Ads would function properly.

--

--

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.