- Home /
Inter-process communication with a Unity SWF?
I have a C++ Windows program, into which I've embedded a Flash player (using this API) and I'm loading in a SWF that I exported from Unity. This works fine and dandy. But now, I want to transfer data back and forth between C++ and the SWF. Does anyone have a suggestion on how to go about this?
I saw this question which mentions a few possibly relevant things (like WWW and sending data from Unity to AS3 and back) but I'm still fuzzy on the particulars. I'm thinking I could do some sort of socket implementation, but the docs say that the built-in Unity networking API won't work with the Flash export, so I'd have to write it in AS3...? Is that the way to go, or is there something better?
Thanks for your time.
Answer by mikebelotti · Aug 31, 2012 at 03:35 PM
Got it. Here's what I did:
Sending data from a Unity SWF to C++
This was accomplished with a simple `ActionScript.Statement("ExternalInterface.call(\"FunctionIWantToCall\", {0})", thingIWantToPass);` where `FunctionIWantToCall` is a function in the main.cpp of my C++ program, and `thingIWantToPass` is, well, whatever. I just had to make sure that I exposed `FunctionIWantToCall` properly on the C++ side (this was done through the Flash-to-DirectX API I mentioned in the question) and called `ActionScript.Import("flash.external.ExternalInterface");` beforehand in Unity. No fancy casting, no crazy type-marshaling; it wasn't too much of a headache.
Sending data from C++ to a Unity SWF
This part is a bit more involved, but I was able to piece it together through bits of documentation found around UnityAnswers/Forums. On the Unity side, I have something like this:
public class FlashMediator : MonoBehaviour
{
public string dataFromCPP;
public void ReceiveStuff(object o)
{
ActionScript.Statement("FlashMediator$dataFromCPP$ = {0}.payload", o);
Debug.Log("This is Unity saying I GOT \"" + dataFromCPP + "\" FROM C++!");
}
}
which is basically Cat's example from here. For this to even work, though, I realized I needed another SWF to contain my UnitySWF. It's that parent SWF that's actually calling `ReceiveStuff` via `IUnityContent.sendMessage` in AS3. So I crack open FlashDevelop and come up with something like this:
public class Main extends MovieClip implements IUnityContentHost
{
var _unity_loader:UnityContentLoader;
private function init(e:Event = null):void
{
var ulp:UnityLoaderParams = new UnityLoaderParams(true);
_unity_loader = new UnityContentLoader("MyUnitySWF.swf", this, ulp);
ExternalInterface.addCallback("FromCPPToUnity", FromCPPToUnity);
addChild(_unity_loader);
}
public function FromCPPToUnity(str:String):void
{
_unity_loader.unityContent.sendMessage("FlashThingy", "ReceiveStuff", { payload:str } );
}
}
Note that that won't compile as-is; I tried to include only what's relevant to this question.
Basically, C++ will call `FromCPPToUnity` via the Flash-to-DirectX API, which is exposed to it by `ExternalInterface.addCallback` in the parent SWF. In this example, it's passing a string, which has to then be passed to Unity as an AS3 Object (hence the `{}`); I just gave it the property `payload` which is then referenced back in Unity. "FlashThingy" is the name of the GameObject in Unity to which I've attached the `FlashMediator` script from before, so, presumably, `sendMessage` will look for "FlashThingy" in the currently-loaded Unity scene, look for a function called `ReceiveStuff` on any of its components, and then call it, passing the object parameter.
And lo, a deed is done. Sorry if all this is obvious, but it certainly wasn't to me at first. I just figured that taking Unity stuff and having it run inside other (C++) programs is really useful--at least it is to me. Hopefully this is helpful.
Do the ` characters not do code formatting anymore? Fantastic -_-