Unity3D高级手册0904:Unity WEB播放器和浏览器通信

2014-09-20 20:27:31|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:游戏, 虚拟现实, Unity3D|来源:唯设编程网

Unity Manual > Advanced > Web Player Deployment > Unity Web Player and browser communication

Unity手册->高级->web播放器部署-> Unity WEB播放器和浏览器通信

The HTML page that contains Unity Web Player content can communicate with that content and vice versa. Basically there are two communication directions:

HTML页面,其中包含Unity Web播放的内容可以通信的内容,反之亦然。基本上有两种通信方向:

*       The web page calls functions inside the Unity web player content.  

*       该网页内调用Unity web播放器内容内部的功能。

*       The Unity web player content calls functions in the web page.

*       Unity web播放器的内容调用功能在在web页中。

Each of these communication directions is described in more detail below.

这些通信方向的每一个详细描述如下。

Calling Unity web player content functions from the web page

web页面调用Unity web播放器内容

The Unity Web Player plugin and ActiveX Controls both have a function, SendMessage(), that can be called from a web page in order to call functions within Unity web player content. This function is very similar to the GameObject.SendMessage function in the Unity scripting API. When called from a web page you pass an object name, a function name and a single argument, and SendMessage() will call the given function in the given game object.

在Unity 的Web Player插件和ActiveX控件都有一个函数,SendMessage(),可以从网页上一个web页面被调用,为了调用Unity web播放器内容内部功能。这个功能是非常类似于GameObject.SendMessage函数在Unity 脚本API里。当从所谓的网页上你传递一个对象的名称,一个函数名和一个简单参数,和SendMessage()将调用给定的函数在在给定对象的游戏里。

In order to call the Unity Web Player's SendMessage() function you must first get a reference to the Unity web player content object being displayed. You can use JavaScript's document object and its getElementById() function to obtain a reference to the content. Here is an example JavaScript function that would execute the SendMessage() function on the Unity web player content with an object/embed tag id value of UnityContent; in turn SendMessage() will then call the function MyFunction() on the game object named MyObject, passing a piece of string data as an argument:

为了调用Unity Web播放器的SendMessage()函数必须先得到网站的Unity 播放器内容对象的一个引用被显示出来。你可以使用JavaScript的文档对象和getElementById()函数来获取对内容的引用。下面是一个JavaScript示例函数将利用object/ embed标签的UnityContent id值在Unity web播放器内容上执行SendMessage()函数,然后反过来SendMessage()将会调用函数调用MyFunction()在游戏对象名称上的MyObject来传递的一个字符串数据作为参数:

<script type="text/javascript" language="javascript">

<!--

function SaySomethingToUnity()

{
    document.getElementById("UnityContent").SendMessage("MyObject", "MyFunction", "Hello from a web page!");

}

-->
</script>

Inside of the Unity web player content you need to have a script attached to the GameObject named MyObject, and that script needs to implement a function named MyFunction:

在Unity 网络播放器内容的内部里你需要一个脚本附加到名为MyObject的GameObject上,该脚本需要实现名为myFunction函数:

function MyFunction(param : String)
{
    Debug.Log(param);
}

A single string, integer or float argument must be passed when using SendMessage(), the parameter is required on the calling side. If you don't need it then just pass a zero or other default value and ignore it on the Unity side. Additionally, the game object specified by the name can be given in the form of a path name. For example, /MyObject/SomeChild where SomeChild must be a child of MyObject and MyObject must be at the root level due to the '/' in front of its name.

一个单一的字符串,整数或浮点数必须通过使用SendMessage()传递,参数是需要在非正式的调用。如果你不需要它然后只通过一个零或其他默认值并忽略它在Unity方面。此外,游戏对象指定通过名称可以得到在一个路径名。例如,/ MyObject/ SomeChild在那儿SomeChild必须是MyObject的子和MyObject必须在根级别由于'/在其名称的前面。

<12>
发表评论0条 】
网友评论(共?条评论)..
Unity3D高级手册0904:Unity WEB播放器和浏览器通信