Unity Web Player and browser communication Unity网络播放器和浏览器通信

Date:2011-10-29
Desktop

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网络播放器内容可以和浏览器通信,反之亦然。主要有2个通信方向:

  • The web page calls functions inside the Unity web player content.
    网页在Unity网络播放器内容中调用函数。
  • The Unity web player content calls functions in the web page.
    Unity网络播放器内容在网页调用函数。

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

下面详细描述每个通信方向。

Calling Unity web player content functions from the web page
从网页调用Unity网络播放器内容函数。

The Unity Web Player object has 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网络播放器对象有一个函数SendMessage(),可以从网页在Unity网络播放器内容中调用这个函数。这个函数非常相似Unity脚本API中的GameObject.SendMessage函数。当从网页调用时,传递一个对象名,一个函数名和一个参数,并且SendMessage()将在给定的游戏物体调用给定的函数。

In order to call the Unity Web Player's SendMessage() function you must first get a reference to the Unity web player object. You can use unityObject.getObjectById() function to obtain a reference to the object. Here is an example JavaScript function that would execute the SendMessage() function on the Unity web player content with placeholder 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网络播放器的SendMessage()函数,你必须首先获得一个一个引用到Unity网络播放器对象。可以使用unityObject.getObjectById()函数以获取一个引用到对象。下面是一个JavaScript函数例子,在带有占位符ID,UnityContent的值的Unity网络播放器内容,将执行SendMessage()函数。反过来SendMessage()将在名为MyObject的游戏物体调用MyFunction()函数,传递字一段符串数据作为参数。

<script type="text/javascript" language="javascript">
<!--
function SaySomethingToUnity()
{
	var unity = unityObject.getObjectById("UnityContent");
	unity.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的游戏物体,并该脚本需要执行名为MyFunction的函数:

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

Note: keep in mind that if the function doesn't have any arguments, then an empty string ("") should be passed as an argument.

注意:如果函数没有任何参数,将传递一个空字符串作为参数。

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()时,必须传递入一个字符串、整数或浮点数参数,这个参数是必须的。如果你并不需要它那么仅传递0或其他默认的值,Unity将忽略它。此外,通过名称指定的游戏物体可以是给定的路径名形式。例如,/MyObject/SomeChild,SomeChild必须是MyObject的子物体,并且MyObject必须在根目录,因为它的名称前面有字符'/'。

Calling web page functions from Unity web player content
从Unity网络播放器内容调用网页函数

In order to call a web page function from within your Unity web player content you must use the Application.ExternalCall() function. Using that function you can call any JavaScript function defined in the web page, passing any number of parameters to it. Here is an example Unity script that uses the Application.ExternalCall() function to call a function named SayHello() found within the web page, passing a piece of string data as an argument:

为了从Unity网络播放器内容中调用网页函数,必须使用Application.ExternalCall()函数。使用该函数可以调用任意网页中定义的JavaScript函数,传递任意参数给它。这儿是Unity脚本的一个例子,使用Application.ExternalCall()函数来调用网页中名为SayHello()的函数。

Application.ExternalCall( "SayHello", "The game says hello!" );

The web page would need to define the SayHello() function, for example:

网页中必须定义SayHello()函数,例如:

<script type="text/javascript" language="javascript">
<!--
function SayHello( arg )
{
    // show the message
    alert( arg );
}
-->
</script>

Executing arbitrary browser code from Unity web player content
从Unity网络播放器内容执行任意浏览器的代码

You don't even have to define functions in the embedding web page, instead you can use the Application.ExternalEval() function to execute arbitrary browser code from the web player content.

你甚至无需在嵌入的网页定义函数,反而可以使用Application.ExternalEval()函数从网络播放器内容来执行任意浏览器代码。

The following example checks that the page embedding the web player content is fetched from a certain host (unity3d.com), if that's not the case then it will redirect to another URL. This technique can be used to prevent deep linking to your web player content:

下面的例子检查,页面嵌入的网络播放器内容是否取自某个主机(unity3d.com),如果不是,将重定向另一个URL。这种技术可以用来防止深层链接到您的网络播放器的内容:

深层链接是指绕开被链网页的主页而使用户直接进入其某一个分页的链接方式。】

Application.ExternalEval(
    "if(document.location.host != 'unity3d.com') { document.location='http://unity3d.com'; }"
);

页面最后更新: 2010-07-28

分类:Manual|评论: 0|浏览: 36 翻译: U_鹰
评论
暂无评论
发表评论
用户名:

评论审核后才会显示!