谈谈今天的学习收获,发现了一个好东西,unity与html能够相互通信,意味着我之前学的web开发还能在unity中用得上,哈哈,太happy了!下面简单谈谈通过Unity3D调用HTML网页的脚本函数,以及通过HTML网页调用Unity3D中的脚本函数。
1.1Unity3D浏览器调用HTML网页中的函数
Unity3D浏览器通过执行Application.ExternalCall()来调用任何在HTML网页里定义JavaScript函数,比如下面一句调用了HTML网页里SayHello()函数,并传递了一句话作为参数。
Application.ExternalCall("SayHello","helloworld");
在HTML网页里需定义SayHello()方法,如下所示:
效果:
1.2HTML网页调用Unity3D浏览器中的脚本函数
Unity3D浏览器的插件或ActiveX控件都有一个SendMessage()的函数,HTML网页通过这个函数与Unity3D进行通信,通过该函数可以传递对象名、函数名以及简单参数,然后SendMessage()就会调用Unity3D与GameObject上绑定的函数。在调用SendMessage()函数之前,必须先得到Unity WebPlayer的引用。这里可以使用JavaScript对象Document的getElementById()函数来获得该引用。
下面是一个例子,它会执行SendMessage()函数,嵌入在Object或embed标签下的Unity web player的id是UnityContent,SendMessage函数会从一个名为MyObject对象上的MyFunction()函数,并传递一句话作为参数。在Unity内容里,需要放置一个名为MyObject的对象,并在该对象上附加实现了名称为MyFunction函数的脚本文件。HTML实现关键代码如下:
Unity3D浏览器中与MyObject对象绑定好的函数MyFunction
function MyFunction(param : String) { 语句; }
==================== 迂者 丁小未 CSDN博客专栏=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ群:858550 cocos2dx QQ群:280818155
====================== 相互学习,共同进步 ===================
转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/17048089
下面是官方翻译:
Unity Manual>Advanced> Web PlayerDeployment > Unity Web Playerand browser communication
Unity手册->高级->web播放器部署-> Unity WEB播放器和浏览器通信
Unity Web Player and browser communication Unity WEB播放器和浏览器通信
The HTML page that contains Unity Web Playercontent can communicate with that content and vice versa. Basically there aretwo communication directions:
HTML页面,其中包含Unity Web播放的内容可以通信的内容,反之亦然。基本上有两种通信方向:
The web page calls functions inside theUnity web player content.
该网页内调用Unityweb播放器内容内部的功能。
The Unity web player content callsfunctions in the web page.
Unity web播放器的内容调用功能在在web页中。
Eachof these communication directions is described in more detail below.
这些通信方向的每一个详细描述如下。
Calling Unity web player content functionsfrom the web page
从web页面调用Unity web播放器内容
The Unity Web Player plugin and ActiveX Controls bothhave a function,SendMessage(), that can be called from a web page inorder to call functions within Unity web player content. This function is verysimilar to theGameObject.SendMessagefunction in the Unity scripting API. When called from a web page you pass anobject name, a function name and a single argument, andSendMessage()will call the given function in the given game object.
在Unity的Web Player插件和ActiveX控件都有一个函数,SendMessage(),可以从网页上一个web页面被调用,为了调用Unity web播放器内容内部功能。这个功能是非常类似于GameObject.SendMessage函数在Unity脚本API里。当从所谓的网页上你传递一个对象的名称,一个函数名和一个简单参数,和SendMessage()将调用给定的函数在在给定对象的游戏里。
Inorder to call the Unity Web Player'sSendMessage() function you mustfirst get a reference to the Unity web player content object being displayed.You can use JavaScript'sdocument object and its getElementById()function to obtain a reference to the content. Here is an example JavaScriptfunction that would execute theSendMessage() function on the Unity webplayer content with an object/embed tag id value ofUnityContent; inturn SendMessage() will then call the functionMyFunction() onthe game object named MyObject, passing a piece of string data as anargument:
为了调用Unity Web播放器的SendMessage()函数必须先得到网站的Unity播放器内容对象的一个引用被显示出来。你可以使用JavaScript的文档对象和getElementById()函数来获取对内容的引用。下面是一个JavaScript示例函数将利用object/ embed标签的UnityContent id值在Unityweb播放器内容上执行SendMessage()函数,然后反过来SendMessage()将会调用函数调用MyFunction()在游戏对象名称上的MyObject来传递的一个字符串数据作为参数:
Insideof the Unity web player content you need to have a script attached to theGameObjectnamed MyObject, and that script needs to implement a function namedMyFunction:
在Unity网络播放器内容的内部里你需要一个脚本附加到名为MyObject的GameObject上,该脚本需要实现名为myFunction函数:
function MyFunction(param: String)
{
Debug.Log(param);
}
Asingle string, integer or float argument must be passed when usingSendMessage(),the parameter is required on the calling side. If you don't need it then justpass 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 ofa path name. For example, /MyObject/SomeChild where SomeChildmust be a child ofMyObject and MyObject must be at the rootlevel due to the '/' in front of its name.
一个单一的字符串,整数或浮点数必须通过使用SendMessage()传递,参数是需要在非正式的调用。如果你不需要它然后只通过一个零或其他默认值并忽略它在Unity方面。此外,游戏对象指定通过名称可以得到在一个路径名。例如,/ MyObject/ SomeChild在那儿SomeChild必须是MyObject的子和MyObject必须在根级别由于'/在其名称的前面。
Thedefault html file generated when you publish web player content includes bothan object and embed tag in order to have the content load properly in allbrowsers. In order to allow browser-based JavaScript to distinguish between thetwo tag elements they each use a unique id value, UnityObject for theobject tag andUnityEmbed for the embed tag. Because of this, thedefault html file also includes a JavaScript function,GetUnity(), thatperforms some simple browser detection and returns a reference to the tagelement in use. Here is an example using that function:
默认的HTML文件生成当你发布web播放器的内容时包括了一个object和embed标签,以便在所有浏览器中的内容正确加载。为了使基于浏览器的JavaScript来区分这两种标记的元素,他们每次使用一个唯一的ID值,UnityObject为object标签和UnityEmbed为embed标签。正因为如此,默认的HTML文件还包括一个JavaScript函数,GetUnity(),即执行一些简单的浏览器检测并返回一个使用的参考标记元素。下面是一个示例使用该功能:
Calling web page functions from Unity webplayer content
从Unity播放器内容调用web页函数
In order to call a web page function from within yourUnity web player content you must use theApplication.ExternalCall()function. Using that function you can call any JavaScript function defined inthe web page, passing any number of parameters to it. Here is an example Unityscript that uses theApplication.ExternalCall() function to call afunction named SayHello() found within the web page, passing a piece ofstring data as an argument:
为了从你内部Unity web播放器内容调用一个WEB页函数,你必须使用Application.ExternalCall()函数。使用该功能,你可以调用任何JavaScript函数中定义的网页,传递任意数量的参数给它。这里有一个例子Unity脚本使用Application.ExternalCall()函数来调用一个函数名为SayHello()发现的网页,传递一个字符串数据作为参数:
Application.ExternalCall("SayHello", "The game says hello!" );
Theweb page would need to define the SayHello() function, for example:
该网页将需要定义sayHello()函数,例如:
Executing arbitrary browser code from Unityweb player content
从Unity web播放器内存执行任意浏览器的代码。
You don't even have to define functions in the embeddingweb page, instead you can use theApplication.ExternalEval()function to execute arbitrary browser code from the web player content.
你甚至不必在嵌入网页定义功能,替代的是你可以使用Application.ExternalEval()函数来执行任意浏览器的代码从网页播放器的内容。
Thefollowing example checks that the page embedding the web player content isfetched from a certain host (unity3d.com), if that's not the case then it willredirect to another URL. This technique can be used to prevent deep linking toyour web player content:
下面的例子检查该网页嵌入web播放器的内容是从某主机(unity3d.com),如果不是这样,那么它将被重定向到另一个URL。这种技术可以用来防止深层链接到你的web播放器的内容:
Application.ExternalEval(
"if(document.location.host!= 'unity3d.com') {document.location='http://unity3d.com'; }"
);