Rewarded interstitial is a type of incentivized ad format that allows you offer rewards for ads that appear automatically during natural app transitions. Unlike rewarded ads, users aren't required to opt-in to view a rewarded interstitial.
Prerequisites
- Unity plugin 5.4.0 or higher.
Implementation
The main steps to integrate rewarded interstitial ads are:
- Load an ad.
- Display the ad and handle the reward event.
- Use ad event callbacks
Load an ad
Loading an ad is accomplished using the static LoadAd()
method on the
RewardedInterstitialAd
class. The load method requires an ad unit ID, an
AdRequest
object, and a completion handler which gets called when ad loading
succeeds or fails. The loaded RewardedInterstitialAd
object is provided as a
parameter in the completion handler. The example below shows how to load a
RewardedInterstitialAd
.
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private RewardedInterstitialAd rewardedInterstitialAd;
...
public void Start()
{
...
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
RewardedInterstitialAd.LoadAd(adUnitId, request, adLoadCallback);
}
private void adLoadCallback(RewardedInterstitialAd ad, string error)
{
if (error == null)
{
rewardedInterstitialAd = ad;
}
}
}
Display the ad and handle the reward event
When presenting your ad, you must provide a callback to handle the reward for the user.
The following code presents the best method for displaying a rewarded interstitial ad.
public void ShowRewardedInterstitialAd()
{
if (rewardedInterstitialAd != null)
{
rewardedInterstitialAd.Show(userEarnedRewardCallback);
}
}
private void userEarnedRewardCallback(Reward reward)
{
// TODO: Reward the user.
}
Use ad event callbacks
To further customize the behavior of your ad, you can hook into a number of events. Listen for these events by registering a delegate for the appropriate event handler.
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private RewardedInterstitialAd rewardedInterstitialAd;
...
public void Start()
{
...
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
RewardedInterstitialAd.LoadAd(adUnitId, request, adLoadCallback);
}
private void adLoadCallback(RewardedInterstitialAd ad, string error)
{
if (error == null)
{
rewardedInterstitialAd = ad;
rewardedInterstitialAd.OnAdFailedToPresentFullScreenContent += HandleAdFailedToPresent;
rewardedInterstitialAd.OnAdDidPresentFullScreenContent += HandleAdDidPresent;
rewardedInterstitialAd.OnAdDidDismissFullScreenContent += HandleAdDidDismiss;
rewardedInterstitialAd.OnPaidEvent += HandlePaidEvent;
}
}
private void HandleAdFailedToPresent(object sender, AdErrorEventArgs args)
{
MonoBehavior.print("Rewarded interstitial ad has failed to present.");
}
private void HandleAdDidPresent(object sender, EventArgs args)
{
MonoBehavior.print("Rewarded interstitial ad has presented.");
}
private void HandleAdDidDismiss(object sender, EventArgs args)
{
MonoBehavior.print("Rewarded interstitial ad has dismissed presentation.");
}
private void HandlePaidEvent(object sender, AdValueEventArgs args)
{
MonoBehaviour.print(
"Rewarded interstitial ad has received a paid event.");
}
}