Game Development
Reward ad
Learn to reward the player with a rewarded ad.
What Is a Rewarded Advertisement?
A rewarded advertisement is an ad that allows a player to earn an in-game reward in exchange for voluntarily watching it. The reward can be gold, energy, an extra life, or an in-game resource. The player should be given the option to watch the ad, and the reward should only be granted when viewing completes successfully.
Player watches ad
↓
Ad completes
↓
Reward granted
The ad provider's completion notification should be used to grant the reward.
Granting Rewards with a Callback
When an advertisement finishes, the ad provider can invoke a callback. This callback reports whether the ad has completed and whether the reward condition has been met. The game can increase the player's resource amount based on this notification.
void OnAdCompleted()
{
coins += 50;
}
In a real project, the callback signature may vary depending on the advertising package being used.
Exercise Grant a Reward When an Ad Completes
Create a MonoBehaviour class named RewardHandler. Define an int variable named coins. In the OnAdCompleted method, increase the coin amount by 50 and print the new amount to the console. Run the callback example from the Start method.
Show example solution
using UnityEngine;
public class RewardHandler : MonoBehaviour
{
int coins = 0;
void Start()
{
OnAdCompleted();
}
void OnAdCompleted()
{
coins += 50;
Debug.Log("Coins: " + coins);
}
}
Test yourself
-
When should a player receive a reward from a rewarded advertisement?
- When the player opens the game
- When the ad completes successfully
- Every time the ad button is tapped
- When the ad starts loading
Show answer
When the ad completes successfully — The reward should be granted when the callback reports that the advertisement has completed successfully.
-
Complete the Reward Callback Method
void OnAdCompleted()
{
coins += ____;
}
Show answer
void OnAdCompleted()
{
coins += 50;
}
-
Rewarded Advertisement Flow
- Rewarded Ad
- An ad watched in exchange for a reward
- Callback
- Is invoked when an event occurs
- Completion
- Indicates that the ad has completed
- Reward
- An in-game benefit given to the player
Next lesson: Ethical principles