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

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

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

The default html file generated when you publish web player content includes both an object and embed tag in order to have the content load properly in all browsers. In order to allow browser-based JavaScript to distinguish between the two tag elements they each use a unique id value, UnityObject for the object tag and UnityEmbed for the embed tag. Because of this, the default html file also includes a JavaScript function, GetUnity(), that performs some simple browser detection and returns a reference to the tag element in use. Here is an example using that function:

默认的HTML文件生成当你发布web播放器的内容时包括了一个object和embed标签,以便在所有浏览器中的内容正确加载。为了使基于浏览器的JavaScript来区分这两种标记的元素,他们每次使用一个唯一的ID值,UnityObject为object标签和UnityEmbed为embed标签。正因为如此,默认的HTML文件还包括一个JavaScript函数,GetUnity(),即执行一些简单的浏览器检测并返回一个使用的参考标记元素。下面是一个示例使用该功能:

<script type="text/javascript" language="javascript">
<!--
function SaySomethingToUnity()
{
    GetUnity().SendMessage("MyObject", "MyFunction", "Hello from a web page!");

}
-->
</script>

Calling web page functions from Unity web player content

从Unity播放器内容调用web页函数

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 web播放器内容调用一个WEB页函数,你必须使用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 web 播放器内存执行任意浏览器的代码。

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:

下面的例子检查该网页嵌入web播放器的内容是从某主机(unity3d.com),如果不是这样,那么它将被重定向到另一个URL。这种技术可以用来防止深层链接到你的web播放器的内容:

Application.ExternalEval(
    "if(document.location.host != 'unity3d.com') { document.location='http://unity3d.com'; }"
);
<12>
发表评论0条 】
网友评论(共?条评论)..
Unity3D高级手册0904:Unity WEB播放器和浏览器通信