Communicating Flash and Unity

This is a very simple example of connecting Flash and Unity through JavaScript. I did this example like 2 weeks ago but didn’t blog it (just tweeted it :-P)
Like Flash, Unity also has it’s own “ExternalInterface.call()” function called “Application.ExternalCall()“, since both Flash and Unity can call a function in JavaScript, communication between them (inside a browser) is easy.

For this example I just made a cube in Unity named “UnityCube”, added it to the scene, then created a new C# script and attached it to the cube. The important function in the CubeScript.cs is OnMouseDown, which invokes the JavaScript function called “resizeSquare” that will make a square to resize in Flash. It also has a function called “moveCSharp” which moves UnityCube in different directions.

On the Flash side, with ExternalInterface.call(“moveJS”, param) the function “moveJS” in JavaScript will be called and the cube in Unity will move in different directions dependig on the parameter sent (left, right, forward, backwards and jump). And function “resizeSquare” changes the size of the red square in the SWF.

Now for JavaScript, there are of course two functions, the one called from Unity (resizeSquare), and the one called from Flash (moveJS):

//This function is called from Flash (when one of the buttons is clicked) via ExternalInterface.call();
function moveJS(direction)
{

//sendMessage(); calls a function that belongs to an instance
//the firts parameter is the name of the target instance in Unity, next the name of the function we want to call, and finally, the parameter to send.

GetUnity().SendMessage(“UnityCube”, “moveCSharp”, direction);
}

//This function is called from Unity (when the cube is clicked) via Application.ExternalCall();
function resizeSquare()
{

//check how to call the ActionScript function depnding on the OS
if (navigator.appName.indexOf(“Microsoft”) != -1)
{
FlashUnity.resizeSquare();
}
else
{
window.document.FlashUnity.resizeSquare();

}

}

You can check the example (and source) here.

3 thoughts on “Communicating Flash and Unity

  1. Hi,

    Great work! Nice and simple.
    I’ve played a bit with your example and it is really cool but I have a question:
    What if I don’t want the unity app to be in a browser and instead being a standalone app .exe. How can I communicate with flash then?

    Thanks
    Pedro

    1. I think that would be by sending data to a server, and have Flash and Unity reading that information. Maybe there are other options but it’s the one that comes to my mind right now

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s