Skip to content

Unity Plugin - Rewarded ads

Rewarded ads are full-screen ads that give the user an in-app reward, such as coins or an extra life, for watching the ad to the end. Users choose to watch them, so offer them in exchange for something the user wants.

Before you start, install and initialize the plugin.

1. Subscribe to events

Subscribe to the rewarded events before calling LoadRewarded, so you don't miss any. Unsubscribe when the object is disabled.

void OnEnable()
{
    A11MediationSDK.OnRewardedLoaded     += OnLoaded;
    A11MediationSDK.OnRewardedLoadFailed += OnLoadFailed;
    A11MediationSDK.OnRewardedEarned     += OnEarned;
    A11MediationSDK.OnRewardedClosed     += OnClosed;
    A11MediationSDK.OnRewardedShowFailed += OnShowFailed;
    A11MediationSDK.OnRewardedShown      += OnShown;
    A11MediationSDK.OnRewardedClicked    += OnClicked;
    A11MediationSDK.OnRewardedRevenue    += OnRevenue;
}

void OnDisable()
{
    A11MediationSDK.OnRewardedLoaded     -= OnLoaded;
    A11MediationSDK.OnRewardedLoadFailed -= OnLoadFailed;
    A11MediationSDK.OnRewardedEarned     -= OnEarned;
    A11MediationSDK.OnRewardedClosed     -= OnClosed;
    A11MediationSDK.OnRewardedShowFailed -= OnShowFailed;
    A11MediationSDK.OnRewardedShown      -= OnShown;
    A11MediationSDK.OnRewardedClicked    -= OnClicked;
    A11MediationSDK.OnRewardedRevenue    -= OnRevenue;
}

2. Handle the events

Every event passes the ad unit ID first, so check it if you use more than one rewarded ad unit. When an ad closes or fails to show, load the next one. When loading fails, we recommend retrying with exponential backoff. See the full example.

private void OnLoaded(string id, A11AdInfo info) { }
private void OnLoadFailed(string id, A11AdLoadError error) { }
private void OnEarned(string id, A11AdInfo info)
{
    // Grant the reward here, see step 5
}
private void OnClosed(string id, A11AdInfo info) { }
private void OnShowFailed(string id, A11AdInfo info, A11AdShowError error) { }
private void OnShown(string id, A11AdInfo info) { }
private void OnClicked(string id, A11AdInfo info) { }
private void OnRevenue(string id, A11AdInfo info) { }

3. Load the ad

Load the ad well before you need it. When OnRewardedLoaded fires, offer the reward to the player.

public void Load() => A11MediationSDK.LoadRewarded(AdUnitId);

4. Show the ad

Call ShowRewarded only when the player chooses to watch, for example when they tap "Watch ad for a reward".

A11MediationSDK.ShowRewarded(AdUnitId);

5. Grant the reward

Grant the reward in your OnRewardedEarned handler.

private void OnEarned(string id, A11AdInfo info)
{
    if (id != AdUnitId) return;
    GrantReward(); // Your own reward logic, for example add coins
}

Caution

OnRewardedClosed fires whether or not the user earned the reward. The reward should be granted only in OnRewardedEarned.

Full example

using UnityEngine;
using Aurion11.A11Mediation;

public class RewardedExample : MonoBehaviour
{
    private const string AdUnitId = "your_rewarded_ad_unit";
    private bool _isReady;
    private float _retryDelay = 5f;

    void OnEnable()
    {
        A11MediationSDK.OnRewardedLoaded     += OnLoaded;
        A11MediationSDK.OnRewardedLoadFailed += OnLoadFailed;
        A11MediationSDK.OnRewardedEarned     += OnEarned;
        A11MediationSDK.OnRewardedClosed     += OnClosed;
        A11MediationSDK.OnRewardedShowFailed += OnShowFailed;
        A11MediationSDK.OnRewardedShown      += OnShown;
        A11MediationSDK.OnRewardedClicked    += OnClicked;
        A11MediationSDK.OnRewardedRevenue    += OnRevenue;
    }

    void OnDisable()
    {
        A11MediationSDK.OnRewardedLoaded     -= OnLoaded;
        A11MediationSDK.OnRewardedLoadFailed -= OnLoadFailed;
        A11MediationSDK.OnRewardedEarned     -= OnEarned;
        A11MediationSDK.OnRewardedClosed     -= OnClosed;
        A11MediationSDK.OnRewardedShowFailed -= OnShowFailed;
        A11MediationSDK.OnRewardedShown      -= OnShown;
        A11MediationSDK.OnRewardedClicked    -= OnClicked;
        A11MediationSDK.OnRewardedRevenue    -= OnRevenue;
    }

    public void Load() => A11MediationSDK.LoadRewarded(AdUnitId);

    // Call when the player chooses to watch, for example taps "Watch ad for a reward"
    public void TryShow()
    {
        if (!_isReady) return;
        _isReady = false;
        A11MediationSDK.ShowRewarded(AdUnitId);
    }

    private void OnLoaded(string id, A11AdInfo info)
    {
        if (id != AdUnitId) return;
        _isReady = true; // Offer the reward to the player
        _retryDelay = 5f;
    }

    private void OnLoadFailed(string id, A11AdLoadError error)
    {
        if (id != AdUnitId) return;
        Debug.LogWarning($"Rewarded load failed: {error}");
        // Retry with exponential backoff: 5 s, 10 s, 20 s and so on, up to 60 s
        Invoke(nameof(Load), _retryDelay);
        _retryDelay = Mathf.Min(_retryDelay * 2f, 60f);
    }

    private void OnEarned(string id, A11AdInfo info)
    {
        if (id != AdUnitId) return;
        // Grant the reward here
        Debug.Log("Reward earned - grant coins / lives / etc.");
    }

    private void OnClosed(string id, A11AdInfo info)
    {
        if (id != AdUnitId) return;
        Load(); // Preload the next one
    }

    private void OnShowFailed(string id, A11AdInfo info, A11AdShowError error)
    {
        if (id != AdUnitId) return;
        Debug.LogWarning($"Rewarded show failed: {error}");
        Load();
    }

    private void OnShown(string id, A11AdInfo info) { }

    private void OnClicked(string id, A11AdInfo info) { }

    private void OnRevenue(string id, A11AdInfo info)
    {
        if (id != AdUnitId) return;
        Debug.Log($"Rewarded revenue: {info.Revenue} {info.RevenueCurrency}");
    }
}