Rewarding Players — Implementing Unity Ads API

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 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

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.

--

--

--

Unity developer with a love of learning all things programming.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Dungeon Escape Mobile 2D

2D Mobile Game: UI Manager

Simple Android Network Traffic Inspection With Chucker And Seismic

Worms Eye View From Spiral colored decore glass through a roof

STF — Run autotests on Android devices managed by Jenkins CI

A Refined Approach to MVC in Android Part 5 — Extending to Lists and Fragments

Managing Android Multi-module Project with Gradle Plugin and Kotlin

Android’s ImageView scale type cheatsheet

Update your flutter app’s package name with a single command

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luke Duckett

Luke Duckett

Unity developer with a love of learning all things programming.

More from Medium

Spawn Manager — Part 2

Platformer: Player Animation Part 4

Creating a UI Shield Strength indicator

Level Design in Unity Part 1: The Floor