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.
- Unrewarded Ads that can be skipped.
- 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.
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.
1 |
using UnityEngine.Advertisements; |
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.
1 |
[SerializeField] string androidID= "4135481"; |
Now in the start method, we will use this ID to initialize the ads.
1 2 3 4 5 |
void Start() { //initalize the ads using the android id Advertisement.Initialize(androidID); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class AdsManager : MonoBehaviour, IUnityAdsListener { //andorid ads ID [SerializeField] string androidID = "4135481"; // Start is called before the first frame update void Start() { //initalize the ads using the android id Advertisement.Initialize(androidID); } // Update is called once per frame void Update() { } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using UnityEngine; using UnityEngine.Advertisements; public class AdsManager : MonoBehaviour, IUnityAdsListener { //andorid ads ID [SerializeField] string androidID = "4135481"; // Start is called before the first frame update void Start() { //initalize the ads using the android id Advertisement.Initialize(androidID); } public void OnUnityAdsDidFinish(string placementId, ShowResult showResult) { } public void OnUnityAdsDidError(string message) { } public void OnUnityAdsDidStart(string placementId) { } public void OnUnityAdsReady(string placementId) { } } |
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.
1 2 3 4 5 6 7 |
void Start() { //add this object as the listner for the ads events Advertisement.AddListener(this); //initalize the ads using the android id Advertisement.Initialize(androidID); } |
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.
1 2 3 |
//placements Ids [SerializeField] string doubleMoneyVideoPlacementId = "doubleMoneyVideo"; [SerializeField] string skippableVideoPlacementId = "skippableVideo"; |
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.
1 2 3 4 5 6 7 8 9 |
public void ShowDoubleMoneyAd() { //show double money ad Advertisement.Show(doubleMoneyVideoPlacementId); } public void ShowSkippableAd() { //show skippable ad Advertisement.Show(skippableVideoPlacementId); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult) { //check if the player finished watching the ad if (showResult == ShowResult.Finished) { //check if the ad that was watched is the double money video ad if (placementId == doubleMoneyVideoPlacementId) { //in that case //debug in the console to check if it's working Debug.Log("Double Money"); //call the add money method of our money manager and add it's current amount of money to it. which means double money MoneyManager.instance.AddMoney(MoneyManager.instance.Money); } } } |
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.
- Close button, which will close the panel directly.
- 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.
1 2 3 4 |
//reference to the double money panel [SerializeField] private GameObject DoubleMoneyPanel; //the interval at which we will show the double money panel [SerializeField] private float doubleMoneyAdInterval = 60f; |
Now let’s create a coroutine that will keep running and show the panel based on the double money interval.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
IEnumerator DoubleMoneyAdRoutine() { //first disable the double money panel DoubleMoneyPanel.SetActive(false); //this routine will never stop. while (true) { //wait for some time, here it's the doubleMoneyAdInterval yield return new WaitForSeconds(doubleMoneyAdInterval); //check if the panel is not enabled if (!DoubleMoneyPanel.activeSelf) { //show the panel DoubleMoneyPanel.SetActive(true); } } } |
Now start the coroutine at the start method.
1 2 3 4 |
void Start() { StartCoroutine(DoubleMoneyAdRoutine()); } |
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.
1 2 |
//the interval at which we will show the skippable ads [SerializeField] private float skippableVideoAdInterval = 150f; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
IEnumerator SkippableVideoAdRoutine() { //this coroutine will keep running while (true) { //check if the player bought the remove ads using the in game store if (PlayerPrefs.GetInt("adsRemoved", 0) == 1) { //if true then break from the loop. yield break; } //wait for some time, here it's the skippableVideoAdInterval yield return new WaitForSeconds(skippableVideoAdInterval); //show the ad using the ads manager GetComponent<AdsManager>().ShowSkippableAd(); } } |
Finally, start the coroutine in the start method and remove the update method.
1 2 3 4 5 |
void Start() { StartCoroutine(DoubleMoneyAdRoutine()); StartCoroutine(SkippableVideoAdRoutine()); } |
That’s it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using UnityEngine; using UnityEngine.Advertisements; public class AdsManager : MonoBehaviour, IUnityAdsListener { //andorid ads ID [SerializeField] string androidID = "4135481"; //placements Id [SerializeField] string doubleMoneyVideoPlacementId = "doubleMoneyVideo"; [SerializeField] string skippableVideoPlacementId = "skippableVideo"; // Start is called before the first frame update void Start() { //add this object as the listner for the ads events Advertisement.AddListener(this); //initalize the ads using the android id Advertisement.Initialize(androidID); } public void ShowDoubleMoneyAd() { //show double money ad Advertisement.Show(doubleMoneyVideoPlacementId); } public void ShowSkippableAd() { //show skippable ad Advertisement.Show(skippableVideoPlacementId); } public void OnUnityAdsDidFinish(string placementId, ShowResult showResult) { //check if the player finished watching the ad if (showResult == ShowResult.Finished) { //check if the ad that was watched is the double money video ad if (placementId == doubleMoneyVideoPlacementId) { //in that case //debug in the console to check if it's working Debug.Log("Double Money"); //call the add money method of our money manager and add it's current amount of money to it. which means double money MoneyManager.instance.AddMoney(MoneyManager.instance.Money); } } } public void OnUnityAdsDidError(string message) { } public void OnUnityAdsDidStart(string placementId) { } public void OnUnityAdsReady(string placementId) { } } |