Integrating Unity Ads into Your Game

Learn how to integrate Unity Ads into your game. By Sean Duffy.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

Wire up the Time-Delayed Present Reward Button

Open AdManager.cs and add the following method:

public bool IsAdWithZoneIdReady(string zoneId) {
  return Advertisement.IsReady(zoneId);
}

This will check if an ad is ready to display based on the zoneId string you pass to the method.

Open PresentAdReward.cs and add the following method:

private bool IsReady() {
  if (Time.time > remainingCooldown) {
    return AdManager.instance.IsAdWithZoneIdReady(zoneId);
  }
  
  return false;
}

This method calls the AdManager’s IsAdWithZoneIdReady() to see if a video ad is ready. This happens only if your timer has exceeded the remaining cool down time.

Next, add the following to show the video:

private void OpenReward() {
  AdManager.instance.ShowVideoAd(PresentRewardCallback, zoneId);
}

The method is wired to the Present Reward button in the UI. When the user clicks the button to watch a video, PresentRewardCallback will be passed the result.

Now add the code for the callback:

private void PresentRewardCallback(ShowResult result) {
  switch (result) {
    case ShowResult.Finished:
      Debug.Log("Player finished watching the video ad and is being rewarded with extra fuel.");
      GameManager.instance.extraFuel += rewardAmount;

      if (coolDown > 0f) {
          remainingCooldown = Time.time + coolDown;
      }
      break;
    case ShowResult.Skipped:
      Debug.Log("Player skipped watching the video ad, no reward.");
      break;
    case ShowResult.Failed:
      Debug.Log("video ad failed, no reward.");
      break;
  }
}

If the video ad plays to completion, then an amount of fuel gets rewarded to the player for their next run. This is customizable by setting the rewardAmount float value in the PresentAdReward script. A check happens to ensure that the cool down value is greater than zero. If it is, then the remaining cool down gets set to the current elapsed game time, plus the cool down value. This has the effect of resetting the count down timer for the reward back to 30 seconds so players will have to wait a further 30 seconds before they can use the reward again.

The skipped and failed cases log information to the console. They don’t reward the player if they skipped the video ad, or the ad failed to play.

To track the elapsed time for the reward cool down, add the following to the Update() method in PresentAdReward.cs

if (button) {

  button.interactable = IsReady();

  if (button.interactable) {
    cooldownText.text = "Reward ready! Touch to watch video and open";
  }
  else {
    cooldownText.text = "Next reward in: " + (int)(remainingCooldown - Time.time) + " seconds";
  }
}

The button’s interactable property is set to true or false based on whether a video ad is actually ready to play.

If the button is interactable, then the text below the button gets set to "Reward ready!". Otherwise, the current remaining time to wait for the reward displays.

Finally, hook OpenPresent() to the button by adding the following code to the bottom of Start() in the PresentAdReward.cs script:

button.onClick.AddListener(OpenReward);

Build and run the game, and crash into a rock to view the game over screen. You should now see a shiny reward present available for you to click on!

UnityAds-reward-ready

Tap or click on the present icon to view the video ad. Then receive your staggeringly large supply of 20 reward fuel! After closing the ad, you’ll notice that the reward won’t be available for 30 seconds and the count down timer displays instead.

UnityAds-reward-waiting

Remove your Test Button

Well done on making it this far! :]

Time for a little cleanup. You’ll now remove the button that lets players watch a video ad with no reward attached. Hooking this button up to the Unity Ads API was a useful learning exercise on using Unity Ads. But really, nobody will want to watch an ad without a reward.

Go to the Lander scene and remove the button as follows:

  • In the Hierachy: expand the Canvas GameObject.
  • Expand the GameOverPanel GameObject.
  • Expand the ButtonsPanel.
  • Right-Click the WatchVideoAdBtn GameObject.
  • Click Delete.

UnityAds-watch-video-ad-button

Open the Lander.cs script in your code editor, and remove the private field variable called watchVideoAdBtn from the top of the class.

Remove the following line from Start():

watchVideoAdBtn = GameObject.Find("WatchVideoAdBtn").GetComponent<Button>();

Then a little further down in the method find and remove this line:

watchVideoAdBtn.onClick.AddListener(AdManager.instance.ShowStandardVideoAd);

Save the Lander.cs script, go back to the Unity Editor, and run the game. After crashing (or landing), you’ll notice that the old button is now gone. You simply have the two buttons providing fuel rewards in return for watching video ads.

UnityAds-cleanup-complete

Here is what the timed ad reward experience looks like on an actual device:

RW_Unity_Ad_2

Switching Off Test Mode for Game Release

The last thing to do before releasing your game to the Apple or Android stores is to switch off ad test mode. There is a way to force test mode off through the Unity Ads Admin panel if you forget to disable test mode before releasing your game, but it’s best to do this before submitting your app.

Click the Services tab in the Unity Editor and click on Ads. Un-check Test Mode.

UnityAds-disable-test-mode

That is it! If you run your game on an actual device at this point, you will get live Unity Ads coming through.

Note: It is against Unity Ads’ TOS to view live ads on your own testing devices, so make sure you only disable test mode for an actual release build of your game.

Changing the Orientation of Your Video Ads

If you release your game as a portrait orientation app, video ads may play in landscape mode on the device. That requires players to tilt their device to the side to view the videos — that’s a little awkward. In that case, follow these steps to change the ads to match the orientation.

  • Login to your Ads portal.
  • Select your Unity Ads project.
  • Click on the link for the App Store you are targeting.
  • Select the Settings tab.
  • Toggle Video Orientation to on.
  • Click Save.

UnityAdsVidOrientation

Don’t be that developer who forces players to rotate their devices the other way around to view ads! :]

UnityAds-ad-orientation-ragecomic

Filtering Ads

In some cases you may wish to exclude certain ad categories from displaying to your players. Filtering out ads is a simple task thanks to the Unity Ads admin portal. Here is how you can define a set of filtered out categories:

  • Login to the Unity Ads portal.
  • Select your project.
  • Select a platform store<.>
  • Click the Ad Filtering tab.
  • Select the categories you wish to exclude.
  • Click Save.

UnityAdFilters

Sean Duffy

Contributors

Sean Duffy

Author

Mitch Allen

Tech Editor

Chris Belanger

Editor

Brian Moakley

Final Pass Editor and Team Lead

Over 300 content creators. Join our team.