文档

Unity Ads Monetization


Android API reference

Unity Ads Monetization


Initialize the SDK in Android

Initialize the Unity Ads SDK in your Android app. Set your Game ID and configure a listener to handle initialization status and enable ad monetization.
阅读时间1 分钟最后更新于 5 小时前

重要
If you serve Unity Ads through a mediation partner, such as Unity LevelPlay, Google AdMob, or AppLovin MAX, follow your mediation partner's Unity Ads adapter guide instead of this direct-integration guide. In most cases, the mediation SDK initializes the Unity Ads adapter for you, so calling Unity Ads initialization from your own code risks a duplicate initialization. Install and initialize the Unity Ads SDK yourself only when your mediation partner's adapter documentation tells you to, such as on an open-source adapter path. For more information, refer to Implement Unity Ads in a mediation stack.
重要
You must configure user consent before or during SDK initialization. Serving ads without consent configuration may violate privacy regulations in some regions.
To initialize the SDK, you need your app's Game ID for the target platform. To locate it, go to the Unity Monetization Dashboard, then navigate to Monetization > Apps > [your app] > Network.
Initialize the SDK early in your project's run-time life cycle before you need to load an ad. Refer to the following code sample to understand how to initialize the Unity Ads SDK.

SDK initialization example

Use
InitializationConfiguration.Builder
to construct a configuration object with your Game ID, test mode flag, and optional mediation metadata, then pass it to
UnityAds.initialize()
along with an
InitializationListener
. The listener's
onInitializationComplete
callback receives a
UnityAdsError
if initialization fails, or
null
on success.
Only mediation adapters set mediation metadata. If you integrate Unity Ads directly, omit it.
InitializationConfiguration config = new InitializationConfiguration.Builder("YOUR_GAME_ID") .withTestMode(true) .build();InitializationListener listener = error -> { if (error == null) { // Initialization successful } else { // Handle initialization error }};UnityAds.initialize(config, listener);
Next steps: To continue your integration, refer to the Implement interstitial ads in Android documentation.