Documentation

Show and hide ads during gameplay

Hide banner ads during gameplay, show interstitial ads from a script, and show a rewarded ad button only when an ad is available.
Read time 3 minutesLast updated 2 days ago

After you add ad components to your game, you can control when their ads show. This page covers how to do the following tasks:

Prerequisites

Before you begin, add the LevelPlay SDK and the ad components you need. For detailed steps, refer to Get started with LevelPlay components.

Hide a banner ad during gameplay

The Banner Ad (LevelPlay) component loads and displays a banner ad at a fixed position on the screen.
To hide the banner during gameplay, do the following:
  1. In your level-start event, call Banner Ad (LevelPlay) > HideAd ().
  2. In your level-end event, call ShowAd ().
HideAd()
hides the banner without destroying it.

Show an interstitial ad from a script

The Interstitial Ad (LevelPlay) component loads and shows a full-screen interstitial ad, for example, between levels. You can reference the component from your own scripts. Each component exposes its settings as public properties, such as
AndroidAdUnitId
,
LoadOnStart
, and
PlacementName
. It also exposes its events as public
UnityEvent
properties.
The following example shows the interstitial ad when
FinishLevel()
runs, if an ad is ready and the placement isn't capped. Otherwise, it loads the next level. When the ad closes, the
OnAdClosed
listener loads the next level.
using Unity.Services.LevelPlay;using UnityEngine;public class LevelEnd : MonoBehaviour{ [SerializeField] LevelPlayInterstitialAdBehaviour m_Interstitial; void OnEnable() => m_Interstitial.OnAdClosed.AddListener(OnInterstitialClosed); void OnDisable() => m_Interstitial.OnAdClosed.RemoveListener(OnInterstitialClosed); public void FinishLevel() { if (m_Interstitial.IsAdReady() && !m_Interstitial.IsPlacementCapped()) { m_Interstitial.ShowAd(); } else { LoadNextLevel(); } } void OnInterstitialClosed(LevelPlayAdInfo adInfo) => LoadNextLevel(); void LoadNextLevel() { /* ... */ }}
To show an interstitial ad from the script, do the following:
  1. Create a C# script named
    LevelEnd
    , paste in the example code, and attach the script to a
    GameObject
    in the scene.
  2. In the Inspector window, drag the
    GameObject
    that has the Interstitial Ad (LevelPlay) component into the Interstitial field of the LevelEnd component.
  3. Call
    FinishLevel()
    when the level ends. Use one of the following options:
    • From another script: For example, call
      levelEnd.FinishLevel();
      in your game-over logic.
    • From a
      UnityEvent
      in the Inspector window
      : For example, in a UI Button's On Click () list, add an entry, assign the
      LevelEnd
      GameObject
      , and select LevelEnd > FinishLevel ().
When the level ends, the interstitial ad shows if one is ready, and the next level loads after the ad closes. If no ad is ready, the next level loads right away.
To reach features the component doesn't expose, use the
Ad
property to get the underlying
LevelPlayBannerAd
,
LevelPlayInterstitialAd
, or
LevelPlayRewardedAd
.

Show a rewarded ad button only when an ad is available

To show the button only when an ad is available, enable the button in the On Ad Loaded event of the Rewarded Ad (LevelPlay) component, and disable it in On Ad Displayed.
To set up the button, do the following:
  1. In the Inspector window, deactivate the button
    GameObject
    by clearing the checkbox next to its name. The button stays hidden until the first ad loads.
  2. In the Hierarchy window, select the
    GameObject
    that has the Rewarded Ad (LevelPlay) component.
  3. In the Inspector window, find On Ad Loaded and select Add (+) to add an entry.
  4. Drag your button's
    GameObject
    from the Hierarchy window into the object field of the new entry.
  5. In the function dropdown, select GameObject > SetActive (bool), then select the checkbox so the event shows the button.
  6. Repeat steps 3–5 for On Ad Displayed, but leave the checkbox cleared so the event hides the button.
When an ad loads, the button appears. When the ad is displayed, the button hides again until the next ad loads.