Rewarded video ads allow publishers to offer the user something of value in exchange for engaging with advertising content.
Basic rewarded video ad request
Here's the minimal code needed to create and load a rewarded video ad:
private void RequestRewardBasedVideo()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "INSERT_AD_UNIT_HERE";
#elif UNITY_IPHONE
string adUnitId = "INSERT_AD_UNIT_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
RewardBasedVideoAd rewardBasedVideo = RewardBasedVideoAd.Instance;
AdRequest request = new AdRequest.Builder().Build();
rewardBasedVideo.LoadAd(request, adUnitId);
}
Reward-based video ads adhere to a singleton design. Requests to load an ad are made to a shared instance.
Like interstitial ads, rewarded video ads need to be explicitly shown at an appropriate stopping point in your game, as the following shows:
private void GameOver()
{
if (rewardBasedVideo.IsLoaded())
{
rewardBasedVideo.Show();
}
}
Rewarded video ad events
RewardBasedVideoAd
provides ad events to notify you about an ad's lifecycle.
These events are of type EventHandler
. This example demonstrates how to register
for ad events on a rewarded video ad:
// Reward based video instance is a singleton. Register handlers once to
// avoid duplicate events.
if (!rewardBasedEventHandlersSet)
{
// Ad event fired when the rewarded video ad
// has been received.
rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
// has failed to load.
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
// is opened.
rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
// has started playing.
rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
// has rewarded the user.
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
// is closed.
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
// is leaving the application.
rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
rewardBasedEventHandlersSet = true;
}
Since a rewarded video instance is a singleton object, we recommend registering for ad events only once to avoid duplicate events.
The OnAdRewarded
event is the only event that contains special event arguments. It passes
an instance of Reward
with a Type
and Amount
describing the reward given to the user:
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
print("User rewarded with: " + amount.ToString() + " " + type);
}