Nerd For Tech
Published in

Nerd For Tech

Unity Guide

Rewarded video ads | Unity

A quick guide about how to implement rewarded video ads in Unity

Objective: Implement rewarded video ads in our 2D mobile game with Unity.

In the last post I covered how to implement mobile input with Unity. Now, it’s time to implement rewarded video ads in order to grant certain reward to the player if the ads are watched.

Choose building platform

To start, let’s make sure that the correct build platform for our game is selected. To implement Unity ads in our game we’ll need to choose between Android or iOS. In our case, we’ll choose Android, therefore, let’s open the build settings and make sure to switch the build platform:

Import ads package

Then, to be able to implement the Unity ads, let’s import the respective package from Unity Technologies by using the Package Manager. Make sure to open the package manager and look within the Unity Registry for the Advertisement package to click on the Install button:

Unity Project ID

Next, let’s make sure to create an Unity Project ID to be able to use and associate the Unity Ads with our account. To do it, let’s open the Project Settings and within the Services tab we’ll be able to choose an organization of our account:

Enable Ads

Once the project is associated with an organization let’s enable the ads for the project by opening the Services window and clicking on the Ads section:

Then, let’s make sure to define the COPPA compliance for the project to determine if the ads will be for children under age of 13 or not. Once done, we’ll be able to see that the Ads section is now enabled:

Next, let’s enable the test mode within the same section to test if our ads will work (and avoid being tagged for fraud):

Finally, let’s go to the Unity dashboard and make sure to complete the Ads activation in the respective project. We’ll be using just Unity Ads and not Unity Mediation in our game:

Once the ads are enabled we’ll see the Ad Units, which are important to select which type of ad we’ll be using in our game. In this case, as we’re implementing rewarded videos, we’ll be using the Rewarded Android ad unit. It’s also important to notice the Game ID’s (which are unique) above the Ad Units as it will be important to associate the ads shown with our account.

An Ad Unit is a collection of ad format settings for content serving at a single surfacing point in your game. Use them to designate the surfacing point to serve rewarded or interstitial ads. You can also use Ad Units to configure advanced settings, such as ad types and whether to allow skipping.

Create the ads button

Once our project is ready to test the Unity ads, we’ll need a button to test the rewarded videos. If you’ve been following my latest posts then you’ll remember that we had implemented a store for our game, so, let’s use the respective shop button to test our rewarded videos:

Implementing the Advertisement package

And now, in order to start using the Unity ads in our game, let’s start by opening the Shop script that handles the shop. Then, let’s:

  • Add the UnityEngine.Advertisements namespace at the top to be able to use the Unity ads SDK in this script.
  • Implement the next interfaces (which will allow us to handle the ads):
  1. IUnityAdsInitializationListener
  2. IUnityAdsLoadListener
  3. IUnityAdsShowListener

Each interface implements the next methods:

If you want to know more about each one of these methods you can check the official Unity API reference here:

Next, let’s add the next variables, which we’ll use to handle the Unity ads SDK:

  • Android game ID

This variable will store the game ID of Android to associate it with the project in your organization. You can find this ID on your Unity dashboard.

  • Test mode

This variable will indicate if the ads should be displayed on test mode or not.

  • Enable per placement mode

This variable will indicate if the SDK should be initialized with the Load API lifecycle.

  • Rewarded video ID

This variable will store Ad Unit ID of the rewarded video.

  • Reward button

This variable will store a reference to the ad button in order to enable or disable it according to the SDK initialization and load process.

Once the variables have their respective value defined, let’s create a new method to initialize the Unity ads SDK using the respective values as parameters:

Note: The Unity ads SDK must be initialized before loading any ad in our game.

Then, let’s call the method on the Awake method to make sure that the SDK initializes at the very start:

Next, let’s create a new method to load the rewarded video ad. We’ll need to use the Advertisement.Load method with the respective Ad Unit ID and a reference to the IUnityAdsLoadListener interface that we’re implementing:

Then, we’ll call the above method using one of the methods implemented by the IUnityAdsInitializationListener interface to make sure that the Unity ads SDK gets initialized before loading the ad:

In the other method implemented by the IUnityAdsInitializationListener interface we’ll just make sure to print the error in case that the SDK fails to initialize:

And now, let’s define the methods implemented by the IUnityAdsLoadListener interface. First we’ll use the OnUnityAdsAdLoaded method to enable the use of the ad button if the ad was loaded successfully:

And, on the other hand, if the ad didn’t load successfully, we’ll just print the error:

Then, let’s create a new public method that will be called by the OnClick event of our ad button. In this method we’ll disable the use of the button and we’ll display the loaded ad by using the Advertisement.Show method with the respective Ad Unit ID and the IUnityAdsShowListener reference (the interface we’re implementing) as parameters:

Important: Once the method was created make sure to reference it in the OnClick event of the ad button through the inspector window.

Finally, in order to verify that the rewarded video was completely watched, let’s use the callback methods implemented by the IUnityAdsShowListener interface:

  • OnUnityAdsShowStart handles the logic for the ad starting to play.
  • OnUnityAdsShowClick handles the logic for the user clicking on the ad.
We won’t use these methods for now.
  • OnUnityAdsShowComplete handles the logic for the ad finishing, so we will make sure to compare the UnityAdsShowCompletionState enum parameter to check if the video was successfully completed and grant the reward we want:
Once the reward was granted make sure to load another ad so that the ad button keeps working as expected.
  • OnUnityAdsShowFailure handles the logic for the Ad Unit failing to show, so we’ll print the error in that case and load another ad.

And now, if we run the game with Unity we’ll see that the Unity ads SDK gets initialized as expected:

And, if we use the button in our shop, we’ll be able to see the respective test ad as we’re using the test mode:

When your ads are ready be sure to deactivate the test mode on both sides (code and dashboard) of Unity.

And that’s it, we implemented rewarded videos for our game with Unity! :D. I’ll see you in the next post, where I’ll be showing how to publish our Unity game to the Google Play Store.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

How to have a Neovim configuration compatible with Vim

Output of the command tree -L 2 -I plugged on $HOME/.config/nvim

Adaptation, a primal survival instinct

Getting started with Azure and Xamarin

Getting Started with Litmus SDK

Noob’s Guide to use Terraform with AWS

Cross account Secrets Access in AWS

Scaling Up Smart: 4 key tips on successfully using cloud-native technology to scale your…

Continuous Integration 101

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
Fernando Alcantara Santana

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).

More from Medium

Setting up a Unity Project for the High Definition Render Pipeline (HDRP)

Introducing Stream’s New SDK for the Unity Gaming Engine

How to Build an Interactive Security Camera System in Unity

Enabling Unity Ads