求一个unity3d简单例子教程与wcf通信的例子...

用户名:蓬莱仙羽
文章数:533
评论数:71
访问量:63399
注册日期:
阅读量:1297
阅读量:3317
阅读量:581033
阅读量:465359
51CTO推荐博文
谈谈今天的学习收获,发现了一个好东西,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&
650) this.width=650;" src="http://img.blog.csdn.net/12875?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGluZ3hpYW93ZWkyMDEz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
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!&);
Unity3D浏览器中与MyObject对象绑定好的函数MyFunction
function MyFunction(param : String) {
==================== 迂者 丁小未 CSDN博客专栏=================
MyBlog:& & & & & & &MyQQ:
Unity QQ群:858550 & & & & cocos2dx QQ群:
======================&相互学习,共同进步&===================
转载请注明出处:48089
欢迎关注我的微博:
下面是官方翻译:
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 thefunction 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来传递的一个字符串数据作为参数:
&scripttype=&text/javascript& language=&javascript&&
functionSaySomethingToUnity()
&&&document.getElementById(&UnityContent&).SendMessage(&MyObject&,&MyFunction&, &Hello from a web page!&);
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(),即执行一些简单的浏览器检测并返回一个使用的参考标记元素。下面是一个示例使用该功能:
&scripttype=&text/javascript& language=&javascript&&
functionSaySomethingToUnity()
&&&GetUnity().SendMessage(&MyObject&, &MyFunction&,&Hello from a web page!&);
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 thefunction.
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 );
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 thefunction
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 (), 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播放器的内容是从某主机(),如果不是这样,那么它将被重定向到另一个URL。这种技术可以用来防止深层链接到你的web播放器的内容:
Application.ExternalEval(
&&& &if(document.location.host!=
'') {document.location=''; }&
本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)网站已改版,请使用新地址访问:
UnityWcfExample 演示如何使用微软的IOC库—— 来配置WCF应用。WCF和IOC都是很常用的工具,很 CSharp C#编程 238万源代码下载-
&文件名称: UnityWcfExample
& & & & &&]
&&所属分类:
&&开发工具: C#
&&文件大小: 270 KB
&&上传时间:
&&下载次数: 31
&&提 供 者:
&详细说明:演示如何使用微软的IOC库——Unity来配置WCF应用。WCF和IOC都是很常用的工具,很多时候需要联合使用。-Demonstrated how to use MS IOC library Unity and WCF, both are very popular.
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&UnityWcfExample\Unity.Integration.Web\AspNet\IoC.cs&&...............\.....................\......\UnityHttpApplication.cs&&...............\.....................\......\UnityHttpModule.cs&&...............\.....................\bin\Debug\Microsoft.Practices.ObjectBuilder2.dll&&...............\.....................\...\.....\Microsoft.Practices.Unity.dll&&...............\.....................\...\.....\Unity.Integration.Web.dll&&...............\.....................\...\.....\Unity.Integration.Web.pdb&&...............\.....................\IContainerAccessor.cs&&...............\.....................\obj\Debug\Refactor\Unity.Integration.Web.dll&&...............\.....................\...\.....\ResolveAssemblyReference.cache&&...............\.....................\...\.....\Unity.Integration.Web.csproj.FileListAbsolute.txt&&...............\.....................\...\.....\Unity.Integration.Web.dll&&...............\.....................\...\.....\Unity.Integration.Web.pdb&&...............\.....................\Properties\AssemblyInfo.cs&&...............\.....................\Unity.Integration.Web.csproj&&...............\.....................\Wcf\UnityInstanceProvider.cs&&...............\.....................\...\UnityServiceBehavior.cs&&...............\.....................\...\UnityServiceHost.cs&&...............\.....................\...\UnityServiceHostFactory.cs&&...............\.....Example\bin\Microsoft.Practices.ObjectBuilder2.dll&&...............\............\...\Microsoft.Practices.Unity.dll&&...............\............\...\Unity.Integration.Web.dll&&...............\............\...\Unity.Integration.Web.pdb&&...............\............\...\UnityExample.dll&&...............\............\...\UnityExample.pdb&&...............\............\...\UnityWebService.dll&&...............\............\...\UnityWebService.pdb&&...............\............\Default.aspx&&...............\............\Default.aspx.cs&&...............\............\Default.aspx.designer.cs&&...............\............\Global.asax&&...............\............\Global.asax.cs&&...............\............\Loggers\ConsoleLogger.cs&&...............\............\.......\ILogger.cs&&...............\............\.......\NullLogger.cs&&...............\............\.......\ResponseLogger.cs&&...............\............\obj\Debug\ResolveAssemblyReference.cache&&...............\............\...\.....\TempPE\Service References.ServiceReference1.Reference.cs.dll&&...............\............\...\.....\UnityExample.csproj.FileListAbsolute.txt&&...............\............\...\.....\UnityExample.dll&&...............\............\...\.....\UnityExample.pdb&&...............\............\Properties\AssemblyInfo.cs&&...............\............\Service References\ServiceReference1\configuration.svcinfo&&...............\............\..................\.................\configuration91.svcinfo&&...............\............\..................\.................\Reference.cs&&...............\............\..................\.................\Reference.svcmap&&...............\............\..................\.................\UnityEnabledWebService.disco&&...............\............\..................\.................\UnityEnabledWebService.wsdl&&...............\............\..................\.................\UnityEnabledWebService.xsd&&...............\............\..................\.................\UnityEnabledWebService1.xsd&&...............\............\..................\.................\UnityEnabledWebService2.xsd&&...............\............\ServiceTester.aspx&&...............\............\ServiceTester.aspx.cs&&...............\............\ServiceTester.aspx.designer.cs&&...............\............\UnityExample.csproj&&...............\............\UnityExample.csproj.user&&...............\............\Web.config&&...............\UnityExample.sln&&...............\UnityExample.suo&&...............\.....WebService\bin\Microsoft.Practices.ObjectBuilder2.dll&&...............\...............\...\Microsoft.Practices.Unity.dll&&...............\...............\...\Unity.Integration.Web.dll&&...............\...............\...\Unity.Integration.Web.pdb&&...............\...............\...\UnityWebService.dll&&...............\...............\...\UnityWebService.pdb&&...............\...............\CompositeType.cs&&...............\...............\ISuffixProvider.cs&&...............\...............\IUnityEnabledWebService.cs&&...............\...............\MySuffixProvider.cs&&...............\...............\MyUnityServiceHostFactory.cs&&...............\...............\obj\Debug\Refactor\UnityWebService.dll&&...............\...............\...\.....\ResolveAssemblyReference.cache&&...............\...............\...\.....\TempPE\Service References.ServiceReference1.Reference.cs.dll&&...............\...............\...\.....\UnityWebService.csproj.FileListAbsolute.txt&&...............\...............\...\.....\UnityWebService.dll&&...............\...............\...\.....\UnityWebService.pdb&&...............\...............\Properties\AssemblyInfo.cs&&...............\...............\UnityEnabledWebService.svc&&...............\...............\UnityEnabledWebService.svc.cs&&...............\...............\UnityWebService.csproj&&...............\...............\UnityWebService.csproj.user&&...............\...............\Web.config&&...............\......Integration.Web\obj\Debug\Refactor&&...............\.....................\...\.....\TempPE&&...............\.....Example\obj\Debug\Refactor&&...............\............\...\.....\TempPE&&...............\.....WebService\obj\Debug\Refactor&&...............\...............\...\.....\TempPE&&...............\......Integration.Web\bin\Debug&&...............\.....................\obj\Debug&&...............\.....Example\obj\Debug&&...............\............\Properties\DataSources&&...............\............\Service References\ServiceReference1&&...............\.....WebService\obj\Debug&&...............\......Integration.Web\AspNet&&...............\.....................\bin&&...............\.....................\obj&&...............\.....................\Properties&&...............\.....................\Wcf&&...............\.....Example\App_Data
&相关搜索:
&输入关键字,在本站238万海量源码库中尽情搜索:
&[] - 网页上实现3D效果展示,实现unity3D与网页JS接口,使用ajax异步调用数据库
&[] - wcf 教程, Word文档,13个文档
&[] -  本书是wcf 领域内少有的经典著作,以WCF 的预备知识开篇,最后以REST 和Plain XML 服务的具体案例分析结束全书,全面而深入地讲解了WCF 技术的各个知识点,在广度和深度上都恰到好处。本书不仅系统地阐述了WCF 的重要理论知识,而且还提供了大量的实例和解决常见问题的最佳方案。它是WC
&[] - 计算机图形学动画源码,有3d效果,很有用的,关于三维变换,圆形,正方形。
&[] - unity 3D基础教程,Brizzle.pdf
主讲一游戏制作过程
&[] - MS,unity,c#,IOC容器,MS,Unity,c#,IOC容器
&[] - 网页上实现3D效果展示,实现unity3D与网页JS接口,使用ajax异步调用数据库
&[] - 一个比较不错的wcf的框架兼容性比较强的!本帖子已过去太久远了,不再提供回复功能。

参考资料

 

随机推荐