Get started with LevelPlay components
Add the LevelPlay SDK and a rewarded ad to your game from the Unity Editor, and configure privacy settings before the SDK initializes.
Read time 5 minutesLast updated 20 hours ago
LevelPlay components let you add the LevelPlay SDK and banner, interstitial, and rewarded ads to your game from the Unity Editor, without writing integration code. This page shows you how to add a rewarded ad and grant its reward. To add a banner or interstitial ad, follow the same steps and select that component's menu item instead.
Some APIs must be called before the SDK initializes. If your game uses privacy settings, test suite metadata, or the global impression data subscription, refer to Configure privacy, test suite, and impression data before initialization.
For more information on how the components work, refer to Introduction to LevelPlay components.
Prerequisites
Before you begin, make sure you meet the following requirements:
- The Ads Mediation package () is installed in your project. For more information, refer to Unity Package integration.
com.unity.services.levelplay - An app is set up in the LevelPlay dashboard, and you have its App Key for each platform you target (Android, iOS, or both).
- An ad unit is created in the LevelPlay dashboard for each ad format you want to show, and you have its Ad Unit ID for each platform.
Add a rewarded ad to your game
To add the LevelPlay SDK and a rewarded ad to your game, do the following:
- Open the scene where your game starts, for example your boot or main menu scene.
- Select GameObject > Monetization > LevelPlay SDK. This creates a named
GameObject.LevelPlay - In the Inspector window, enter your Android App Key, iOS App Key, or both.
- Select GameObject > Monetization > Rewarded Ad (LevelPlay). To add a different ad format, select Banner Ad (LevelPlay) or Interstitial Ad (LevelPlay) instead.
- In the Inspector window, enter the Android Ad Unit ID, iOS Ad Unit ID, or both.
- Connect the ad to your game:
- To show the ad, select a UI Button and add an entry to its On Click () list. Drag the Rewarded Ad (LevelPlay) into the object field and select LevelPlayRewardedAdBehaviour > ShowAd ().
GameObject - To grant the reward, add an entry to the On Ad Rewarded event on the Rewarded Ad (LevelPlay) component, and select the method on your that grants the reward. For an example, refer to Grant a reward with a script.
GameObject
- To show the ad, select a UI Button and add an entry to its On Click () list. Drag the Rewarded Ad (LevelPlay)
- Enter Play mode.
When you enter Play mode, the SDK initializes, the ad loads automatically, and selecting the button shows the ad. In the Unity Editor, LevelPlay simulates initialization and shows mock ads, so you can test your event connections without a device. In Play mode, each component's Inspector window shows a Play Mode Controls section.
For the properties of each ad component, refer to the Banner Ad (LevelPlay), Interstitial Ad (LevelPlay), and Rewarded Ad (LevelPlay) sections of LevelPlay component reference.
For the public methods and events of each component, refer to the Banner Ad (LevelPlay) component API, Interstitial Ad (LevelPlay) component API, and Rewarded Ad (LevelPlay) component API.
If the Console window shows an error message, refer to Troubleshoot ad initialization and display.
Grant a reward with a script
Create a script that grants the reward. The following example adds the reward amount to :
Coinsusing Unity.Services.LevelPlay;using UnityEngine;public class CoinWallet : MonoBehaviour{ public int Coins; public void GrantReward(LevelPlayAdInfo adInfo, LevelPlayReward reward) { Coins += reward.Amount; }}
To connect the script to the reward, do the following:
- On the Rewarded Ad (LevelPlay) component, add an entry to On Ad Rewarded.
- Drag in the that has the
GameObjectcomponent.CoinWallet - Select CoinWallet > GrantReward from the Dynamic section of the list.
Configure privacy, test suite, and impression data before initialization
The following APIs must be called before the SDK initializes:
- Privacy settings ()
LevelPlayPrivacySettings - Test suite metadata ()
LevelPlay.SetMetaData("is_test_suite", "enable") - The global subscription
LevelPlay.OnImpressionDataReady
To make sure these APIs run before the SDK initializes, use one of the following options:
- Call the APIs before the SDK starts: Keep Initialize On Start enabled and call the APIs from in a script in the same scene.
Awake() - Initialize the SDK: Disable Initialize On Start, call the APIs, and then call yourself.
Initialize()
Call the APIs before the SDK starts
Unity runs on every object before it runs the SDK component's .
Awake()Start()To call the APIs before the SDK initializes, do the following:
- Keep Initialize On Start enabled.
- Call the APIs from in one of your scripts in the same scene.
Awake()
The following example sets GDPR and CCPA consent in :
Awake()using Unity.Services.LevelPlay;using UnityEngine;public class AdsPrivacySetup : MonoBehaviour{ void Awake() { // Pass the consent choices the player recorded in your consent dialog. LevelPlayPrivacySettings.SetGDPRConsent(true); LevelPlayPrivacySettings.SetCCPA(false); }}
For more information about privacy settings, refer to Regulation advanced settings for Unity.
Initialize the SDK
To initialize the SDK manually, do the following:
- On the LevelPlay SDK component, disable Initialize On Start.
- Call on the component when you're ready, for example after the player answers your consent dialog. You can call it from a
Initialize(), such as a button's On Click (), or from code.UnityEvent
Next steps
To control when ads show, refer to Show and hide ads during gameplay.