Skip to content

Unity 2021 Skippable Ads and Rewarded Ads Tutorial|Monetize Your Game

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

Unity 2021 Skippable Ads and Rewarded Ads Tutorial|Monetize Your Game

Introduction

In this lesson, you will learn how to integrate Unity Ads into an already existing project. We will split our code into two scripts. One script will handle showing the ads and another script that will handle our monetization strategy (when to show the ads).

We will be adding two types of ads.

  1. Unrewarded Ads that can be skipped.
  1. Rewarded Ads that can’t be skipped but will double our current money.

First, we need to integrate ads into our game.

1. Unity Ads Integration.

Make sure that you are logged in to your unity account in the editor.

Go to Project Settings→Services→Ads

Then select your organization.

And Create a project ID.

Now you will be asked whether your app is directed towards children younger than 13 years old.

Select No and click Save.

Now the window will change and it will display a button called “Off” which means the ads are not yet turned on.

Click on it to turn it “On”.

Unity will import the ads package.

After it’s done you will see the current version of the package imported.

Click on the Dashboard link to open, We will need it for later.

In the dashboard select the project.

Then from the left menu select placements.

From this menu, we can check our app IDs for both the play store and the app store, as well as the ad placements we have.

Here we can change/add new types of ads and configure them.

2. Ads Manager.

Alright Now, let’s create our ads manager.

This manager will be responsible for showing ads upon our request.

But to keep our code clean we will create another script later which will contain the logic on when and how to show ads.

-Create a new gameobject call it “Ads Manager”.

-Create and attach a new script called “AdsManager”.

 

 

In this script add the ads namespace.

Now we need to initialize the ads using our app ID, we will be building the game for android so we will use the android app ID which we can find in the dashboard.

Now let’s adds it as a string in our script.

Now in the start method, we will use this ID to initialize the ads.

Now since we need to implement two types of ads. One of them being a rewarded video.

We need to check if the player finished watching the ad until the end only in that case we want to reward the player.

The ads package provides different events that we can subscribe to in order to listen to these events.

In order to achieve this, we need to implement an interface called “IUnityAdsListener”.

If you are not familiar with interfaces in C# you can refer to this article to learn more about it.

We will see an error because we did not implement the interface members.

Click on “Implement interface.

We will see these methods/events added to our class.

Let’s reorder them so that our class looks more cleaner, also remove the exceptions thrown inside them because we don’t need them.

While we are at it remove the update method since we won’t need it.

The code until now should look like this.

Now, these are just methods that we had to add because the interface is like a contract.

It’s our job to make use of these events that’s why we need to register them.

In the start method, we need to add a listener to the ad events which already exist in our class.

In the Start method add the following line.

This way if an ad finished showing for example “OnUnityAdsDidFinish” will get called.

Now before we start showing ads we need to get their placements ids.

Let’s go back to the dashboard.

We can see that there are two ads already defied for us but let’s create our own anyways.

Click on add placement.

Add a new placement give it the following id “doubleMoneyVideo”.

Add another placement for the non-rewarded ads which the player will need to pay us real money to remove later if he/she wants to.

Give it the following id “skippableVideo”.

Now when we show ads in our game we will use these placements ids to tell unity ads which ad we want to show.

Add these placement ids as strings in our ads manager-script.

Now we can pass these placement IDs to the method “Advertisement.Show()” to show different ads.

So let’s create two methods one for each placement, these will be public methods that we will call from different places in our game.

Now in the “OnUnityAdsDidFinish” we need to check for which type of ads the player just finished watching.

If it was the skippable ad then we don’t care but for the double money ad, we need to double the player’s money.

The placement id will get passed to the OnUnityAdsDidFinish when it gets called so we can use it to check if it’s the rewarded video ad placement.

3. Double Money Ads

We will create a panel that will ask the player if he/she would like to watch an ad and in return, we will double the money they have.

Under The game UI create a UI Panel call it “Double Money Panel”.

By default it it will take the whole size of the canvas.

So let’s change its scale.

Now it looks better

 

Let’s change its color.

To this panel, we will add Two buttons.

  1. Close button, which will close the panel directly.
  1. Double Money button, which will double our money.

Under our panel to add a button call it “Close Button”.

Change its position and pivot to the top right.

 

It’s too small so let’s fix that by changing the width and height of the button.

 

 

Let’s change and increase the size of the text as well.

 

On the close button click event drag and drop the double money itself and for its click event select Gameobject→ SetActive and make sure the checkbox is unchecked.

 

This way when we click on the close button we will disable the double money panel.

Test that to check if it’s working.

 

Now add another button to our panel in the middle, call it “Double Money Button”.

Change its text and text size.

 

 

Now for this button let’s link its click event to our show double money ad method in our ads manager.

Now let’s test this button.

 

This means that our ads are working correctly.

Also after we press the double money button in the same fashion as the close button we want to close the panel as well.

 

 

Now disable this panel.

 

 

We will show this panel from the script that we just created “MonetizationStrategy”.

So back to the MonetizationStrategy script.

We want to enable the double money ads every few minutes now for testing we will set it to one minute (60 seconds).

Add the following.

Now let’s create a coroutine that will keep running and show the panel based on the double money interval.

Now start the coroutine at the start method.

In the editor add a reference to the panel gameobject.

Let’s start the game and collect few euros and test the ads.

4. Skippable Ads.

These types of ads will not reward the player and in general, it’s good to avoid them or at least show them at the right time. Like when the player opens the game or when the player closes the game or maybe in other games after every level.

In our game, we will show these ads every few minutes, but we will give the option for the player to stop them using the IAP later.

In the MonetizationStrategy script add the following field and coroutine.

In the coroutine, we will check for a key from the PlayersPrefs, called “adsRemoved”.

Later on, we will set this value to “1” which means true the default value for this key will be “0” which means false.

Finally, start the coroutine in the start method and remove the update method.

That’s it.

Lecture Code

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide