Rewarded video ads are fullscreen video ads that users have the option of watching in full in exchange for in-app rewards.
This guide shows you how to integrate rewarded video ads from AdMob into a Unity app.
Prerequisites
Complete Get Started. Your Unity application should already have the Google Mobile Ads Unity plugin imported.
Get reference to singleton instance
The first step toward displaying a rewarded video ad is to get a reference to
the singleton RewardBasedVideoAd
instance. This reference can be retrieved by
calling RewardBasedVideoAd.Instance
.
using GoogleMobileAds.Api; ... public class GoogleMobileAdsDemoScript : MonoBehaviour { private RewardBasedVideoAd rewardBasedVideo; ... public void Start() { // Initialize the Google Mobile Ads SDK. MobileAds.Initialize(initStatus => { }); // Get singleton reward based video ad reference. this.rewardBasedVideo = RewardBasedVideoAd.Instance; } }
Load an ad
Because RewardedBasedVideoAd
is a singleton, requests to load an ad should be
made using a shared instance.
It is highly recommended to call LoadAd()
as early as possible (for example,
in the Start()
method of a script attached to a GameObject
) to allow videos
to be preloaded, as shown below.
...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private RewardBasedVideoAd rewardBasedVideo;
...
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => { });
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
this.RequestRewardBasedVideo();
}
private void RequestRewardBasedVideo()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
}
}
Always test with test ads
The sample code above contains an ad unit ID and you're free to request ads with it. It's been specially configured to return test ads rather than production ads for every request, which makes it safe to use.
However, once you register an app in the AdMob UI and create your own ad unit IDs for use in your app, you'll need to explicitly configure your device as a test device when you're developing. This is extremely important. Testing with real ads (even if you never tap on them) is against AdMob policy and can cause your account to be suspended. See Test Ads for information on how you can make sure you always get test ads when developing.
Ad events
To further customize the behavior of your ad, you can hook into a number of
events in the ad's lifecycle: loading, opening, closing, and so on. Listen for
these events by registering a delegate for the appropriate EventHandler
. The
most important event is OnAdRewarded
, which is called when the user should be
rewarded for watching a video. You can also implement other ad events, as shown
below.
...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private RewardBasedVideoAd rewardBasedVideo;
...
public void Start()
{
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
this.RequestRewardBasedVideo();
}
private void RequestRewardBasedVideo()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
}
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print(
"HandleRewardBasedVideoFailedToLoad event received with message: "
+ args.Message);
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
MonoBehaviour.print(
"HandleRewardBasedVideoRewarded event received for "
+ amount.ToString() + " " + type);
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
}
The OnAdRewarded
event 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);
}
The OnAdFailedToLoad
event also contains special event arguments. It passes an
instance of HandleAdFailedToLoadEventArgs
with a Message
describing the
error:
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("Rewarded video ad failed to load: " + args.Message);
// Handle the ad failed to load event.
}
Ad event | Description |
---|---|
OnAdLoaded |
The OnAdLoaded event is invoked when an ad has finished
loading. |
OnAdFailedToLoad |
The OnAdFailedToLoad event is invoked when an ad fails to
load. The Message parameter describes the type of failure that
occurred.
|
OnAdOpening
|
This method is invoked when the ad is displayed, covering the device's screen. If you're using an analytics package to track clickthroughs, this is a good place to record one. |
OnAdStarted
|
This method is invoked when the ad starts to play |
OnAdRewarded
|
This method is invoked when a user should be rewarded for watching a
video. The Reward parameter describes the reward to be presented to
the user.
|
OnAdClosed
|
This method is invoked when the rewarded video ad is closed due to the user tapping on the close icon or using the back button. If your app paused its audio output or game loop, this is a great place to resume it. |
OnAdLeavingApplication
|
This method is invoked after OnAdOpened , when a user click
opens another app (such as the Google Play store), backgrounding the current
app.
|
Show the ad
Before displaying a rewarded ad to users, they must be presented with an explicit choice to view rewarded ad content in exchange for a reward. Rewarded ads must always be an opt-in experience.
To show a rewarded video ad, use the IsLoaded()
method to verify that it's
done loading, then call Show()
.
The following code snippet demonstrates how to display a rewarded video ad.
private void UserOptToWatchAd()
{
if (rewardBasedVideo.IsLoaded()) {
rewardBasedVideo.Show();
}
}
Reload an ad
The OnAdClosed
event is a handy place to load a new rewarded
video ad after displaying the previous one:
public void Start() { this.rewardBasedVideo = RewardBasedVideoAd.Instance; rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed; } public void HandleRewardBasedVideoClosed(object sender, EventArgs args) { this.RequestRewardBasedVideo(); }
Additional resources
Samples
- HelloWorld example a minimal implementation of all ad formats
Next steps
- Create your own rewarded video ad unit in the AdMob UI.
- Try another ad format: