[Unity3d]unity与html通信

简介:

谈谈今天的学习收获,发现了一个好东西,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()方法,如下所示:

<Script type = "text/javascript" language = "javascript"> 	function SayHello(arg) 	{ 		alert(arg); 	} </Script>



效果:


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实现关键代码如下:

<script type = "text/javascript" language = "javascript"> 	function SaySomethingToUnity() 	{ 		//获得Unity浏览器对象的ID 		var unity = unityObject.getObjectById("UnityContent"); 		//调用SendMessage函数访问Unity3D浏览器对象中的脚本函数 		unity.SendMessage("MyObject","MyFunction","Hello from a web page!"); 	}   </script>

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

欢迎关注我的微博: http://weibo.com/u/2590571922


下面是官方翻译:

Unity Manual>AdvancedWeb 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.

UnityWeb 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来传递的一个字符串数据作为参数:

<scripttype="text/javascript" language="javascript">

<!--

functionSaySomethingToUnity()

{

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

}

-->

</script>

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网络播放器内容的内部里你需要一个脚本附加到名为MyObjectGameObject上,该脚本需要实现名为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播放器的内容时包括了一个objectembed标签,以便在所有浏览器中的内容正确加载。为了使基于浏览器的JavaScript来区分这两种标记的元素,他们每次使用一个唯一的ID值,UnityObjectobject标签和UnityEmbedembed标签。正因为如此,默认的HTML文件还包括一个JavaScript函数,GetUnity(),即执行一些简单的浏览器检测并返回一个使用的参考标记元素。下面是一个示例使用该功能:

<scripttype="text/javascript" language="javascript">

<!--

functionSaySomethingToUnity()

{

   GetUnity().SendMessage("MyObject", "MyFunction","Hello from a web page!");

}

-->

</script>

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()函数,例如:

<scripttype="text/javascript" language="javascript">

<!--

function SayHello(arg )

{

    // show the message

    alert( arg );

}

-->

</script>

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'; }"

);

 



















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366205,如需转载请自行联系原作者

相关文章
|
5月前
electron解决创建新窗口html文件不能引入ipcRenderer通信
electron解决创建新窗口html文件不能引入ipcRenderer通信
|
4月前
|
图形学
【Unity3D开发小游戏】Unity3D零基础一步一步教你制作跑酷类游戏
【Unity3D开发小游戏】Unity3D零基础一步一步教你制作跑酷类游戏
|
4月前
|
设计模式 存储 前端开发
【Unity3D日常开发】Unity3d中使用MVC框架
【Unity3D日常开发】Unity3d中使用MVC框架
|
7月前
|
数据采集 物联网 Linux
Unity3D下实现Linux平台RTMP推流(以采集Unity窗体和声音为例)
随着物联网等行业的崛起,越来越多的传统行业如虚拟仿真、航天工业、工业仿真、城市规划等,对Linux下的生态构建,有了更大的期望,Linux平台下,可选的直播推拉流解决方案相对Windows和移动端,非常少,基于Unity的Linux推送方案,更是几无参考。本文以Unity3d环境下Linux平台推送Unity窗体和Unity采集的音频,然后编码推送到RTMP服务器为例,大概说下实现过程。
134 0
|
存储 C# 图形学
从零开始做一款Unity3D游戏<一>——亲自上手使用Unity(二)
从零开始做一款Unity3D游戏<一>——亲自上手使用Unity
从零开始做一款Unity3D游戏<一>——亲自上手使用Unity(二)
|
编解码 JavaScript C#
从零开始做一款Unity3D游戏<一>——亲自上手使用Unity(一)
从零开始做一款Unity3D游戏<一>——亲自上手使用Unity
从零开始做一款Unity3D游戏<一>——亲自上手使用Unity(一)
|
小程序 定位技术 API
Unity3D-开发工具选择与Unity安装
本笔记基于windows平台,使用Unity软件ARFoundation开发工具开发安卓系统 开发工具版本: Windows11 Unity Hub 2.5.5 Unity2020.3.25f1c1(LTS)
703 0
Unity3D-开发工具选择与Unity安装
|
XML vr&ar Android开发
【Android】Android与Unity安卓AR通信(3)
unity导出作为安卓项目中的库来使用 1. 首先打开一个安卓项目 2. 准备好unity中Export出的安卓包
231 0
|
Java C# Android开发
Unity与安卓通信(2)
Unity与安卓通信(2)
84 0
|
Java API 图形学
安卓与Unity通信(1)
通过导入Unity的classes.jar,继承unity的UnityPlayerActivity实现unity与安卓的交互
233 0