Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by mikebelotti · Aug 27, 2012 at 03:46 PM · networkingflash

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mikebelotti · Aug 31, 2012 at 03:37 PM 0
Share

Do the ` characters not do code formatting anymore? Fantastic -_-

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 3.5 with AS3 1 Answer

Can I export a swf from Unity which is "local-with-filesystem"? 0 Answers

Unity networking tutorial? 6 Answers

Only one player for the host but two for the client, 0 Answers

Networking change of GameObject properties (color, size, etc) 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges