标题: unity3d-communication-with-browser-and-flash,unity3d通信(二) [打印本页] 作者: 会飞的鱼 时间: 2011-12-21 14:57 标题: unity3d-communication-with-browser-and-flash,unity3d通信(二) Now we have successfully called a function in Unity, it's time to send something back to the browser. This is very simple as well:
//Unity3D js code: Application.ExternalCall("javascriptFunction", "arameter1"); //Javascript code in browser: function javascriptFunction( param ){ alert( param ); }
Instead of alerting the function call, we can send this parameter to Flash and show the value in a Flash text field for example. This requires just a 1 line more code compared to calling a Unity3D function. First we need to define in Flash which function are allowed to be called by the browser.
ExternalInterface.addCallback("functionNameInBrowser", functionNameInFlash );
Now we can call functionNameInBrowser by the browser on a Flash object, which calls functionNameInFlash in Flash. To achieve this I use the following code in javascript:
//Cross browser function to get a movie on a page. function getFlashMovie(movieName) { var isIE = navigator.appName.indexOf("Microsoft") != -1; return (isIE) ? window[movieName] : document[movieName]; } //Call function in flash function javascriptFunction( param ){ getFlashMovie('flashMovieIdOnPage').functionNameInBrowser( param ); }
By***cuting the code from above, the function functionNameInFlash will be called like a regular function call. A sample function might look like the following code:
function functionNameInFlash( param:String ):void{ message.text = param; }
下面几个链接为各示例演示。
This is all you need to communicate between a browser, Unity3D and Flash. Once you know this trick you can for example:
■Build a Unity3D "browserchat"
■Make Unity3D objects clickable and load content in Flash
■Drag a rectangle in Flash and drag an cube in Unity3D
■Add, delete and drag objects from the Flash interface and show this in Unity3D
All examples are bundled in one download, which includes all the Unity3D, Flash, Flex, Actionscript and Javascript files.
I hope this will help anybody. Please let me know your feedback!
由 uke 发表作者: 菜刀吻电线 时间: 2012-1-23 23:29
愿你来年工资加“新”,少做“新”苦事,多领新票子;生活更新,买栋新房子,开辆新车子;心情舒“新”,听听新曲子,看看新片子;新年快乐心想事成。 作者: tc 时间: 2012-2-1 23:30
心中有爱,爱咋咋地 作者: 奇 时间: 2012-2-7 23:30
赞一个,哈哈 作者: tc 时间: 2012-3-20 23:30
灌水。。。 作者: C.R.CAN 时间: 2012-4-15 23:26
不错 非常经典 实用 作者: 奇 时间: 2012-4-25 23:21
我也来支持下 作者: tc 时间: 2012-5-9 23:26
精典,学习了! 作者: 菜刀吻电线 时间: 2012-5-20 23:19
不会吧,太恐怖了 作者: tc 时间: 2012-5-25 23:18
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!! 作者: tc 时间: 2012-6-5 23:26
其实楼主所说的这些,俺支很少用! 作者: C.R.CAN 时间: 2012-6-9 23:25
凡系斑竹滴话要听;凡系朋友滴帖要顶 作者: C.R.CAN 时间: 2012-6-10 23:22
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!! 作者: tc 时间: 2012-9-1 01:05
其实楼主所说的这些,俺支很少用! 作者: tc 时间: 2012-10-9 23:27
好铁多多发,感激分享 作者: 奇 时间: 2012-12-5 23:20
跑着去顶朋友滴铁 作者: 菜刀吻电线 时间: 2012-12-8 11:45
读铁系缘分,顶铁系友情 作者: 晃晃 时间: 2013-3-3 23:55
我就看看,我不说话