Deliver interactive playable ads through Unity Exchange.
Read time 6 minutesLast updated 2 months 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 (
<script src="mraid.js"></script>
) as early as possible in the ad's code, before referencing any MRAID functions.
function to detect when the MRAID state changes from "loading" to "ready".
Use the
getState
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.
functionshowMyAd(){// Ad content}if(mraid.getState()=== ‘loading'){ mraid.addEventListener(‘ready', showMyAd);}else{showMyAd();}
Access the
MRAID_ENV
object to verify any additional information you need before initialization. This variable is attached to the window object and contains the following attributes:
Attribute
Type
Example
Description
version
string
"3.0"
The version of the implemented MRAID interface.
sdk
string
"The Unity Ads SDK"
The name of the SDK running the webview.
sdkVersion
string
"4.0"
The version of the SDK running the webview.
appId
string
"com.unity3d.ads.example"
The package name or application ID of the app running the ad.
limitAdTracking
boolean
true
true
indicates that Limit Ad Tracking is active, and
false
indicates that it is not.
coppa
boolean
true
true
indicates that the app is directed at children, and
false
indicates that it is not.
Note
Unity does not support the
ifa
attribute.
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 the
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.
The number of pixels offset from the left edge of the rectangle defined by
getMaxSize()
.
y
The number of pixels offset from the top of the rectangle defined by
getMaxSize()
.
width
The current width of the container in pixels.
height
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.
The number of pixels offset from the left edge of the rectangle defined by
getMaxSize()
.
y
The number of pixels offset from the top of the rectangle defined by
getMaxSize()
.
width
The current width of the container in pixels.
height
The current height of the container in pixels.
getMaxSize
Because Unity does not support the
expand
and
resize
methods, this method always returns the maximum size (in density-independent pixel width and height) of the ad's container. The return values for full-screen interstitial ads will always be the full screen dimensions.
getMaxSize():JavaScriptObject
Return values
{width: number, height: number}
Value
Description
width
The current width of the container in pixels.
height
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
"inline"
Indicates an inline ad placement.
"interstitial"
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
width
The maximum width of the screen size in pixels.
height
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 the
expand
and
resize
methods, the ad state will never return
"expanded"
or
"resized"
values.
getState(): string
Return values
Value
Description
"default"
Indicates that the ad container is in its default visible state.
"hidden"
Indicates that the ad container is hidden.
"loading"
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
3.0
Indicates that the MRAID interface is version 3.0.
isViewable (deprecated)
Important
This event has been deprecated. However, Unity still supports it to maintain backward compatibility.
This method returns whether the ad container is currently on or off the screen.
isViewable():boolean
Return values
Value
Description
true
The container is on screen and viewable.
false
The container is off screen and not viewable.
Note
This method triggers the
viewableChange
event.
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
url
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.
The name of the event the listener is subscribed to, which can include:
"error"
"ready"
"sizeChange"
"stateChange"
"viewableChange"
listener
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
event
string
The name of the feature to query:
"calendar"
"inlineVideo"
"sms"
"storePicture"
"tel"
Return values
Value
Description
true
The device supports the queried feature.
false
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 an
Uncaught TypeError
such as the following example, it might indicate that the MRAID interface is not available yet, or that the ad creative failed to include the
<MRAID>
script tag so the interface was never attached.
Example
Uncaught TypeError: Cannot read property 'getVersion' of undefined.
This error would occur if you try to call the
getVersion
method without including the
<MRAID>
script tag. To correct this, add the script tag in earlier in the code, before referencing any MRAID functions.