[Edit by Cat: Please note that the below example does not work in Unity 3.5.2. The updated project can be found here.]
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 constructing 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.
I'll post more examples in this thread.
Last edited by catburton; 05-28-2012 at 07:13 AM. Reason: Added link to updated project.
Wow thanks for this. Unfortunately I'm getting a little red box with "Fatal Error : null" when I try compile this or just run the swf provided. Any idea where I may have gone wrong?
Last edited by tertle; 12-23-2011 at 04:56 AM.
You need a content debugger of the flashplayer; please check out the faq here; http://unity3d.com/unity/preview/faq
Without a proper error message I can't tell you what's going on, but most likely your swf isn't published with version=13 or your wmode isn't set to direct on the flash embed, or your flashplayer is lower then 11.
Silly me. Forgot I had 11.2 beta build installed. Cheers.
-edit- hmm actually runs fine in 11.2 content debugger. just null errors in browser.
Last edited by tertle; 12-23-2011 at 05:09 AM.
You can send and receive an arguments with this method (Replace WWW & WWWForm) ?
darkloy, yes, check out the provided example; it show sending arguments both ways.
Hi Ralph! Thank you for your post.
Can you tell us, is it possible now to integrate compiled stage3d layer (that unity builds) with native flash GUI?
I mean in FlashDevelop or FlashBuilder
sorry for my english, my native language was c++
@darkloy : glad I could help, let us know how it works out for you.
Hi vtornik, in the example I remove the entire opaque preloaded from the stage; however, you can keep an overlay sprite / movie clip over the stage3d content. Keep in mind that this on some configurations/platforms might cost you some performance. However, it's entirely possible do your overlaying gui in flash and the game and the 3d in unity!
We'd love to see an example of someone using that functionality; let us know what you manage to do with it !
Last edited by UnityRalph; 12-23-2011 at 11:20 AM.
Thank you a lot, Ralph!
sorry for my english, my native language was c++
If you provide other examples I would be very happy: D ...
i'm trying to run the example in ie8 with the flash debugger and running vizzy flash tracer but when i click the button, nothing seems to happen.
any ideas on what i'm doing wrong?
This is really good stuff - just one more question:
Via the sendMessage method what sort of objects could I theoretically push accross from Flash to Unity? I'm thinking images (loaded from web or created) or audio files... is that possible? As a kind of WWW. replacement as it's missing in the current release?
@rogerisland; I'd advice to handle that type of media on the AS3 side for now; however, I will follow up with a new example showing how to for example put a video on a unity texture.
so i went back and reinstalled the setup and followed the instructions and as far as i can tell, it's still not working:
Install "content debugger" from Adobe Flash Player [used ie9]:
Go to http://flashplayerversion.com/, and make sure that it says 'Debugger: Yes': [says WIN 11,1,102,55]
Create a file called mm.cfg and added ErrorReportingEnable=1; TraceOutputFileEnable=1
i drag the AS3Communication.html into the browser, it loads up, it says running the in the debug player at the bottom right of the screen.
i click the button a few times
Find and open your flashlog.txt
it says: Player: init engine [space] MolehillGfxDevice: DirectX9 (Direct blitting)
is this "Player: init engine MolehillGfxDevice: DirectX9 (Direct blitting)" the result i'm looking for? this appears before i press the button and only appears on load so i'm assuming the expected result would be different.
Shifty Nevers : that looks fine to me; if you now hit the button you should get the flash traces; depending on what you compile the source with though, it might be stripping the traces out of the source code I provided above.