Journal 110 — Unity 2D Mobile, Dungeon Escape Unity Ads

Chris Nielsen
5 min readOct 24, 2021

Objective: To add Unity Ads to game in order to add gems for shop

In this article, I am going to go over how to add Unity Ads to the mobile game.

We can start by creating a Unity Ads button in the shop panel. After it’s duplicated, we need to remove the OnClick function for buying an item.

Next, we can go the Services window and turn on Unity Ads. You may need to create a Project ID during this process.

After turning on Ads, make sure to also select Test Mode, as this allows the game to simulate the ads during testing.

Reward Ads Overview

To generate the reward ads, I am primarily following the UnityAds guide here. We need a script to hold the reward ads behavior, and an Ad Initializer. There are several interface scripts that are in the Unity Advertisements API that come with the Package Manager, so you shouldn’t need to generate any other scripts.

As you can see from the Hierarchy and Assets screenshot below, I have added:

  • Reward Button that will hold the rewards script
  • Ads Manager for holding the Ads Initializer
  • The Shop C# script needs to be updated for the rewards button
  • The GameManager needs to be updated to include a reference to the player in order to reward gems
  • I’ll use a timer script to show a countdown timer in the rewards button
  • The AdsInitializer script for initializing the Unity Ads
  • The RewardedAdsButton script will handle the loading and showing of Unity Ads

I’ll start with a script for the Ads Initializer, along with an empty game object called the Ads Manager.

For the reward ads button, I want to set up a means to have the player wait for a new ad to be generated before clicking on another ad. Otherwise they could endlessly press the reward ad button. I have initially started with a 20 second delay.

We can go to the Services window in Unity and click on Dashboard to go to the Unity project website. Then we can look at the Ad Units to see which type of ad we need to refer to in the script. In this case, we want to use a “Rewarded_Android” ad to have the player watch the whole ad, and be rewarded with 100g.

The new script contains functions for loading ads, showing ads, interface methods for checking status of ads showing and loading (e.g., finished, errors, initialization complete, ready, start) that are described in the Knowledge Base.

I included a timer script to handle a delay of 20 seconds.

During testing, I found the shop panel with the contents and ads button was not working all under one parent. The shop panel is set active and inactive as the player walks up and leaves the shop. The scripts for the children were not running as a result, so the ads were not loading or showing.

To address this, I made the ads button a separate object from the rest of the shop panel. In order to simulate the opening and closing of the shop panel, I include an initial SetActive function when the shop panel is first opened up, but then if the player walks away and comes back, I simply hide the button and text, so the script can still run for the countdown timer and the ads initializing and loading.

In the rewards script above, I needed to update the GameManager so the rewards button can communicate through the Game Manager to add the gems to the player, instead of using the rewards button directly to update the player gems. Similarly, to update the gems shown on screen, we can use the UIManager.Instance, since there is already a method to update the number of gems the player has.

Now, after all these steps, we can check the build on PC.

As you can see, ads button works and the timer works, but the test build does not show the ads and reward the gems correctly.

However, let’s try the Android build.

It works!

Thank you for your time!

--

--

Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.