Playable and interactive ads
Deliver interactive playable ads through Unity Exchange.
Read time 6 minutesLast updated 4 hours ago
Playable ads are the second-most popular type of ad shown on Unity Exchange. Because all Unity inventory is mobile in-app projects, the majority of it supports playable ads.
MRAID
Mobile Rich Media Ad Interface Definitions (MRAID) is the common API for mobile rich media ads that run in mobile apps. MRAID provides a way for ad developers to design interactive ads that work in MRAID-compliant systems, using common code. MRAID controls how ads interact in the apps where they appear.Identification and initialization
To ensure your ads are compatible with the Unity MRAID interface:- Identify them as being MRAID compliant by including an MRAID script tag () as early as possible in the ad's code, before referencing any MRAID functions.
<script src="mraid.js"></script>
- Use the function to detect when the MRAID state changes from "loading" to "ready".
addEventListener
- Use the method to verify a "ready" state before showing the ad. This prevents timing issues, such as the ready event firing before the ad has been registered.
getState
Access thefunction showMyAd() { // Ad content } if (mraid.getState() === ‘loading') { mraid.addEventListener(‘ready', showMyAd); } else { showMyAd(); }
MRAID_ENV
Attribute | Type | Example | Description |
---|---|---|---|
| string |
| The version of the implemented MRAID interface. |
| string |
| The name of the SDK running the webview. |
| string |
| The version of the SDK running the webview. |
| string |
| The package name or application ID of the app running the ad. |
| boolean |
|
|
| boolean |
|
|
For more information on how to identify and initialize MRAID functionality, refer to the IAB documentation.
Events
Ads must register event listeners to receive any of the following events. For more information, refer to theaddEventListener
removeEventListener
error
This event triggers whenever the host cannot execute a function called by the ad.error(message: string, action: string): void
Parameter | Type | Description |
---|---|---|
| string | A description of the error. |
| string | The name of the MRAID action called when the error triggered. |
ready
This event triggers when initialization completes.ready()
sizeChange
This event triggers whenever the ad container dimensions change (for example, as a response to device orientation changes).sizeChange(width: number, height: number)
Parameter | Type | Description |
---|---|---|
| number | The width of the new view. |
| number | The height of the new view. |
stateChange
This event triggers whenever the state of the ad container changes.stateChange(state: string)
Parameter | Type | Description |
---|---|---|
| string | An indication of the ad container's state. This can be
|
exposureChange
This event triggers whenever there is a change in the exposure of the ad. Examples include when:- The ad displays.
- The ad closes.
- The application has a foreground or background.
- The ad container's exposure is altered (for example, the user opens the privacy settings).
exposureChange(exposedPercentage: number, visibleRectangle: Rectangle | null, occlusionRectangles: Rectangle[] | null)
Parameter | Type | Description |
---|---|---|
| number | The percentage of the ad that is visible on the screen (between
|
|
| The visible portion of the ad container, formatted as
|
|
| An array of rectangles describing any sections of the
|
audioVolumeChange
This event triggers whenever the device's volume changes.audioVolumeChange(volumePercentage: number): void
Parameter | Type | Description |
---|---|---|
| number | The percentage of maximum volume set on the device (between
|
viewableChange (deprecated)
This event triggers whenever there is a change in the viewability of the ad. Examples include when:
- The ad displays.
- The ad closes.
- The application has a foreground or background.
viewableChange(viewable: boolean): void
Parameter | Type | Description |
---|---|---|
| boolean |
|
Supported methods
Unity supports the following MRAID methods:- addEventListener
- close
- getCurrentAppOrientation
- getCurrentPosition
- getDefaultPosition
- getMaxSize
- getplacementtype
- getScreenSize
- getState
- getVersion
- isViewable
- open
- removeEventListener
- supports
- unload
addEventListener
Use this method to subscribe a specific handler function to a specific event. Multiple listeners can be subscribed to a specific event and a single listener can handle multiple events.addEventListener(event: string, listener: function): void
Parameter | Type | Description |
---|---|---|
| string | The name of the event to listen to. For a complete list, refer to the section on events. |
| object | The function to execute when the event triggers. |
close
Use this method to downgrade an ad container's state. Because Unity does not currently support bothexpand
resize
close():void
getCurrentAppOrientation
Use this method to query the device for the current orientation of the app.getCurrentAppOrientation(): { orientation: string, locked: boolean }
Return values
Value | Description |
---|---|
| A string describing the orientation of the app. This can be
|
| A boolean indicating whether the orientation is locked in its current position, where true indicates that the orientation is locked. |
getCurrentPosition
Use this method to return the current position and size of the ad view, measured in density-independent pixels.getCurrentPosition():JavaScriptObject
Return values
{x: number, y: number, width: number, height: number}
Value | Description |
---|---|
| The number of pixels offset from the left edge of the rectangle defined by
|
| The number of pixels offset from the top of the rectangle defined by
|
| The current width of the container in pixels. |
| The current height of the container in pixels. |
getDefaultPosition
Use this method to return the position and size of the default ad view, measured in density-independent pixels, regardless of what state the calling view is in.getDefaultPosition():JavaScriptObject
Return values
{x: number, y: number, width: number, height: number}
Value | Description |
---|---|
| The number of pixels offset from the left edge of the rectangle defined by
|
| The number of pixels offset from the top of the rectangle defined by
|
| The current width of the container in pixels. |
| The current height of the container in pixels. |
getMaxSize
Because Unity does not support theexpand
resize
getMaxSize():JavaScriptObject
Return values
{width: number, height: number}
Value | Description |
---|---|
| The current width of the container in pixels. |
| The current height of the container in pixels. |
getplacementtype
This method returns the type of placement, which might affect the creative's behavior.getplacementtype():string
Return values
Value | Description |
---|---|
| Indicates an inline ad placement. |
| Indicates an interstitial ad placement. |
getScreenSize
This method returns the actual pixel width and height, based on the current orientation in density-independent pixels, of the device on which the ad is running.getScreenSize():JavaScriptObject
Return values
{width: number, height: number}
Value | Description |
---|---|
| The maximum width of the screen size in pixels. |
| The maximum height of the screen size in pixels. |
getState
This method returns the current state of the ad container. Because Unity does not support theexpand
resize
"expanded"
"resized"
getState(): string
Return values
Value | Description |
---|---|
| Indicates that the ad container is in its default visible state. |
| Indicates that the ad container is hidden. |
| Indicates that the ad container is loading. |
getVersion
This method returns the version of the MRAID interface that Unity supports, allowing the bidder to confirm the basic features before display.getVersion():string
Return values
Value | Description |
---|---|
| Indicates that the MRAID interface is version 3.0. |
isViewable (deprecated)
This method returns whether the ad container is currently on or off the screen.
isViewable():boolean
Value | Description |
---|---|
| The container is on screen and viewable. |
| The container is off screen and not viewable. |
open
This method displays an embedded browser window in the application that loads an external URL. On device platforms that do not allow an embedded browser, this method invokes the native browser with the external URL.open(url:string):void
Parameters
Parameter | Type | Description |
---|---|---|
| string | The URL of the external web page. |
removeEventListener
Use this method to unsubscribe a specific handler method for a specific event. You should always remove event listeners when they're no longer in use to avoid errors.removeEventListner(event: string, listener: fuction: void)
Parameters
Parameter | Type | Description |
---|---|---|
| string | The name of the event the listener is subscribed to, which can include:
|
| function | The function to be removed. |
supports
This method returns whether the requesting device supports a specific feature.supports(feature:string):boolean
Parameters
Parameter | Type | Description |
---|---|---|
| string | The name of the feature to query:
|
Value | Description |
---|---|
| The device supports the queried feature. |
| The device does not support the queried feature. |
unload
This method informs the host that the ad should no longer be shown to the user. Use this method if the ad encounters errors or runtime exceptions that do not allow it to render correctly.unload(): void
Unsupported methods
Unity does not support the following methods:createCalendarEvent
expand
getExpandProperties
getLocation
getResizeProperties
initVpaid
playVideo
resize
setExpandProperties
setResizeProperties
storePicture
useCustomClose
Troubleshooting
If you get anUncaught TypeError
<MRAID>
Example
Uncaught TypeError: Cannot read property 'getVersion' of undefined.
getVersion
<MRAID>