Rewarding Players — Implementing Unity Ads API

Luke Duckett
CodeX
Published in
4 min readJan 3, 2022

In this quick article, I will go over how to implement the Unity Ads API in your game to show the user rewarded and unrewarded ads.

The first step is to install the Advertisement package from the Package Manager. Go to “Window” > “Package Manager” and under “Unity Registry” find “Advertisement” and click Install.

Next, we need to create a project ID to link our Unity ads to our account. Go to “Edit” > “Project Settings” > “Ads” and select your organization linked to your Unity account.

You can now click the create project ID to create your new ID.

You will have to answer a question about if the project will be marketed to children, which will create the link for you.

Next, turn the Ads Service on and ensure you have the most up-to-date version installed. For testing purposes make sure you enable “Test mode”.

Remember when your game goes live you’ll need to turn test mode off.

Now we can go to our Unity Dashboard and finish our set-up. Ensure that the correct project is selected then click the “Complete Activation” button. You will be given some set-up options, select “I’m not using Meditation, only Unity Ads’.

Now we can set up our Ad units. Ad Units are the type of ad you wish to show. In our case, we are going to set up rewarded Android ads and fire them when the player presses a button. Take note of the ad unit ID and your game Id at the top of the screen.

In my game scene, I have created a button for the player to click to watch an ad and be rewarded with in-game currency.

Next, create an empty object to house a new adsManager script. Add your script to the object. In the script, we will need the namespace “UnityEngine.Advertisements”. I am setting the class using the singleton pattern to be able to access it across all scripts.

Next, we want to implement the IUnityAdsInitializationListener, IUnityAdsLoadListener and IUnityAdsShowListener interfaces. This gives us access to context data from the advertisements. I have set up each function to debug its information. There is more information in the documentation.

We need to make sure we initialize and load our ads when we first load our game so the ads are ready for when the player needs them.

We can get a reference to our button

and set its interactable status to false if the ad does not load.

Next, we can now hook up the functionality to reward the player. On the player function, we add a function to adjust our diamond amount.

Now we can find and cache a reference to the player on the Ads Manager.

On the function “OnUnityAdsShowComplete” we can check the enum UnityAdsShowCompletionState

This is to see if the player has watched the entire ad. If we have multiple ads we can add a switch statement that checks the ad unit ID and rewards the player appropriately and disable our button.

We can now set up the functionality to show the ads. On my UIManager I’ve added the following function.

Now we can assign this to the button On Click event.

Now we can test our functionality.

That's all for now.

--

--

Luke Duckett
CodeX
Writer for

🎮 First Nations Unity Dev from Wonnarua country 🏞️ | From Player to Lifelong Learner: Crafting Games, Debugging Code, and Embracing New Technology