查看: 4458|回复: 5
打印 上一主题 下一主题

unity3d-communication-with-browser-and-flash,unity3d通信(一)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-12-21 14:56:09 |只看该作者 |倒序浏览
老外写的一篇关于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.
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-5-23 23:21:44 |只看该作者
我就看看,我不说话
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-7-5 23:21:37 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

643

主题

1

听众

9937

积分

高级设计师

Rank: 6Rank: 6

纳金币
9935
精华
1

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-7-6 15:13:02 |只看该作者
好好好好好好好好好好好好好好好
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-9-27 23:23:59 |只看该作者
不错 非常经典  实用
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

6#
发表于 2012-9-28 23:20:24 |只看该作者
“再次路过……”我造一个-----特别路过
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-14 12:46 , Processed in 0.104963 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部