Search Results for

Show / Hide Table of Contents

# Rewarded Ad

##### Note

Make sure you've done SDK Initialization before attempting to load any ads.

Use RewardedAd to load and display an rewarded.

##### Tip

You can find a functional code in "Rewarded" sample.

## Declare variable

RewardedAd implements IDisposable.

Dispose() severs the event chain (your callbacks will no longer be called) and lets the engine free the resources (e.g. held by underlying AndroidJavaObject).

Calling Dispose() before losing the reference might have performance benefits.

Thus we shall keep a reference in some relatively persistent variable.

For example, an i-var on MonoBehaviour.

    using System;
    using System.Collections;
    using AppstockSDK.Api;
    using UnityEngine;

    #nullable enable

    namespace AppstockSDK.Demo.Rewarded
    {
        public class RewardedDemo : MonoBehaviour
        {
            [SerializeField] private bool showVideoAd;
            private IRewardedAd? _adUnit;

The other property -- `showVideoAd` lets us customize the content of the rewarded in editor (on scene).

## Instantiation

Call the constructor and set other parameters.

    _adUnit = new RewardedAd()
    {
        PlacementID = showVideoAd ? "16" : "12",
    };

## Loading the Ad

Once all properties are set, call LoadAd().

    _adUnit.LoadAd();

## Listening to events

RewardedAd exposes few events.

OnAdLoaded is useful to know you can call Show(Action?).

OnAdFailed and OnAdClosed are useful to dispose of the no-longer-useful ad unit.

OnReward is important to award users the bonuses.

In the sample the script subscribes to all events and logs whenever they are fired.

    SubscribeToEvents(_adUnit);
    _adUnit.LoadAd();

    private void SubscribeToEvents(IRewardedAd adUnit)
    {
        adUnit.OnAdLoaded += OnAdUnitLoaded;
        adUnit.OnAdDisplayed += OnAdUnitDisplayed;
        adUnit.OnAdFailed += OnAdUnitFailed;
        adUnit.OnAdClicked += OnAdUnitClicked;
        adUnit.OnAdClosed += OnAdUnitClosed;
        adUnit.OnReward += OnAdUnitRewarded;
    }

    private void OnAdUnitLoaded(AdInfo? adInfo)
    {
        Debug.Log($"[{DateTime.Now:O}] Ad Loaded. Info: {adInfo?.ToString() ?? "(null)"}.");
        _adUnit?.Show();
    }

    private void OnAdUnitFailed(AdError? adError)
    {
        Debug.LogError($"[{DateTime.Now:O}] Ad Failed: {adError?.Message}.");
        DropAdUnit();
    }

    private void OnAdUnitDisplayed() => Debug.Log($"[{DateTime.Now:O}] Ad Displayed.");
    private void OnAdUnitClicked() => Debug.Log($"[{DateTime.Now:O}] Ad Clicked.");

    private void OnAdUnitClosed()
    {
        Debug.Log($"[{DateTime.Now:O}] Ad Closed.");
        DropAdUnit();
    }
    private void OnAdUnitRewarded(AdReward? adReward) => Debug.Log($"[{DateTime.Now:O}] Ad Rewarded: {adReward}.");

## Destroying the ad unit

Unsubscribe from events, call Dispose() and clear the reference i-var.

    private void UnsubscribeFromEvents(IRewardedAd adUnit)
    {
        adUnit.OnAdLoaded -= OnAdUnitLoaded;
        adUnit.OnAdDisplayed -= OnAdUnitDisplayed;
        adUnit.OnAdFailed -= OnAdUnitFailed;
        adUnit.OnAdClicked -= OnAdUnitClicked;
        adUnit.OnAdClosed -= OnAdUnitClosed;
        adUnit.OnReward -= OnAdUnitRewarded;
    }

    private void DropAdUnit()
    {
        if (_adUnit is null)
        {
            return;
        }
        UnsubscribeFromEvents(_adUnit);
        _adUnit.Dispose();
        _adUnit = null;
    }