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

    Banner Ad

    Note

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

    Use BannerAd to load and display a banner.

    Tip

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

    Declare variable

    BannerAd implements IDisposable. Calling Dispose() immediately destroys the respective View -- i.e. removes the banner from the screen.

    Warning

    Losing the reference to BannerAd instance without calling Dispose() may result in a banner being stuck on screen for the remainder of the app's lifecycle.

    Thus you must 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.Banner
    {
        [RequireComponent(typeof(SdkInitializer))]
        public class BannerDemo : MonoBehaviour
        {
            [SerializeField] private bool showVideoAd;
            public AnchoredAdPosition bannerPosition;
    
            private IBannerAd? _adUnit;
    

    For info on SdkInitializer component see SDK Initialization.

    The other two properties -- showVideoAd and bannerPosition let us customize the position and content of the banner in editor (on scene).

    banner-demo-components-in-unity-editor

    Instantiation

    Pass desired AdSize into the constructor followed by other parameters.

    _adUnit = showVideoAd
        ? new BannerAd(new(320, 250))
        {
            PlacementID = "8",
            AdUnitFormat = AdFormat.Video,
            AnchoredPosition = bannerPosition,
            AdPosition = bannerPosition.ToAdPosition(),
        }
        : new BannerAd(new(320, 250))
        {
            PlacementID = "4",
            AnchoredPosition = bannerPosition,
            AdPosition = bannerPosition.ToAdPosition(),
        };
    

    Loading the Ad

    Once all properties are set, call LoadAd().

    _adUnit.LoadAd();
    

    Listening to events

    BannerAd exposes few events that may or may not be of interest to you.

    Relatively important may be OnAdFailed.

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

    SubscribeToEvents(_adUnit);
    _adUnit.LoadAd();
    
    private void SubscribeToEvents(IBannerAd adUnit)
    {
        adUnit.OnAdLoaded += OnAdUnitLoaded;
        adUnit.OnAdFailed += OnAdUnitFailed;
        adUnit.OnAdClicked += OnAdUnitClicked;
        adUnit.OnAdClosed += OnAdUnitClosed;
    }
    
    private void OnAdUnitLoaded()
    {
        Debug.Log($"[{DateTime.Now:O}] (video: {showVideoAd}) Ad Loaded.");
    }
    
    private void OnAdUnitFailed(AdError? adError)
    {
        Debug.LogError($"[{DateTime.Now:O}] (video: {showVideoAd}) Ad Failed: {adError?.Message}.");
    }
    
    private void OnAdUnitClicked() => Debug.Log($"[{DateTime.Now:O}] (video: {showVideoAd}) Ad Clicked.");
    
    private void OnAdUnitClosed()
    {
        Debug.Log($"[{DateTime.Now:O}] (video: {showVideoAd}) Ad Closed.");
    }
    

    Hiding the banner

    Use Hide() / Show() to (temporarily?) change banner's visibility without destroying the view completely.

    public void SetHidden(bool hidden)
    {
        if (_adUnit is null)
        {
            return;
        }
        if (hidden)
        {
            _adUnit.Hide();
        }
        else
        {
            _adUnit.Show();
        }
    }
    

    Destroying the banner

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

    private void UnsubscribeFromEvents(IBannerAd adUnit)
    {
        adUnit.OnAdLoaded -= OnAdUnitLoaded;
        adUnit.OnAdFailed -= OnAdUnitFailed;
        adUnit.OnAdClicked -= OnAdUnitClicked;
        adUnit.OnAdClosed -= OnAdUnitClosed;
    }
    
    public void DropAdUnit()
    {
        if (_adUnit is null)
        {
            return;
        }
        UnsubscribeFromEvents(_adUnit);
        _adUnit.Dispose();
        _adUnit = null;
    }
    
    In this article
    Back to top Generated by DocFX