Rewarded ads

A rewarded ad is a configurable full-screen ad. The user gets a reward for viewing the ad.

  1. Adding a rewarded ad to the project
  2. Loading ads
  3. Displaying ads
  4. Rewarded ad events
  5. Clearing ads

Adding a rewarded ad to the project

To enable advertising, create a RewardedAd object in the script (in C#) that is attached to the GameObject.

...
using YandexMobileAds;
using YandexMobileAds.Base;
...

public class YandexMobileAdsRewardedAdDemoScript : MonoBehaviour
{
    private RewardedAd rewardedAd;
    ...
    private void RequestRewardedAd()
    {
        string adUnitId = "YOUR_adUnitId";

        rewardedAd = new RewardedAd(adUnitId);
    }
    ...
}
Copied to clipboard

The RewardedAd constructor contains the adUnitId parameter — a unique identifier that is assigned in the Partner interface and looks like this: adf-XXXXXX/YYYYYY.

Note.

In the mobile mediation interface, the AdUnitId is called the location ID.

Loading ads

After creating and configuring the object of the RewardedAd class, you need to load the ad. To load an ad, use the LoadAd method, which takes the AdRequest object as an argument.

...
private void RequestRewardedAd()
{
    ...
    AdRequest request = new AdRequest.Builder().Build();

    rewardedAd.LoadAd(request);
    ...
}
...
Copied to clipboard

Displaying ads

After the ad has loaded, you can display it:

...
private void ShowRewardedAd()
{
    if (this.rewardedAd.IsLoaded())
    {
        rewardedAd.Show();
    }
    else
    {
        Debug.Log("Rewarded Ad is not ready yet");
    }
}
...
Copied to clipboard

Rewarded ad events

To track events that occur in a rewarded ad, register the delegate for the appropriate EventHandler, as shown below:

...
private void RequestRewardedAd()
{
    ...
    rewardedAd.OnRewardedAdLoaded += this.HandleRewardedAdLoaded;
    rewardedAd.OnRewardedAdFailedToLoad += this.HandleRewardedAdFailedToLoad;
    rewardedAd.OnRewardedAdOpened += this.HandleRewardedAdOpened;
    rewardedAd.OnRewardedAdClosed += this.HandleRewardedAdClosed;
    rewardedAd.OnRewardedAdLeftApplication += this.HandleRewardedAdLeftApplication;
    rewardedAd.OnRewardedAdShown += this.HandleRewardedAdShown;
    rewardedAd.OnRewardedAdFailedToShow += this.HandleRewardedAdFailedToShow;
    rewardedAd.OnRewardedAdDismissed += this.HandleRewardedAdDismissed;
    rewardedAd.OnRewarded += this.HandleOnRewarded;
    ...
}

public void HandleRewardedAdLoaded(object sender, EventArgs args)
{
    Debug.Log("HandleRewardedAdLoaded event received");
}

public void HandleRewardedAdFailedToLoad(object sender, AdFailureEventArgs args)
{
    Debug.Log("HandleRewardedAdFailedToLoad event received with message: " + args.Message);
}

public void HandleRewardedAdOpened(object sender, EventArgs args)
{
    Debug.Log("HandleRewardedAdOpened event received");
}

public void HandleRewardedAdClosed(object sender, EventArgs args)
{
    Debug.Log("HandleRewardedAdClosed event received");
}

public void HandleRewardedAdLeftApplication(object sender, EventArgs args)
{
    Debug.Log("HandleRewardedAdLeftApplication event received");
}

public void HandleRewardedAdShown(object sender, EventArgs args)
{
    Debug.Log("HandleRewardedAdShown event received");
}

public void HandleRewardedAdFailedToShow(object sender, AdFailureEventArgs args)
{
    Debug.Log("HandleRewardedAdFailedToShow event received with message: " + args.Message);
}

public void HandleRewardedAdDismissed(object sender, EventArgs args)
{
    Debug.Log("HandleRewardedAdDismissed event received");
}

public void HandleOnRewarded(object sender, Reward args)
{
    Debug.Log("HandleOnRewarded event received: amout = " + args.amount + ", type = " + args.type);
}
Copied to clipboard

Clearing ads

When an ad object is no longer needed, you can delete it. To do this, call the Destroy method:

rewardedAd.Destroy();
Copied to clipboard