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:
- Hide a banner ad during gameplay
- Show an interstitial ad from a script
- Show a rewarded ad button only when an ad is available
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:
- In your level-start event, call Banner Ad (LevelPlay) > HideAd ().
- In your level-end event, call ShowAd ().
HideAd()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 , , and . It also exposes its events as public properties.
AndroidAdUnitIdLoadOnStartPlacementNameUnityEventThe following example shows the interstitial ad when runs, if an ad is ready and the placement isn't capped. Otherwise, it loads the next level. When the ad closes, the listener loads the next level.
FinishLevel()OnAdClosedusing 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:
- Create a C# script named , paste in the example code, and attach the script to a
LevelEndin the scene.GameObject - In the Inspector window, drag the that has the Interstitial Ad (LevelPlay) component into the Interstitial field of the LevelEnd component.
GameObject - Call when the level ends. Use one of the following options:
FinishLevel()- From another script: For example, call in your game-over logic.
levelEnd.FinishLevel(); - From a in the Inspector window: For example, in a UI Button's On Click () list, add an entry, assign the
UnityEventLevelEnd, and select LevelEnd > FinishLevel ().GameObject
- From another script: For example, call
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 property to get the underlying , , or .
AdLevelPlayBannerAdLevelPlayInterstitialAdLevelPlayRewardedAdShow 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:
- In the Inspector window, deactivate the button by clearing the checkbox next to its name. The button stays hidden until the first ad loads.
GameObject - In the Hierarchy window, select the that has the Rewarded Ad (LevelPlay) component.
GameObject - In the Inspector window, find On Ad Loaded and select Add (+) to add an entry.
- Drag your button's from the Hierarchy window into the object field of the new entry.
GameObject - In the function dropdown, select GameObject > SetActive (bool), then select the checkbox so the event shows the button.
- 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.