Working with UnityObject 工作于UnityObject

优质
小牛编辑
123浏览
2023-12-01
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.

UnityObject是JavaScript脚本,简化了Unity的内容嵌入到HTML中。它具有检测Unity网络播放器插件的功能,初始化网络播放器的安装和嵌入Unity的内容。尽管也可以部署UnityObject.js文件和HTML文件一起在web服务器,最好从Unity服务器http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject.js加载文件。这样总能引用UnityObject的最新版本。请注意在Unity服务器上托管的UnityObject.js文件是压缩版,使其很小,以节省流量。如果你想浏览源代码,在Windows可以在安装目录Data\Resources文件夹找到,在Mac OS X在安装目录Contents/Resources文件夹找到。默认UnityObject会发生匿名数据到GoogleAnalytics,这是用来帮助我们确定安装类型和转换率。

Functions 功能

embedUnity

Embeds Unity content into HTML.

嵌入Unity内容到HTML。

Parameters 参数:

  • id - HTML element (placeholder) to be replaced by Unity content.
    HTML元素(占位符),用来替换Unity内容。
  • 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%").
    Unity内容的宽度。可以指定像素值(如600,450)或百分比值(如,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%").
    Unity内容的高度。可以指定像素值(如600,450)或百分比值(如,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.
    可选参数。包含参数列表的对象。参见自定义Unity网络播放器加载的屏幕自定义Unity网络播放器的行为可能的值。
  • attributes - Optional. Object containing list of attributes. These will be added to underlying <object> or <embed> tag depending on the browser.
    可选属性。包含属性列表的对象。这些将被添加到<object> or <embed>标签,取决于浏览器。
  • 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.

这个功能通常在返回之前操作全部完成。因此它并不是安全地立即访问网络播放器对象。一个回调函数可提供在完成时获取通知,另外屡次调用getObjectById,直到它不返回一个空值。

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.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.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.

如果不可用,安装完整网络播放器。标准仅安装网络播放器一小部分,其余的文件会自动下载。默认值为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.

如果不可用,自动开始网络播放器安装。一些平台不支持这个功能。默认值为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.

启用基于Java安装。一些平台不支持这个功能。默认值为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.

启用基于ClickOnce安装。一些平台不支持这个功能。默认值为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.

通知Unity有关网络播放器安装。如果网络播放器已经安装,这个什么也不做。默认值为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.

注册一个函数,将在网页DOM加载时调用一次。

Parameters 参数:

  • event - Function that will be called once web page's DOM is loaded. This function does not expect any parameters.
    事件 - 在网页DOM加载时,函数将被调用一次。该函数没有任何参数。

页面最后更新:2010-08-06