Integrating Unity Ads into Your Game
Learn how to integrate Unity Ads into your game. By Sean Duffy.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Integrating Unity Ads into Your Game
30 mins
- Getting Started
- The Lander Scene
- Setup the Ads Service
- The Unity Ads Dashboard
- Initializing Unity Ads
- Adding Video Ads to your Game
- The Game Over UI
- Wire up the Watch a Video Ad button
- Designing Engaging Ads for Your Game
- Rewarding and Enticing the Player with Rewarded Ads
- Timing Ad Availability
- Wire up the Time-Delayed Present Reward Button
- Remove your Test Button
- Switching Off Test Mode for Game Release
- Changing the Orientation of Your Video Ads
- Filtering Ads
- Show Me the Money!
- Where to Go From here?
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! :]
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.
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
.
If you run the app on a device, here is what the test ad experience will look like:
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.
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.