Display a rewarded ad with Unity Ads
Tutorial
·
intermediate
·
+10XP
·
20 mins
·
(44)
Unity Technologies

Rewarded ads are a great way to boost engagement, letting players move beyond the free-to-play tier of your game without any financial outlay.
In this tutorial, you’ll display a rewarded video ad in a Unity project and capture the result of displaying that ad.
Languages available:
1. Overview
Rewarded ads are a great way to boost engagement, letting players move beyond the free-to-play tier of your game without any financial outlay. In this tutorial, you’ll display a rewarded video ad in a Unity project and capture the result of displaying that ad.
2. Before you begin
If you’re unfamiliar with Unity Ads (or need to refresh your memory), review Get started with Unity Ads before you start this tutorial.
3. Create a script to display the ad
To create and begin the script to display a rewarded video ad:
1. If you haven’t already, create or load a project and connect it to Unity Ads. Navigate to the dashboard and find and note your project’s Game IDs.
2. In the Hierarchy, select Add (+) and then Create Empty.
3. Name the new GameObjected “RewardedAdDisplayObject”.
4. Create a new C# script called “RewardedAdDisplay” and open it in your IDE of choice.
5. On line 4, reference the Advertisements namespace:
using UnityEngine.Advertisements;
6. On line 6, add listeners:
public class RewardedAdDisplay : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
7. Beginning on line 8, add:
public string myGameIdAndroid = "XXXXXXX";
public string myGameIdIOS = "XXXXXXX";
public string adUnitIdAndroid = "Rewarded_Android";
public string adUnitIdIOS = "Rewarded_iOS";
public string myAdUnitId;
public string myAdStatus = "";
public bool adStarted;
public bool adCompleted;
private bool testMode = true;
Important: On lines 8 and 9, replace XXXXXXX with your Game IDs.
8. In Start(), beginning on line 22, add:
#if UNITY_IOS
Advertisement.Initialize(myGameIdIOS, testMode, this);
myAdUnitId = adUnitIdIOS;
#else
Advertisement.Initialize(myGameIdAndroid, testMode, this);
myAdUnitId = adUnitIdAndroid;
#endif
The code above sets some specific instructions to initialize the ad based on the hardware the app is running on.
4. Overview of the available Unity Ads Listeners
The IUnityAdsInitializationListener is an interface that has methods that handle Initialization results. These methods are:
- void OnInitializationComplete();
- void OnInitializationFailed(UnityAdsInitializationError error, string message);
The IUnityAdsLoadListener is an interface that has methods that handle Load results. These methods are:
- void OnUnityAdsAdLoaded(string adUnitId);
- void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message);
The IUnityAdsShowListener is an interface that has methods that handle Show results.. These methods are:
- void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message);
- void OnUnityAdsShowStart(string adUnitId);
- void OnUnityAdsShowClick(string adUnitId);
- void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState);
Unity recommends implementing all methods in your code, even if you do not use them all.
5. Implement the listeners’ callback methods
Now you’re ready to use those methods to complete your code:
1. Starting on line 38 of your script, create the following methods:
public void OnInitializationComplete()
{
Debug.Log("Unity Ads initialization complete.");
Advertisement.Load(myAdUnitId,this);
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
myAdStatus = message;
Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
}
public void OnUnityAdsAdLoaded(string adUnitId)
{
Debug.Log("Ad Loaded: " + adUnitId);
if (!adStarted)
{
Advertisement.Show(myAdUnitId,this);
}
}
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
myAdStatus = message;
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
}
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
myAdStatus = message;
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
}
public void OnUnityAdsShowStart(string adUnitId)
{
adStarted = true;
Debug.Log("Ad Started: " + adUnitId);
}
public void OnUnityAdsShowClick(string adUnitId)
{
Debug.Log("Ad Clicked: " + adUnitId);
}
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
adCompleted = showCompletionState == UnityAdsShowCompletionState.COMPLETED;
Debug.Log("Ad Completed: " + adUnitId);
}
2. Your RewardedAdDisplay script is complete. Save your changes, review the method summary below, and then return to the Unity Editor.
Key methods of UnityAds listeners
The OnInitializationFailed, OnUnityAdsFailedToLoad, OnUnityAdsShowFailure methods will pass the error message to our myAdStatus variable.
The OnUnityAdsShowComplete method will set the adCompleted variable to true so we can see the status in the Inspector when we run the Scene.
The OnUnityAdsShowStart method will set the adStarted variable to true so we can stop trying to show the ad.
The OnUnityAdsAdLoaded method will show the ad when it’s ready, as long as the ad has not already been displayed.
Review the full RewardedAdDisplay script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class RewardedAdDisplay : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
public string myGameIdAndroid = "YOUR_GAME_ID_HERE";
public string myGameIdIOS = "YOUR_GAME_ID_HERE";
public string adUnitIdAndroid = "Rewarded_Android";
public string adUnitIdIOS = "Rewarded_iOS";
public string myAdUnitId;
public string myAdStatus = "";
public bool adStarted;
public bool adCompleted;
private bool testMode = true;
// Start is called before the first frame update
void Start()
{
#if UNITY_IOS
Advertisement.Initialize(myGameIdIOS, testMode, this);
myAdUnitId = adUnitIdIOS;
#else
Advertisement.Initialize(myGameIdAndroid, testMode, this);
myAdUnitId = adUnitIdAndroid;
#endif
}
// Update is called once per frame
void Update()
{
}
public void OnInitializationComplete()
{
Debug.Log("Unity Ads initialization complete.");
Advertisement.Load(myAdUnitId,this);
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
myAdStatus = message;
Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
}
public void OnUnityAdsAdLoaded(string adUnitId)
{
Debug.Log("Ad Loaded: " + adUnitId);
if (!adStarted)
{
Advertisement.Show(myAdUnitId,this);
}
}
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
myAdStatus = message;
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
}
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
myAdStatus = message;
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
}
public void OnUnityAdsShowStart(string adUnitId)
{
adStarted = true;
Debug.Log("Ad Started: " + adUnitId);
}
public void OnUnityAdsShowClick(string adUnitId)
{
Debug.Log("Ad Clicked: " + adUnitId);
}
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
adCompleted = showCompletionState == UnityAdsShowCompletionState.COMPLETED;
Debug.Log("Ad Completed: " + adUnitId);
}
}
6. Complete implementation and test the ad
Now you're ready to complete implementation and test the ad:
1. In the Hierarchy, select the RewardedAdDisplayObject GameObject.
2. In the Inspector window, select the lock icon (upper right corner).
3. Direct your attention to the RewardedAdDisplay (Script) component.

4. Enter Play Mode. Note that the Ad Started property in the Inspector is enabled.
5. In the Game view, select Close in the upper-right of the ad display. Note that the Ad Completed property in the Inspector is now also enabled.
Note: If there is an error, it will be displayed in the text box My Ad Status field.
6. Exit Play Mode.
7. Next steps
You have now completed setting up Unity Rewarded Ads and can successfully display the rewarded ad. In your app, you will need to verify that AdCompleted is true and, if so, reward the player.