- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
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 发表 |
|