Most of you have hopefully seen the incredible work of duck on these forums, in particular one of his latest pieces of work, the Vodafone Hometown GP.
While duck worked on all the Unity side of things in this project, I developed all the Flash parts of the application. This was quite a vast project, and it involved some heavy bi-directional interfacing between Actionscript and Unity.
It quickly became obvious that we needed a really robust system for handling messages and events between the 2, and as such I created a Javascript and Actionscript 3 framework to handle the entire process, which I’m hoping will be of use to some of you in your projects.
Even if you aren’t using Flash at all, UniTool offers an extremely streamlined and open method of embedding a Unity file into your page with 1 single line of Javascript:
var unity = uniTool.embedUnity("fileName.unity3d", "div_element_ID", 800, 500);
- fileName.unity3d is the path to the file you want to embed.
- div_element_ID is the ID of the div element you want to embed the object into.
- 800 and 500 are the width and height that the object should be embedded at.
The unity object reference then exposes many methods for things like showing, hiding, sending a message and completely unloading from the page.
UniTool also exposes a concise method for the detection and installation of the WebPlayer:
if (!uniTool.playerIsInstalled())
{
uniTool.addInstallListener(unityInstalledHandler);
uniTool.installPlugin();
}
function unityInstalledHandler ()
{
alert("installation complete!");
}
If you are working on a project that involves Flash and Unity, you have all these controls and more, directly from Actionscript. You can even programmatically embed a Unity object onto the page, directly from Flash, without any Unity object or logic previously existing!
Actionscript:
var unity:IUObject = UniTool.embedUnity("fileName.unity3d", "div_element_ID", 800, 500);
The parameters accepted here are exactly as with the Javascript implementation, and you can even pass in options for background color, logo image, text color etc. You then have reference to a Unity object, within Flash, that you can control directly from Actionscript!
The full source and information can be found on the UniTool Google Code Project. The full docs and examples for Javascript are a little sparse at the moment, but if you view the source of uniTool.js you should get a good feel for what’s available.
For those working with Flash, the UniTool AS3 documentation is extensive and should provide everything you need to know about the framework. Even if you aren’t working with Flash, I recommend having a look through the AS3 docs as most of the principles are the same when working directly with Javascript.
I hope some people find this useful, I know it’s definately going to be a lifesaver for me in any future projects involving Unity and Flash.
Let me know your thoughts.
