Appstock for Unity
  • Documentation
  • API Reference
Search Results for

    Show / Hide Table of Contents
    • Overview
    • Package Contents
    • Installation
    • Requirements and limitations
      • Requirements
      • Unity 2021/2022 Android issues
      • iOS blocks insecure requests
      • iOS Simulator on arm64
    • Displaying Ads
      • SDK Initialization
      • Banner Ad
      • Interstitial Ad
      • Rewarded Ad
      • Native Ads
        • Introduction to Native Ads
        • Building Native Ad Request
        • Applying Native Ad Response
    • Advanced Configuration
      • SDK Configuration
      • Improving targeting
      • Ext Slot
    • Samples

    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).

    rewarded-demo-component-in-unity-editor

    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;
    }
    
    In this article
    Back to top Generated by DocFX