Working with UnityObject

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 at this location on Windows machines

C:\Program Files (x86)\Unity\Editor\Data\Resources

...and in the Contents/Resources folder on Mac OS X (ctrl-click on the application icon and select Show Package Contents to access the Contents folder). 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:

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:

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:

enableAutoInstall

Automatically starts Web Player installation if not available. Some platforms/browsers may not allow the plugin to be updated from the browser or have this feature disabled. Default value is false.

Parameters:

enableJavaInstall

Enables Java based installation. Some platforms may not support Java or may disable this feature. Default value is true.

Parameters:

enableClickOnceInstall

Enables ClickOnce based installation. This feature is only available on Windows and may be disabled or unsupported in a given browser configuration. Default value is true.

Parameters:

enableGoogleAnalytics

Notifies Unity about Web Player installation. This doesn't do anything if the Web Player is already installed. Default value is true.

Parameters:

addLoadEvent

Registers a function that will be called once the web page is loaded.

Parameters:

addDomLoadEvent

Registers a function that will be called once the web page's DOM is loaded.

Parameters:

Page last updated: 2012-11-13