Reward ad
- Game Development course
- Expert
- Lesson 40/47
- 4 min
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 grantedThe 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.
Practice 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.
Starter code
using UnityEngine;
public class RewardHandler : MonoBehaviour
{
// Coin değişkenini tanımla
void Start()
{
// Reklam tamamlanma metodunu çağır
}
// OnAdCompleted metodunu tanımla
}Sample solution
using UnityEngine;
public class RewardHandler : MonoBehaviour
{
int coins = 0;
void Start()
{
OnAdCompleted();
}
void OnAdCompleted()
{
coins += 50;
Debug.Log("Coins: " + coins);
}
}Expected output
Coins: 50
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When should a player receive a reward from a rewarded advertisement?
- When the player opens the game
- When the ad completes successfully Correct answer
- Every time the ad button is tapped
- When the ad starts loading
Explanation: The reward should be granted when the callback reports that the advertisement has completed successfully.
2 Complete the Reward Callback Method
void OnAdCompleted()
{
coins += ____;
}
void OnAdCompleted()
{
coins += 50;
}
Explanation: When the callback completes successfully, 50 is added to the player's coin amount.
3 Rewarded Advertisement Flow
| Term | Meaning |
|---|---|
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 |