- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
老外写的一篇关于unity3d,flash,浏览器三者间互相通信的例子,非常不错,强烈推荐,朋友们必然会用到一个知识点。
I've been using Unity3D for a while now and I would like to share some of my discoveries starting by this post. In case you don't know unity3D yet, you should check out their website: unity3d.com and make sure you'll see the tropical paradise and shadows demo. Unity3D is a very promising 3D engine with the possibility to publish for the web by using a browser plugin (just around 3MB) with extreme good results. Personally I'm not a big fan of using browser plugins which aren't as adopted as Flash is. Flash projects like Papervision3D/Away3D/Alternativa/etc are very impressive and I love to use them. But in case you want to go more advanced, this is all to limited and you have to look at a browser plugin. Unity3D is then the best option.
Okay, enough introduction. As a web oriented developer I want Unity3D elements on my page to completely integrate with other elements in an easy way. Basically, I want to be able to have cross communication between Unity3D, Flash and javascript on a page. Building GUI's using Unity3D is very limited when you compare it with Flash. So why not using best of both worlds together on a page?
While starting with this I found a chat demo build by FlashBangStudios which does cross communication between Unity3D, Flash and a browser. Unfortunately it isn't open sourced, so we can't see everything about what's going on under the hood. So I've build a Unity3D chat myself based on communication within the browser. (Source files are included in the download at the end of this article)
To achieve this I used something similar like SWFObject, called UnityObject, to embed unity files in a clean way on my page. The coded posted there seems to a bit out dated and showed some problems in Internet Explorer 7. So I've tweaked their version a little bit and it now should be working perfectly (Tested with Firefox 3.0.3, Internet Explorer 7, Safari 3.1.2 and Chrome).
With unityobject, embedding is very easy:
var uniObj = new UnityObject("UnityObject.unity3d", "Unity3D", "640", "480"); uniObj.setAttribute("altHTML", "Alternative content when player is not installed"); uniObj.write();
To call a function on a gameObject in unity we can use the following code:
uniObj.msg( "GameObjectName", "FunctionInGameObject", "arameterToSendToUnity" );
All this does is calling a special function on the unityObject within your page called SendMessage(...); and delegates your call to the object and function which you described in the call. In case from above we will trigger FunctionInGameObject in GameObjectName. We can only pass 1 parameter to the triggered Unity function. The most advanced parameter you can use in this case is an array. Using an object isn't supported. So when you want to pass an object as a parameter you need to serialize and deserialize your object. This is however not supported by default in Unity. When you want to do this, you could use JSON for example, which is offcourse supported in javascript, but there are good C# libraries as well. Maybe I'll dedicate a future blog post to this. |
|