Rewarded ads

Warning.

This is an archived version of the documentation. Actual documentation for all platforms can be found here.

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: R-M-XXXXXX-Y.

Loading ads

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

rewardedAd.LoadAd(request);
Copied to clipboard
About loading ads
Use the AdRequest object to transmit the code received in the Adfox interface (for more information, see Help for Adfox):
...
// Code from the Adfox interface for working with direct campaigns.
private Dictionary<string, string> CreateAdfoxParameters()
{ 
    Dictionary<string, string> parameters = new Dictionary<string, string>()
    {
        {"adf_ownerid", "<example>"},
        {"adf_p1", "<example>"},
        {"adf_p2", "<example>"},
        {"adf_pt", "<example>"},
        ...
    };

    return parameters;
}

...

private void RequestRewardedAd()
{
    ...
    AdRequest request = new AdRequest.Builder()
        .WithParameters(CreateAdfoxParameters())
        .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 a delegate for the appropriate EventHandler, as shown below:

...
private void RequestRewardedAd()
{
    ...
    rewardedAd.OnRewardedAdLoaded += HandleRewardedAdLoaded;
    rewardedAd.OnRewardedAdFailedToLoad += HandleRewardedAdFailedToLoad;
    rewardedAd.OnReturnedToApplication += HandleReturnedToApplication;
    rewardedAd.OnLeftApplication += HandleLeftApplication;
    rewardedAd.OnAdClicked += HandleAdClicked;
    rewardedAd.OnRewardedAdShown += HandleRewardedAdShown;
    rewardedAd.OnRewardedAdDismissed += HandleRewardedAdDismissed;
    rewardedAd.OnImpression += HandleImpression;
    rewardedAd.OnRewarded += HandleRewarded;
    rewardedAd.OnRewardedAdFailedToShow += HandleRewardedAdFailedToShow;
    ...
}

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

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

public void HandleReturnedToApplication(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleReturnedToApplication event received");
}

public void HandleLeftApplication(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleLeftApplication event received");
}

public void HandleAdClicked(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleAdClicked event received");
}

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

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

public void HandleImpression(object sender, ImpressionData impressionData)
{
    var data = impressionData == null ? "null" : impressionData.rawData;
    MonoBehaviour.print("HandleImpression event received with data: " + data);
}

public void HandleRewarded(object sender, Reward args)
{
    MonoBehaviour.print("HandleRewarded event received: amout = " + args.amount + ", type = " + args.type);
}

public void HandleRewardedAdFailedToShow(object sender, AdFailureEventArgs args)
{
    MonoBehaviour.print(
        "HandleRewardedAdFailedToShow event received with message: " + args.Message);
}
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