Creating Rewarded Video Ads in Unity

Gabriel Perez
5 min readJan 12, 2022

What if I want to reward my players after watching a video Ad?

The goal is to implement Unity Ads and reward players with several gems after watching a commercial.

I went ahead to create a UI Button in my shop UI. I duplicated the Buy button, renamed it, moved it above the shop window, and even animated the color to have a flash effect.

I opened Unity’s Services panel and clicked on Ads. A window popped up where I enabled Ads. I want to test the ad feature before releasing it to the world, so I made sure I checked “Test Mode.”

I clicked the Dashboard link and it opened a web browser window to my Unity’s Monetization page.

Under “Ad Units,” I selected the platform I’m currently developing.

I need the Ad Unity Id of the “Rewarded Android” ad unit.

I will need to paste the name to a string via code.

Back to Unity, I created a new game object and called it “AdsManager.” I also created a new script with the same name and attached it to the game object.

I didn’t know where to begin, so I looked up Unity’s Documentation on how to implement an Ad. I followed along:

  • Installing the Unity SDK
  • Initializing the SDK in Unity
  • and Implementing Rewarded ads in Unity

It went along nicely! I started with setting up the Initializing process.

using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;
public class AdsManager : MonoBehaviour, IUnityAdsInitializationListener
{
[SerializeField] private Button _showAdButton;
[SerializeField] private string _androidGameId;
[SerializeField] private bool _testMode = true;
private void Awake()
{
InitializeAds();
}
public void InitializeAds()
{
Advertisement.Initialize(_androidGameId, _testMode, this);
}
public void OnInitializationComplete()
{
Debug.Log("Unity Ads Initialization complete.");
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
Debug.Log($"Unity Ads Initialization Failed: {error} - {message}");
}
}

I added two namespaces, the UI and Advertisement:

using UnityEngine.Advertisements;
using UnityEngine.UI;

I have references for the ad button, the game id located in the monetization dashboard in project settings:

And a bool for test mode. All variables are Serialized Fields so that I can have them exposed in the inspector.

I had to implement an interface, IUnityAdsInitializationListener, for Initialization Callbacks.

It implemented two methods. OnInitializationComplete() and OnInitializationFailed().

When the game runs, I want it to initialize the SDK on Awake(). I created a new method and called it “InitializeAds().” I want to make sure that my game Id is referenced before loading Ads. Or it will cause unexpected behavior.

So in InitializeAds(), I call this:

Advertisment.Initialize(_gameID, _testMode, this);

so the SDK can initialize early in the game’s life cycle.

Onto implementing rewarded ads, I added two new interfaces which implemented various methods.

IUnityAdsLoadListenerIUnityAdsShowListener

I also added a variable to reference the name of the Ad Unit ID we copied earlier called “Rewarded_Android.”

[SerializeField] private string _androidAdUnitId = “Rewarded_Android”;

I created a new method to show the rewarded ad:

public void ShowRewardedAd()
{
Debug.Log("Showing rewarded ad");
if(Advertisement.isInitialized)
Advertisement.Show(_androidAdUnitId, this);
}

This method will check to see if the SDK is initialized, and if it is, then Unity can show the Ad from the Ad Unit Id. I also passed in “this” script so it can be called from the onClick event.

public void OnUnityAdsAdLoaded(string placementId)
{
if (_androidAdUnitId.Equals(_androidAdUnitId))
{
_showAdButton.onClick.AddListener(ShowRewardedAd);
}
}

The OnUnityAdsAdLoaded() method checks if the Ad Unit Id equals the same object, then listen for the ShowRewardedAd() method. In simple terms, when the ad button is pressed, the onClick event will fire the ShowRewarededAd() method.

Lastly, the OnUnityAdsShowComplete() method is where I will handle the logic to reward the player.

public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
{
if(_androidAdUnitId.Equals(_androidAdUnitId) && showCompletionState.Equals(UnityAdsCompletionState.COMPLETED))
{
Debug.Log("You Finished the ad. You receive 100G!");
GameManager.Instance.AddGems(100);
UIManager.Instance.UpdateShopGemCount(GameManager.Instance.GetGemsAmount());
}
else if(_androidAdUnitId.Equals(_androidAdUnitId) && showCompletionState.Equals(UnityAdsCompletionState.SKIPPED))
{
Debug.Log("You skipped the ad");
}
else if(_androidAdUnitId.Equals(_androidAdUnitId) && showCompletionState.Equals(UnityAdsCompletionState.UNKNOWN))
{
Debug.Log("Couldn't show ad for Unknown reasons");
}
}

I want to check each completion state. If it is the same Ad Unity Id and the video ad finished, then reward the player 100 gems. The other states will only log on to the Console if the ad somehow manages to skip itself or stop playing for unknown reasons.

With the programming all set, I made sure all references were added to the AdsManager component in the inspector.

I also want to make sure the ad button’s OnClick() event is properly set.

The ShowRewardedAd() method will be called when the user presses the ad button.

Now to test if it works, I enabled the test mode bool in the AdsManager component and hit Play in Unity.

It works! Although it’s only a test, the actual ad is an 8-second video that will run instead of a test splash.

Gabriel

--

--

Gabriel Perez

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.