Creating Rewarded Video Ads in Unity 2021

GameDev Dustin
5 min readJun 12, 2023

Summary
In this article we’ll really roll up our sleeves and get to work on implementing Unity’s Legacy Ad system with Rewarded Ads.

Back in the Unity Editor, let’s create an empty gamoeobject in the Hierarchy window and name it “Ads Manager”.

Now create a new C# script called “AdsManager” and assign it as a component to our Ads Manager gameobject.

The AdsManager Class
For now, we’ll just add a public method called “ShowRewardedAd” to our new AdsManager class.

Now, make sure the Watch_Ad UI element has the button component.

If not, add it as shown above.

Now add an event to the OnClick() section.

Assign the Ads Manager gameobject to it.

Then select the AdsManager class and ShowRewardedAd() method as shown above.

Now when the Watch_Ad button is clicked, ShowRewardedAd() will be called.

Before we continue, just verify in the Package Manager > Unity Registry that “Advertisement Legacy” is installed in your project.

Get Your Game ID
Back on the Unity Gaming Services website under the Monetization > Settings tabs and Game IDs section, write down your Google Play Store ID.

Initializing Advertisements
The above link to Unity’s Documentation gives us the code logic we need to initialize Legacy Ads.

Reference it as needed.

Add the above code logic per Unity’s website.

Make sure to implement the IUnityAdsInitializationListener interface and to add the using statement for UnityEngine.Advertisements.

On the AdsManager component, enter your Android Game ID.

Click run and you should get a successful message in the console.

Implementing Rewarded Ads Code Logic
As always, the ultimate authority is Unity’s own documentation.

Reference the link above as needed.

We could separate this logic out into another class, but for this project we’ll do it all here.

Implement the IUnityAdsLoadListener and IUnityAdsShowLisetner interfaces in the AdsManager class.

Then implement their associated members.

Skipping to the Finish Line
After some trial and error, this is the complete finished AdsManager class.

Most of this is identical to the Unity Documentation.

It’s actually a pretty concise way to implement this functionality with Unity’s Advertisement package handling nearly all of the logic in the background.

We do want to add some class variable references to the PlayerInventory class in order to reward diamonds after the ad has completed.

This is done in the GivePlayerReward() method which is called from OnUnityAdsShowComplete().

One major thing I’d like to not is that you will need additional functionality if you want this to happen immediately on scene load.

When attempting to run this with the Store UI toggled on from the Editor just for testing purposes, you end up with the Advertisement initialization not having completed and it seems to just bug out from there.

When running it in normal gameplay mode, the player takes some time to get to the store and by then the functionality is running as expected.

The UIManager Class
We always want an add to load when the Store UI dialog opens.

To do this we need a references to the AdsManager class and to call the LoadAd() method when OnShopOpen() is called.

Assign the AdsManager script component to the UI Manager component as shown above.

On the Ads Manager component, make sure you’ve assigned the Watch_Ad button.

Also ensure that you’ve set the Android Game ID from the Unity Gaming Services website.

Hopefully, at this point you should be able to run it in the Editor and build to your Android device with Ads working!

If not, review the Unity Documentation and this article.

One last thing to note.

You’ll see the above when testing in the Unity Editor.

The first image in this article was taken of the game running on an Android device.

--

--