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 2 of 4 of this article. Click here to view the first page.

Wire up the Watch a Video Ad button

Open Lander.cs in your code editor and add the following to the bottom of Start():

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

This will hook up the Watch a standard video ad button to display a standard Unity video ad. Give it a try in the editor and marvel at your simulated video ad that appears! :]

UnityAds-EditorAd

Note: Unity will only display a blue static image as a simulation in the Unity Editor. On an actual device under test mode, you’ll get a proper Unity video test ad. You can test skipping or watching it in full. When you disable test mode for release, live video ads from Unity Ads advertisers will appear.

Doge is very impressed with Unity’s editor ed templates…

Doge is very impressed with Unity's editor ad templates...

Designing Engaging Ads for Your Game

The key to designing a great ad experience for your game lies in how you integrate Unity Ads. First, ads should be enticing and rewarding for players.

Second, ads should do their best to keep people playing your game. There is no point in displaying a video ad when the player dies, or at the start/end of a level. This is tedious and frustrating to players. It will likely result in them deleting your game.

Ads should integrate into the gameplay experience you’ve crafted for the player. A simple example would be to offer the player consumable in-game items as a reward for watching an ad. Another example would be a second chance after failing (like offering a checkpoint restore). You could offer these rewards in your UI at a resting point or natural break in the gameplay, or as an offer from an in-game character in a dialogue sequence. Remember that forcing players to watch ads is almost certain to lead to uninstalls — videos should always be optional.

Note: You’ll only want to concern yourself with designing reward video ads. These yield a higher effective cost per thousand impressions, or eCPM. These types of videos create higher engagement from players by allowing them to opt-in.

Users will most likely delete a game that forces them to watch ads with no reward.

Users will most likely delete a game that forces them to watch ads with no reward.

Rewarding and Enticing the Player with Rewarded Ads

You now have the ability to show a standard video ad in-game, but with no reward attached. Offering your players a video ad to watch with no reward isn’t going to impress anyone! Nobody is going to watch a video just for the fun of it. Time to change that up and give players an incentive to watch video ads.

The game currently gives the player eight units of fuel to guide their lander down to the landing pad. Too much maneuvering will waste fuel, and players can run out of fuel on the way down. A simple incentive to watch a video ad would be to reward the player with more fuel for their next run after crashing.

Open AdManager.cs in your code editor and add the following method:

public void ShowRewardedFuelVideoAd() {
  ShowVideoAd(FuelRewardCallBack, "rewardedVideo");
}

This is a public method that returns void, which is required when attaching a method to a button.

A custom callback method is passed in that will reward the player. The zone ID is set to “rewardedVideo”. That tells Unity Ads that this video ad will be a rewarded ad.

Also add the following method to AdManager.cs:

private void FuelRewardCallBack(ShowResult showResult) {
 
  var fuelBtn = GameObject.Find("MoreFuelRewardAdBtn").GetComponent<Button>();
 
  switch (showResult) {
      case ShowResult.Finished:
          Debug.Log("Player finished watching the video ad and is being rewarded with extra fuel.");
          GameManager.instance.extraFuel = 10f;
          fuelBtn.transform.GetChild(0).GetComponent<Text>().text = "Fuel added (10 extra)";
          fuelBtn.enabled = false;
          break;

      case ShowResult.Skipped:
          Debug.Log("Player skipped watching the video ad, no reward.");
          fuelBtn.GetComponent<Button>();
          fuelBtn.transform.GetChild(0).GetComponent<Text>().text = "No fuel added - you skipped the ad";
          fuelBtn.enabled = false;
          break;

      case ShowResult.Failed:
          Debug.Log("video ad failed, no reward.");
          break;
  }
}

The code gets a reference to the button in the UI panel. That’s so that the button state and text will be set depending on the result of the video ad.

A switch statement process different actions. In this case, the action is based on whether the video ad gets watched to completion, is skipped, or fails. In the case of completion, extra fuel is added to the GameManager instance. When the player starts a new game, the extra fuel is added to the player’s fuel supply.

Open Lander.cs and add the following to the bottom of Start(), just after the other AddListener call you added for the standard video ad button earlier.

fuelRewardBtn.onClick.AddListener(AdManager.instance.ShowRewardedFuelVideoAd);

This will call ShowRewardedFuelVideoAd() on the AdManager instance when the player presses the fuel reward button.

Save your code, go back to Unity to try out the results.

Fly around, and either complete a landing, or crash somewhere. You’ll now be able to use the fuel reward button. Since you are in the editor, the test ad will appear. If you look in the console, you will see the debug message “Player finished watching the video ad and is being rewarded with extra fuel”.

If you test this on an actual iOS or Android device, you will see a real video ad play. The same code will be able to determine if you watched the ad to completion or skipped it. If you skipped it, or if the ad failed for some reason, then no fuel reward will be added, and the code for the ShowResult.Skipped switch statement section runs instead of ShowResult.Finished.

UnityAds-fuel-reward-added

If you run the app on a device, here is what the test ad experience will look like:

RW_Ads_1_Animated

Timing Ad Availability

You can expand the experience even further and add a timed delay to reward items. This can serve to ensure that players don’t get rewards too often. It can also ensure that they don’t request too many video ads. Unity’s ad service does have some limitations on the maximum amount of ads a single user will see every 24 hours.

Note: Users are limited by default to 25 ads per day (on a rolling 24-hour basis) per app. Users are also limited to seeing the same ad only once per hour.

Timed delay ads can ensure that a player won’t get “failed video” ads after reaching their daily limit. This will prevent players from seeing odd behavior in your game where they try to access a reward.

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.