Back to Unity

Unity Ads Android SDK API reference - Kotlin and Java

en-us-ads-android-4-dot-19-dot-0-sdk-integration-api-android-api.md

latest46.4 KB
Original Source

Unity Ads Android SDK API reference - Kotlin and Java

Access the Unity Ads SDK public API reference to view available classes, methods, and properties you can use in Kotlin and Java to integrate and control ad behavior in your Android app.

Read time 16 minutes

Last updated 3 days ago


Use this reference for the Unity Ads Android SDK API in Java and Kotlin as you integrate Unity Ads into your Android applications.Before using this API, ensure that you've imported the

UnityAds

library to enable monetization in your app. Refer to Import the UnityAds library for more information.

Classes

BannerAd

The

BannerAd

class manages the loading and displaying of banner ads.

BannerLoadConfiguration

Encapsulates the options needed to load a

BannerAd

.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

placementId

| The unique identifier for your banner Placement. | Yes | |

bannerSize

| The

BannerSize

of the ad (width and height in pixels). | Yes | |

adMarkup

| Ad markup for bidding scenarios. | No | |

mediationInfo

| Mediation partner information. | No | |

mediationAdUnitId

| Used by mediation for tracking performances. | No | |

extras

| A map of additional key-value parameters. | No | |

listener

| A BannerShowListener to handle banner lifecycle events. | No |

KotlinJava

val bannerLoadConfiguration = BannerLoadConfiguration.Builder("banner\_placement", bannerSize).withAdMarkup("ad\_markup").withMediationInfo(mediationInfo).withMediationAdUnitId("mediation\_ad\_unit\_id") .withExtras(mapOf("key" to "value")) .withListener(bannerListener) .build()
BannerLoadConfiguration bannerLoadConfiguration = BannerLoadConfiguration.Builder("banner\_placement", bannerSize).withAdMarkup("ad\_markup").withMediationInfo(mediationInfo).withMediationAdUnitId("mediation\_ad\_unit\_id") .withExtras(Collections.singletonMap("key", "value")) .withListener(bannerListener) .build()

BannerLoadConfiguration.Builder

The builder class for creating a

BannerLoadConfiguration

instance.

Constructor
Builder(placementId: String, bannerSize: BannerSize)

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

placementId

| The unique identifier for your banner Placement. | Yes | |

bannerSize

| The

BannerSize

of the ad (width and height in pixels). | Yes |

Returns : A new

BannerLoadConfiguration.Builder

instance.

Builder methods

|

Method

|

Parameter

|

Required

|

Returns

|

Description

| | --- | --- | --- | --- | --- | |

withAdMarkup(String)

|

adMarkup

: Ad markup for bidding scenarios. |

No

|

Builder

| Sets the ad markup and returns the builder for chaining. | |

withMediationInfo(MediationInfo)

|

mediationInfo

: Mediation partner information. |

No

|

Builder

| Sets the mediation info and returns the builder for chaining. | |

withMediationAdUnitId(String)

|

mediationAdUnitId

: Used by mediation for tracking performances. |

No

|

Builder

| Sets the mediation ad unit ID and returns the builder for chaining. | |

withExtras(Map<String, String>)

|

extras

: A map of additional key-value parameters. Calling this multiple times will override the previous value. |

No

|

Builder

| Sets the extras map and returns the builder for chaining. | |

withListener(BannerShowListener)

|

listener

: A BannerShowListener to handle banner lifecycle events. |

No

|

Builder

| Sets the show listener and returns the builder for chaining. | |

build()

| None |

No

|

BannerLoadConfiguration

| Creates and returns the

BannerLoadConfiguration

instance with the configured parameters. |

KotlinJava

val bannerLoadConfiguration = BannerLoadConfiguration.Builder("banner\_placement", bannerSize).withAdMarkup("ad\_markup").withMediationInfo(mediationInfo).withMediationAdUnitId("mediation\_ad\_unit\_id").withExtras(mapOf("key" to "value")).withListener(bannerListener).build()
BannerLoadConfiguration bannerLoadConfiguration = new BannerLoadConfiguration.Builder("banner\_placement", bannerSize).withAdMarkup("ad\_markup").withMediationInfo(mediationInfo).withMediationAdUnitId("mediation\_ad\_unit\_id").withExtras(Collections.singletonMap("key", "value")).withListener(bannerListener).build();

load()

Loads a banner ad with the specified BannerLoadConfiguration.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

configuration

| An instance of

BannerLoadConfiguration

containing banner-specific loading options. | Yes | |

listener

| A

BannerLoadListener

that is called when loading is complete, providing either a

BannerAd

instance or an error. | Yes |

Returns :

Unit

(void) - This is an asynchronous method that returns immediately. The result is provided through the

BannerLoadListener

callback.

KotlinJava

val bannerSize = BannerSize(320, 50)val loadConfig = BannerLoadConfiguration.Builder("banner\_placement", bannerSize).build()BannerAd.load(loadConfig, { bannerAd, error -\> if (bannerAd != null) { // Banner loaded successfully, you can now add it to your view hierarchy this.bannerAd = bannerAd } else { // Handle load error }})
BannerSize bannerSize = new BannerSize(320, 50);BannerLoadConfiguration loadConfig = new BannerLoadConfiguration.Builder("banner\_placement", bannerSize).build();BannerAd.load(loadConfig, (bannerAd, error) -\> { if (bannerAd != null) { // Banner loaded successfully, you can now add it to your view hierarchy this.bannerAd = bannerAd; } else { // Handle load error }});

view

A view property that provides access to the banner ad instance. Use this property to insert the banner ad within your app's existing view hierarchy at the desired location. Returns :

View?

(nullable View) - The Android View containing the banner ad, or null if the ad has not been loaded successfully.

KotlinJava

// Assuming 'bannerAd' is a loaded BannerAd instance// Assuming 'adPlaceholder' is a ViewGroup (e.g., LinearLayout, FrameLayout) in your layout where you want to display the adbannerAd.view?.let { adPlaceholder.addView(it)}
// Assuming 'bannerAd' is a loaded BannerAd instance// Assuming 'adPlaceholder' is a ViewGroup (e.g., LinearLayout, FrameLayout) in your layout where you want to display the adif (bannerAd.getView() != null) { adPlaceholder.addView(bannerAd.getView());}

BannerView

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

BannerAd

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#bannerad) instead. Access the Android

View

via

ad.getView()

. See Android deprecated APIs for migration details.

The

BannerView

class was the previous API for loading and displaying banner ads.

BannerView.IListener

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

BannerLoadListener

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#bannerloadlistener) and [

BannerShowListener

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#bannershowlistener) instead. See Android deprecated APIs for migration details.

BannerView.IListener

was the previous interface for handling banner ad lifecycle events.

BannerSize

A data class representing the dimensions of a banner ad.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

width

| The width of the banner in pixels. | Yes | |

height

| The height of the banner in pixels. | Yes |

KotlinJava

val bannerSize = BannerSize(320, 50)
BannerSize bannerSize = new BannerSize(320, 50);

UnityBannerSize

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

BannerSize

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#bannersize) instead. See Android deprecated APIs for migration details.

UnityBannerSize

was the previous enum for banner ad dimensions.

InitializationConfiguration

A class containing all the settings for SDK initialization. An instance of this class is created using its

Builder

.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

gameId

| Your unique game identifier from the Unity Ads Monetization dashboard. | Yes | |

isTestModeEnabled

| A boolean that enables test mode. When true, only test ads are shown. Defaults to false. | No | |

logLevel

| Specifies the level of detail for log output. See

LogLevel

. Defaults to INFO. | No | |

mediationInfo

| An object containing information about the mediation partner. See

MediationInfo

. | No | |

extras

| A map of additional key-value parameters for advanced configurations. | No |

InitializationConfiguration.Builder

The builder class for creating an

InitializationConfiguration

instance.

Constructor

Builder(gameId: String)

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

gameId

| Your unique game identifier from the Unity Ads Monetization dashboard. | Yes |

Returns : A new

InitializationConfiguration.Builder

instance.

Builder methods

|

Method

|

Parameter

|

Required

|

Returns

|

Description

| | --- | --- | --- | --- | --- | |

withTestMode(Boolean)

|

isTestModeEnabled

: A boolean that enables test mode. When true, only test ads are shown. Defaults to false. |

No

|

Builder

| Sets the test mode and returns the builder for chaining. | |

withLogLevel(LogLevel)

|

logLevel

: Specifies the level of detail for log output. See

LogLevel

. Defaults to INFO. |

No

|

Builder

| Sets the log level and returns the builder for chaining. | |

withMediationInfo(MediationInfo)

|

mediationInfo

: An object containing information about the mediation partner. |

No

|

Builder

| Sets the mediation info and returns the builder for chaining. | |

withExtras(Map<String, String>)

|

extras

: A map of additional key-value parameters for advanced configurations. |

No

|

Builder

| Sets the extras map and returns the builder for chaining. | |

build()

| None |

No

|

InitializationConfiguration

| Creates and returns the

InitializationConfiguration

instance with the configured parameters. |

KotlinJava

val config = InitializationConfiguration.Builder("YOUR\_GAME\_ID").withTestMode(true).withLogLevel(LogLevel.DEBUG).withMediationInfo(mediationInfo).withExtras(mapOf("key" to "value")).build()
InitializationConfiguration config = new InitializationConfiguration.Builder("YOUR\_GAME\_ID").withTestMode(true).withLogLevel(LogLevel.DEBUG).withMediationInfo(mediationInfo).withExtras(Collections.singletonMap("key", "value")).build();

initialize()

Initializes the Unity Ads SDK with a specified configuration. The

InitializationListener

is called when initialization completes with either success or failure.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

configuration

| The

InitializationConfiguration

object that contains the required settings to initialize the SDK. | No | |

listener

| The

InitializationListener

is called upon completion, indicating success or detailing the reason for failure with an optional error message. | No |

Returns :

Unit

(void) - This is an asynchronous method that returns immediately. The result is provided through the

InitializationListener

callback.

KotlinJava

@OptIn(UnityAdsExperimental::class)fun initializeUnityAds(context: Context) { val config = InitializationConfiguration.Builder("YOUR\_GAME\_ID") .withTestMode(true) .build() val listener = InitializationListener { error -\> if (error == null) { // Initialization successful } else { // Handle initialization error } } UnityAds.initialize(config, listener)}
@OptIn(markerClass = UnityAdsExperimental.class)void initializeUnityAds(Context context) { 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);}

InterstitialAd

The

InterstitialAd

class manages the loading and showing of full-screen interstitial ads.

load()

Loads an interstitial ad with the specified LoadConfiguration.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

configuration

| An instance of

LoadConfiguration

containing the ad loading options. | No | |

listener

| A

LoadListener

callback that is called when loading is complete. Returns an

InterstitialAd

instance if successful, or an Error if loading fails. | No |

KotlinJava

InterstitialAd.load(loadConfig, { interstitialAd, error -\> if (interstitialAd != null) { // Ad loaded successfully, ready to be shown this.interstitialAd = interstitialAd } else { // Handle load error }})
InterstitialAd.load(loadConfig, (interstitialAd, error) -\> { if (interstitialAd != null) { // Ad loaded successfully, ready to be shown this.interstitialAd = interstitialAd; } else { // Handle load error }});

show()

Displays a previously loaded interstitial ad.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

configuration

| An instance of

ShowConfiguration

containing ad display options. | No | |

listener

| An optional

ShowListener

to handle ad lifecycle events. | No |

Returns :

Unit

(void) - This method returns immediately after starting the ad display process. Lifecycle events are provided through the

ShowListener

callback.

KotlinJava

interstitialAd.show(showConfig, interstitialShowListener)
interstitialAd.show(showConfig, interstitialShowListener);

LoadConfiguration

Encapsulates the options for loading

InterstitialAd

and

RewardedAd

formats.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

placementId

| The unique identifier for your ad Placement, found on the Unity Monetization Dashboard. | Yes | |

adMarkup

| Ad markup for bidding scenarios. | No | |

mediationInfo

| Mediation partner information. | No | |

mediationAdUnitId

| Used by mediation for tracking performances. | No | |

extras

| A map of additional key-value parameters. | No |

KotlinJava

val loadConfiguration = LoadConfiguration.Builder("interstitial\_placement").withAdMarkup("ad\_markup").withMediationInfo(mediationInfo).withMediationAdUnitId("mediation\_ad\_unit\_id") .withExtras(mapOf("key" to "value")) // Calling this multiple time will override the previous value. .build()
LoadConfiguration loadConfiguration = new LoadConfiguration.Builder("interstitial\_placement").withAdMarkup("ad\_markup").withMediationInfo(mediationInfo).withMediationAdUnitId("mediation\_ad\_unit\_id") .withExtras(Collections.singletonMap("key", "value")) .build();

LoadConfiguration.Builder

The builder class for creating a

LoadConfiguration

instance.

Constructor

Builder(placementId: String)

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

placementId

| The unique identifier for your ad Placement, found on the Unity Monetization Dashboard. | Yes |

Returns : A new

LoadConfiguration.Builder

instance.

Builder methods

|

Method

|

Parameter

|

Required

|

Returns

|

Description

| | --- | --- | --- | --- | --- | |

withAdMarkup(String)

|

adMarkup

: Ad markup for bidding scenarios. | No |

Builder

| Sets the ad markup and returns the builder for chaining. | |

withMediationInfo(MediationInfo)

|

mediationInfo

: Mediation partner information. | No |

Builder

| Sets the mediation info and returns the builder for chaining. | |

withMediationAdUnitId(String)

|

mediationAdUnitId

: Used by mediation for tracking performances. | No |

Builder

| Sets the mediation ad unit ID and returns the builder for chaining. | |

withExtras(Map<String, String>)

|

extras

: A map of additional key-value parameters. Calling this multiple times will override the previous value. | No |

Builder

| Sets the extras map and returns the builder for chaining. | |

build()

| None | No |

LoadConfiguration

| Creates and returns the

LoadConfiguration

instance with the configured parameters. |

UnityAdsLoadOptions

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

LoadConfiguration.Builder

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#loadconfiguration.builder) instead. See Android deprecated APIs for migration details.

UnityAdsLoadOptions

was the previous class for specifying ad load options.

MediationInfo

A class containing information about the mediation partner being used.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

name

| The name of the mediation partner. | Yes | |

version

| The version of the mediation partner's SDK. | Yes | |

adapterVersion

| The version of the Unity Ads adapter for the mediation partner. | Yes |

KotlinJava

val mediationInfo = MediationInfo("partner\_name", "1.2.3", "4.5.6")
MediationInfo mediationInfo = new MediationInfo("partner\_name", "1.2.3", "4.5.6");

MediationMetaData

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

MediationInfo

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#mediationinfo) instead. See Android deprecated APIs for migration details.

MediationMetaData

was the previous class for passing mediation partner information.

RewardedAd

The

RewardedAd

class manages the loading and showing of rewarded video ads.

load()

Loads a rewarded ad with the specified

LoadConfiguration

.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

configuration

| An instance of

LoadConfiguration

containing the ad loading options. | Yes | |

listener

| A

LoadListener

that is called when loading is complete, providing either a RewardedAd instance or an error. | Yes |

KotlinJava

RewardedAd.load(loadConfig, { rewardedAd, error -\> if (rewardedAd != null) { // Ad loaded successfully, ready to be shown this.rewardedAd = rewardedAd } else { // Handle load error }})
RewardedAd.load(loadConfig, (rewardedAd, error) -\> { if (rewardedAd != null) { // Ad loaded successfully, ready to be shown this.rewardedAd = rewardedAd; } else { // Handle load error }});

show()

Displays a previously loaded rewarded ad.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

configuration

| An instance of

ShowConfiguration

containing ad display options. | Yes | |

listener

| An optional

ShowListener

to handle ad events, including the user reward callback. | Yes |

Returns :

Unit

(void) - This method returns immediately after starting the ad display process. Lifecycle events are provided through the

ShowListener

callback.

KotlinJava

rewardedAd.show(showConfig, rewardedAdShowListener)
rewardedAd.show(showConfig, rewardedAdShowListener);

ShowConfiguration

Encapsulates additional options for showing

InterstitialAd

and

RewardedAd

formats.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

customRewardString

| Used to track completion. | No | |

extras

| A map of additional key-value parameters passed at the time of showing an ad. | No |

KotlinJava

val showConfiguration = ShowConfiguration.Builder().withCustomRewardString("custom\_reward\_string").withExtras(mapOf("key" to "value")).build()
ShowConfiguration showConfiguration = new ShowConfiguration.Builder().withCustomRewardString("custom\_reward\_string").withExtras(Collections.singletonMap("key", "value")).build();

ShowConfiguration.Builder

The builder class for creating a

ShowConfiguration

instance.

Constructor

Builder()

Returns : A new

ShowConfiguration.Builder

instance.

Builder methods

|

Method

|

Parameter

|

Required

|

Returns

|

Description

| | --- | --- | --- | --- | --- | |

withCustomRewardString(String)

|

customRewardString

: Used to track completion. |

No

|

Builder

| Sets the custom reward string and returns the builder for chaining. | |

withExtras(Map<String, String>)

|

extras

: A map of additional key-value parameters passed at the time of showing an ad. |

No

|

Builder

| Sets the extras map and returns the builder for chaining. | |

build()

| None |

Not applicable

|

ShowConfiguration

| Creates and returns the

ShowConfiguration

instance with the configured parameters. |

UnityAdsShowOptions

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

ShowConfiguration.Builder

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#showconfiguration.builder) instead. See Android deprecated APIs for migration details.

UnityAdsShowOptions

was the previous class for specifying ad show options.

TokenConfiguration

A class containing the settings for generating a bidding token.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

adFormat

| The AdFormat for which to generate a token (e.g.,

REWARDED

,

INTERSTITIAL

). | Yes | |

mediationInfo

| An object containing information about the mediation partner. See

MediationInfo

. | No | |

extras

| A map of additional key-value parameters. | No |

KotlinJava

val tokenConfig = TokenConfiguration.Builder(AdFormat.REWARDED).withMediationInfo(mediationInfo).withExtras(extras).build()
TokenConfiguration tokenConfig = new TokenConfiguration.Builder(AdFormat.REWARDED).withMediationInfo(mediationInfo).withExtras(extras).build();

TokenConfiguration.Builder

The builder class for creating a

TokenConfiguration

instance.

Constructor

Builder(adFormat: AdFormat)

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

adFormat

| The AdFormat for which to generate a token (e.g.,

REWARDED

,

INTERSTITIAL

,

BANNER

). | Yes |

Returns : A new

TokenConfiguration.Builder

instance.

Builder methods

|

Method

|

Parameter

|

Required

|

Returns

|

Description

| | --- | --- | --- | --- | --- | |

withMediationInfo(MediationInfo)

|

mediationInfo

: An object containing information about the mediation partner. |

No

|

Builder

| Sets the mediation info and returns the builder for chaining. | |

withExtras(Map<String, String>)

|

extras

: A map of additional key-value parameters. |

No

|

Builder

| Sets the extras map and returns the builder for chaining. | |

build()

| None |

Not applicable

|

TokenConfiguration

| Creates and returns the

TokenConfiguration

instance with the configured parameters. |

getToken

Fetches a bidding token based on the provided

TokenConfiguration

and passes it to the token listener.

|

Parameter

|

Description

|

Required

| | --- | --- | --- | |

configuration

| A

TokenConfiguration

object that specifies the ad format, mediation info, and other parameters. | Yes | |

listener

| A listener that is called with the token if it's successfully fetched, or null if an error occurs. | Yes |

Returns :

Unit

(void) - This is an asynchronous method that returns immediately. The result is provided through the

TokenListener

callback.

KotlinJava

UnityAds.getToken(tokenConfig, listener)
UnityAds.getToken(tokenConfig, listener);

UnityAds

The UnityAds class is a static class that serves as the primary entry point for the SDK. It handles initialization, token retrieval, and provides access to global SDK properties.

Static variables

isInitialized

A boolean property that determines if the Unity Ads SDK is initialized successfully. Returns : true if the Unity Ads SDK has been successfully initialized.

KotlinJava

val isInitialized = UnityAds.isInitialized
boolean isInitialized = UnityAds.isInitialized();

version

A string property that returns the current version of the Unity Ads SDK. Returns : The current installed version of Unity Ads SDK.

KotlinJava

val sdkVersion = UnityAds.version
String sdkVersion = UnityAds.getVersion();

userIdentifier

A string property that represents a unique ID for each user of your app. Returns : The current value.

KotlinJava

UnityAds.userIdentifier = "my-user-id"
UnityAds.userIdentifier = "my-user-id";

Privacy

Use the following methods to set privacy and user consent flags, helping you comply with supported privacy regulations.

setUserConsent()

Displays the user's opt-in choices for personalized ads.

setUserOptOut()

Offers users the option to opt out of data collection and personalized ads.

setNonBehavioral()

Enables the display of contextual (non-personalized) ads.

KotlinJava

UnityAds.userConsent = trueUnityAds.userOptOut = falseUnityAds.nonBehavioral = false
UnityAds.setUserConsent(true);UnityAds.setUserOptOut(false);UnityAds.setNonBehavioral(false);

MetaData

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use the typed privacy methods on [

UnityAds

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#unityads):

setUserConsent()

,

setUserOptOut()

, and

setNonBehavioral()

. See Android deprecated APIs for migration details.

MetaData

was the previous class for setting GDPR and other privacy-related flags.

UnityAdsError

class UnityAdsError internal constructor( val code: Int, val message: String)

An exception returned by the public in case of error from Unity Ads.

|

Parameter

|

Description

| | --- | --- | |

message: String

| The message associated with the error. | |

code: Int

| The code for the error. |

BannerErrorCode

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use

UnityAdsError

instead. See Android deprecated APIs for migration details.

BannerErrorCode

was the previous enum for banner-specific error codes.

BannerErrorInfo

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use

UnityAdsError

instead. See Android deprecated APIs for migration details.

BannerErrorInfo

was the previous class encapsulating banner error details.

UnityAds.UnityAdsInitializationError

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use

UnityAdsError

instead. See Android deprecated APIs for migration details.

UnityAds.UnityAdsInitializationError

was the previous nested enum for initialization error types.

UnityAds.UnityAdsLoadError

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use

UnityAdsError

instead. See Android deprecated APIs for migration details.

UnityAds.UnityAdsLoadError

was the previous nested enum for load error types.

UnityAds.UnityAdsShowError

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use

UnityAdsError

instead. See Android deprecated APIs for migration details.

UnityAds.UnityAdsShowError

was the previous nested enum for show error types.

Interfaces

BannerLoadListener

An interface for handling the result of loading a

BannerAd

. This listener is called when a banner ad load request completes, providing either a successfully loaded

BannerAd

instance or an error.

Method signature

fun interface BannerLoadListener { fun onBannerLoaded(bannerAd: BannerAd?, error: UnityAdsError?)}

|

Method

|

Parameters

|

Description

| | --- | --- | --- | |

onBannerLoaded

|

bannerAd

: The loaded

BannerAd

instance if successful, or null if loading failed.

error

: The

UnityAdsError

describing the failure, or null if loading succeeded. | Called when the banner ad load request completes. Exactly one of the parameters will be non-null. |

KotlinJava

val bannerLoadListener = BannerLoadListener { bannerAd, error -\> if (bannerAd != null) { // Banner loaded successfully this.bannerAd = bannerAd // Add banner.view to your view hierarchy } else { // Handle load error Log.e("UnityAds", "Banner load failed: ${error?.message}") }}
BannerLoadListener bannerLoadListener = (bannerAd, error) -\> { if (bannerAd != null) { // Banner loaded successfully this.bannerAd = bannerAd; // Add banner.getView() to your view hierarchy } else { // Handle load error Log.e("UnityAds", "Banner load failed: " + error.getMessage()); }};

BannerShowListener

An interface for handling show lifecycle events for

BannerAd

. This listener is called at various stages during the banner's display lifecycle.

Method signatures

interface BannerShowListener { fun onBannerShown(bannerAd: BannerAd) fun onBannerClicked(bannerAd: BannerAd) fun onBannerFailedToShow(bannerAd: BannerAd, error: UnityAdsError)}

|

Method

|

Parameters

|

Description

| | --- | --- | --- | |

onBannerShown

|

bannerAd

: The

BannerAd

instance that was shown. | Called when the banner ad is successfully shown on screen. | |

onBannerClicked

|

bannerAd

: The

BannerAd

instance that was clicked. | Called when the user clicks on the banner ad. | |

onBannerFailedToShow

|

bannerAd

: The

BannerAd

instance that failed to show.

error

: The

UnityAdsError

describing the failure. | Called when the banner ad fails to show. |

KotlinJava

val bannerShowListener = object : BannerShowListener { override fun onBannerShown(bannerAd: BannerAd) { Log.d("UnityAds", "Banner shown") } override fun onBannerClicked(bannerAd: BannerAd) { Log.d("UnityAds", "Banner clicked") } override fun onBannerFailedToShow(bannerAd: BannerAd, error: UnityAdsError) { Log.e("UnityAds", "Banner failed to show: ${error.message}") }}
BannerShowListener bannerShowListener = new BannerShowListener() { @Override public void onBannerShown(BannerAd bannerAd) { Log.d("UnityAds", "Banner shown"); } @Override public void onBannerClicked(BannerAd bannerAd) { Log.d("UnityAds", "Banner clicked"); } @Override public void onBannerFailedToShow(BannerAd bannerAd, UnityAdsError error) { Log.e("UnityAds", "Banner failed to show: " + error.getMessage()); }};

InitializationListener

An interface for receiving callbacks about the status of SDK initialization. This listener is called when the SDK initialization process completes.

Method signature

fun interface InitializationListener { fun onInitializationComplete(error: UnityAdsError?)}

|

Method

|

Parameters

|

Description

| | --- | --- | --- | |

onInitializationComplete

|

error

: The

UnityAdsError

describing the failure, or null if initialization succeeded. | Called when SDK initialization completes. If error is null, initialization was successful. |

KotlinJava

val listener = InitializationListener { error -\> if (error == null) { Log.d("UnityAds", "SDK initialized successfully.") } else { Log.e("UnityAds", "SDK initialization failed: ${error.message}") }}
InitializationListener listener = error -\> { if (error == null) { Log.d("UnityAds", "SDK initialized successfully."); } else { Log.e("UnityAds", "SDK initialization failed: " + error.getMessage()); }};

IUnityAdsInitializationListener

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

InitializationListener

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#initializationlistener) instead. See Android deprecated APIs for migration details.

IUnityAdsInitializationListener

was the previous callback interface for SDK initialization.

InterstitialLoadListener

An interface for handling the result of loading an

InterstitialAd

. This listener is called when an interstitial ad load request completes, providing either a successfully loaded

InterstitialAd

instance or an error.

Method signature

fun interface InterstitialLoadListener { fun onInterstitialLoaded(interstitialAd: InterstitialAd?, error: UnityAdsError?)}

|

Method

|

Parameters

|

Description

| | --- | --- | --- | |

onInterstitialLoaded

|

interstitialAd

: The loaded

InterstitialAd

instance if successful, or null if loading failed.

error

: The

UnityAdsError

describing the failure, or null if loading succeeded. | Called when the interstitial ad load request completes. Exactly one of the parameters will be non-null. |

KotlinJava

val interstitialLoadListener = InterstitialLoadListener { interstitialAd, error -\> if (interstitialAd != null) { // Interstitial ad loaded successfully this.interstitialAd = interstitialAd // You can now show the ad } else { // Handle load error Log.e("UnityAds", "Interstitial load failed: ${error?.message}") }}
InterstitialLoadListener interstitialLoadListener = (interstitialAd, error) -\> { if (interstitialAd != null) { // Interstitial ad loaded successfully this.interstitialAd = interstitialAd; // You can now show the ad } else { // Handle load error Log.e("UnityAds", "Interstitial load failed: " + error.getMessage()); }};

RewardedLoadListener

An interface for handling the result of loading a

RewardedAd

. This listener is called when a rewarded ad load request completes, providing either a successfully loaded

RewardedAd

instance or an error.

Method signature

fun interface RewardedLoadListener { fun onRewardedLoaded(rewardedAd: RewardedAd?, error: UnityAdsError?)}

|

Method

|

Parameters

|

Description

| | --- | --- | --- | |

onRewardedLoaded

|

rewardedAd

: The loaded

RewardedAd

instance if successful, or null if loading failed.

error

: The

UnityAdsError

describing the failure, or null if loading succeeded. | Called when the rewarded ad load request completes. Exactly one of the parameters will be non-null. |

KotlinJava

val rewardedLoadListener = RewardedLoadListener { rewardedAd, error -\> if (rewardedAd != null) { // Rewarded ad loaded successfully this.rewardedAd = rewardedAd // You can now show the ad } else { // Handle load error Log.e("UnityAds", "Rewarded ad load failed: ${error?.message}") }}
RewardedLoadListener rewardedLoadListener = (rewardedAd, error) -\> { if (rewardedAd != null) { // Rewarded ad loaded successfully this.rewardedAd = rewardedAd; // You can now show the ad } else { // Handle load error Log.e("UnityAds", "Rewarded ad load failed: " + error.getMessage()); }};

IUnityAdsLoadListener

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

InterstitialLoadListener

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#interstitialloadlistener) or [

RewardedLoadListener

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#rewardedloadlistener) instead. See Android deprecated APIs for migration details.

IUnityAdsLoadListener

was the previous callback interface for ad loading.

ShowListener

An interface for handling show lifecycle events for

ShowAd

or

RewardedAd

. This generic interface provides callbacks for various stages of the ad display lifecycle.

Method signatures

interface ShowListener\<T\> { fun onStarted(unityAd: T) fun onClicked(unityAd: T) fun onCompleted(unityAd: T, state: ShowFinishState) fun onFailed(unityAd: T, error: UnityAdsError)}interface InterstitialShowListener : ShowListener\<InterstitialAd\>// Specialized listener for RewardedAd with reward callbackinterface RewardedShowListener : ShowListener\<RewardedAd\> { fun onRewarded(unityAd: RewardedAd)}

|

Method

|

Parameters

|

Description

| | --- | --- | --- | |

onStarted

|

unityAd

: The ad instance (

InterstitialAd

or

RewardedAd

) that started showing. | Called when the ad begins displaying on screen. | |

onClicked

|

unityAd

: The ad instance that was clicked. | Called when the user clicks on the ad. | |

onCompleted

|

unityAd

: The ad instance that completed.

state

: The

ShowFinishState

indicating if the ad was

COMPLETED

or

SKIPPED

. | Called when the ad finishes showing. For rewarded ads,

COMPLETED

state indicates the user should receive the reward. | |

onFailed

|

unityAd

: The ad instance that failed to show.

error

: The

UnityAdsError

describing the failure. | Called when the ad fails to show. | |

onRewarded

(RewardedShowListener only) |

unityAd

: The

RewardedAd

instance for which the reward should be granted. | Called when the user has earned a reward by watching the rewarded ad to completion. |

KotlinJava

val interstitialListener = object : InterstitialShowListener { override fun onStarted(unityAd: InterstitialAd) { // Do something } override fun onClicked(unityAd: InterstitialAd) { // Do something } override fun onCompleted(unityAd: InterstitialAd, state: ShowFinishState) { // Do something } override fun onFailed(unityAd: InterstitialAd, error: UnityAdsError) { // Do something }}val rewardedListener = object : RewardedShowListener { override fun onStarted(unityAd: RewardedAd) { // Do something } override fun onClicked(unityAd: RewardedAd) { // Do something } override fun onCompleted(unityAd: RewardedAd, state: ShowFinishState) { // Do something } override fun onFailed(unityAd: RewardedAd, error: UnityAdsError) { // Do something } override fun onRewarded(unityAd: RewardedAd) { // Do something }}
ShowListener\<InterstitialAd\> interstitialListener = new InterstitialShowListener() { @Override public void onStarted(InterstitialAd unityAd) { // Do something } @Override public void onClicked(InterstitialAd unityAd) { // Do something } @Override public void onCompleted(InterstitialAd unityAd, ShowFinishState state) { // Do something } @Override public void onFailed(InterstitialAd unityAd, UnityAdsError error) { // Do something }};ShowListener\<RewardedAd\> rewardedListener = new RewardedShowListener() { @Override public void onStarted(RewardedAd unityAd) { // Do something } @Override public void onClicked(RewardedAd unityAd) { // Do something } @Override public void onCompleted(RewardedAd unityAd, ShowFinishState state) { // Do something } @Override public void onFailed(RewardedAd unityAd, UnityAdsError error) { // Do something } @Override public void onRewarded(RewardedAd unityAd) { // Do something }};

IUnityAdsShowListener

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

ShowListener

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#showlistener) instead. See Android deprecated APIs for migration details.

IUnityAdsShowListener

was the previous callback interface for ad show events.

TokenListener

An interface for receiving the result of a

getToken

call. This listener is called when a bidding token request completes.

Method signature

fun interface TokenListener { fun onTokenReady(token: String?)}

|

Method

|

Parameters

|

Description

| | --- | --- | --- | |

onTokenReady

|

token

: The bidding token as a String if successful, or null if token generation failed. | Called when the token request completes. The token should be passed to your mediation partner's SDK for bidding. |

KotlinJava

val listener = TokenListener { token -\> if (token != null) { // Pass the token to your mediation partner's SDK Log.d("UnityAds", "Token received: $token") } else { // Handle token retrieval failure Log.e("UnityAds", "Token retrieval failed") }}
TokenListener listener = token -\> { if (token != null) { // Pass the token to your mediation partner's SDK Log.d("UnityAds", "Token received: " + token); } else { // Handle token retrieval failure Log.e("UnityAds", "Token retrieval failed"); }};

onAdExpired

A method available on

InterstitialAd

and

RewardedAd

instances that allows you to register a callback to handle ad expiration events. This callback is triggered when the ad is no longer valid and should not be shown.

Method signature

// On InterstitialAdfun onAdExpired(callback: (InterstitialAd) -\> Unit)// On RewardedAdfun onAdExpired(callback: (RewardedAd) -\> Unit)

|

Parameter

|

Description

| | --- | --- | |

callback

| (Required) A function that receives the expired ad instance when the ad expires. |

Returns :

Unit

(void) - This method registers the callback and returns immediately.

KotlinJava

// For InterstitialAdinterstitialAd.onAdExpired { ad -\> // Handle ad expiration - load a new ad Log.d("UnityAds", "Interstitial ad expired") // Load a new ad}// For RewardedAdrewardedAd.onAdExpired { ad -\> // Handle ad expiration - load a new ad Log.d("UnityAds", "Rewarded ad expired") // Load a new ad}
// For InterstitialAdinterstitialAd.onAdExpired(ad -\> { // Handle ad expiration - load a new ad Log.d("UnityAds", "Interstitial ad expired"); // Load a new ad});// For RewardedAdrewardedAd.onAdExpired(ad -\> { // Handle ad expiration - load a new ad Log.d("UnityAds", "Rewarded ad expired"); // Load a new ad});

Enums

AdFormat

Enumerated types for different ad formats.

|

Value

|

Description

| | --- | --- | |

INTERSTITIAL

| A full-screen ad shown at natural breaks in the app, which can be skipped after a few seconds. | |

REWARDED

| A full-screen ad that users opt-in to watch in exchange for an in-app reward. | |

BANNER

| An ad that occupies a portion of the screen, typically at the top or bottom. |

LogLevel

Enumerated types for different log verbosity levels.

|

Value

|

Description

| | --- | --- | |

DISABLED

| No logs related to Unity Ads should be printed. | |

INFO

| Default level. Shows standard integration and lifecycle messages. | |

DEBUG

| Shows detailed logs for debugging purposes. | |

ERROR

| Essential errors from init/load/show operations, and critical failures to which a developer must pay attention. |

ShowFinishState

Enumerated types describing the final state of an ad after it has been shown.

|

Value

|

Description

| | --- | --- | |

SKIPPED

| The user skipped the ad before it finished. | |

COMPLETED

| The ad was watched to completion. For

RewardedAd

, this state triggers the reward. |

UnityAds.UnityAdsShowCompletionState

Warning

Deprecated in version 4.19.0. Scheduled for removal in version 5.0.0. Use [

ShowFinishState

](/en-us/ads-android/4.19.0/sdk-integration/api/android-api#showfinishstate) instead. See Android deprecated APIs for migration details.

UnityAds.UnityAdsShowCompletionState

was the previous nested enum for the final state of an ad show.