Working with UnityObject
Desktop
UnityObject is a JavaScript script that simplifies Unity content embedding into HTML. It has functions to detect the Unity Web Player plugin, initiate Web Player installation and embed Unity content. Although it's possible to deploy UnityObject.js file on the web server alongside the HTML file it's best to load it directly from the Unity server at http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject.js. That way you will always reference the most up to date version of UnityObject. Please note that the UnityObject.js file hosted on the Unity server is minified to make it smaller and save traffic. If you want to explore the source code you can find the original file in the Data\Resources folder on Windows and the Contents/Resources folder on Mac OS X. UnityObject by defaults sends anonymous data to GoogleAnalytics which is used to help us identify installation type and conversion rate.
Functions
embedUnity
Embeds Unity content into HTML.
Parameters:
- id - HTML element (placeholder) to be replaced by Unity content.
- src - Path to the web player data file. Can be relative or absolute.
- width - Width of Unity content. Can be specified in pixel values (i.e. 600, "450") or in percentage values (i.e. "50%", "100%").
- height - Height of Unity content. Can be specified in pixel values (i.e. 600, "450") or in percentage values (i.e. "50%", "100%").
- params - Optional. Object containing list of parameters. See Customizing the Unity Web Player loading screen and Customizing the Unity Web Player's Behavior for possible values.
- attributes - Optional. Object containing list of attributes. These will be added to underlying <object> or <embed> tag depending on the browser.
- callback - Optional. Function that will be called once Web Player is loaded. Function must accept single argument that contains following properties:
- success - Boolean value indicating whether operation has succeeded.
- id - Identifier of the Web Player object that has been loaded (same as placeholder).
- ref - Web Player object that has been loaded.
Notes:
This function usually returns before the operation fully completes. Thus it is not safe to access the Web Player object immediately. A callback function can be provided to get notification on completion. Alternatively call getObjectById repeatedly until it doesn't return a null value.
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Unity Web Player | Example</title> <script type="text/javascript" src="http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject.js"></script> <script type="text/javascript"> <!-- if (typeof unityObject != "undefined") { unityObject.embedUnity("unityPlayer", "Example.unity3d", 600, 450, null, null, unityLoaded); } function unityLoaded(result) { if (result.success) { var unity = result.ref; var version = unity.GetUnityVersion("3.x.x"); alert("Unity Web Player loaded!\nId: " + result.id + "\nVersion: " + version); } else { alert("Please install Unity Web Player!"); } } --> </script> </head> <body> <!-- This will be replaced by Unity content. --> <div id="unityPlayer">Unity content can't be played. Make sure you are using compatible browser with JavaScript enabled.</div> </body> </html>
getObjectById
Retrieves the Web Player object.
Parameters:
- id - Identifier of the Web Player object.
- callback - Optional. Function that will be called once Web Player is retrieved. This function must accept a single parameter that contains the Web Player object.
Returns the Web Player object or null if the Web Player is not loaded yet.
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Unity Web Player | Example</title> <script type="text/javascript" src="http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject.js"></script> <script type="text/javascript"> <!-- if (typeof unityObject != "undefined") { unityObject.embedUnity("unityPlayer", "Example.unity3d", 600, 450, null, null, function(result) { if (result.success) { var versionButton = document.getElementById("versionButton"); versionButton.disabled = false; } }); } function versionButtonClick() { var unity = unityObject.getObjectById("unityPlayer"); var version = unity.GetUnityVersion("3.x.x"); alert(version); } --> </script> </head> <body> <!-- This will be replaced by Unity content. --> <div id="unityPlayer">Unity content can't be played. Make sure you are using compatible browser with JavaScript enabled.</div> <div> <input id="versionButton" type="button" value="Version" disabled="disabled" onclick="versionButtonClick();" /> </div> </body> </html>
enableFullInstall
Installs the full Web Player if not available. Normally only a small part of the Web Player is installed and the remaining files are automatically downloaded later. The default value is false.
Parameters:
- value - Boolean value that enables or disables the feature.
enableAutoInstall
Automatically starts Web Player installation if not available. Some platforms don't support this feature. Default value is false.
Parameters:
- value - Boolean value that enables or disables the feature.
enableJavaInstall
Enables Java based installation. Some platforms doesn't support this feature. Default value is true.
Parameters:
- value - Boolean value that enables or disables the feature.
enableClickOnceInstall
Enables ClickOnce based installation. Some platforms doesn't support this feature. Default value is true.
Parameters:
- value - Boolean value that enables or disables the feature.
enableGoogleAnalytics
Notifies Unity about Web Player installation. This doesn't do anything if the Web Player is already installed. Default value is true.
Parameters:
- value - Boolean value that enables or disables the feature.
addLoadEvent
Registers a function that will be called once the web page is loaded.
Parameters:
- event - Function that will be called once the web page is loaded. This function does not expect any parameters.
addDomLoadEvent
Registers a function that will be called once the web page's DOM is loaded.
Parameters:
- event - Function that will be called once web page's DOM is loaded. This function does not expect any parameters.