标题: unity3d发布的flash与其它flash间的通信 [打印本页] 作者: 奔跑的小兔 时间: 2012-2-8 10:16 标题: unity3d发布的flash与其它flash间的通信 By popular demand, here's an example of how one can use UnityShared.swc to create a custom preloader and communicate between Unity and Flash.
The ActionScript source of this project can be used with ActionScript tools like FlashBuilder, FDT and FlashDevelop. The project needs to be setup so that the provided UnityShared.swc is used as a library.
In UnityShared an AS3 class named UnityContentLoader is provided, which is the base for loading and communicating with UnityContent.
When publishing to Flash Application.ExternalCall is routed to what is set as the UnityContentHost, when cons***cting the UnityContentLoader. These messages (unfortunately there is no way to send arguments at this point in time using Application.ExternalCall), then are directly called on IUnityContentHost.
The unityInitStart and unityInitComplete have to be implemented by the class set as IUnityContentHost and signify when the UnityContent is done loading and starting to initialize, and when it's done initializing, and ready for communication from AS3.
To communicate from AS3 to Unity, one calls sendMessage on the unityContent property of unityContentLoader. The sendMessage functionality is similar to the sendMessage functionality of the Unity webplayer.
sendMessage accepts arguments in the following order; first a string path to the gameobject, then the method to be called on that gameobject, and then an anonymous object to contain the arguments. This way, one can marshall direct references to objects between AS3 and Unity, cache them and call them directly.
This api is by no means in it's final state and are subject to change in later drops(we are thinking about a full api inside unity towards AS3, similar to custom platform api's for other platforms), but it should help implementing things like networking.