UnityShared.swc; Communicating between Unity Flash Content and AS3.

Discussion in 'Flash Development' started by RalphH, Dec 23, 2011.

  1. precociousmouse

    precociousmouse

    New Member

    Joined:
    May 3, 2012
    Messages:
    6
    Hi all! I am new to Unity coding and have hit a wall regarding passing an object from a custom flash shell to a loaded unity swf. The object is passed ok to my javascript, but I am having issues trying to access the key/values within it.

    any help would be gratefully received!
  2. Boff

    Boff

    New Member

    Joined:
    May 2, 2012
    Messages:
    11
    @previoucmouse

    your idea is to export unity to flash, and then pull in the resulting swc + swf file to your flash project?


    All the FlashHelper commands are old as Ralph explain to me, (he needs to do a tidy up of that first post if you ask me ;))
    and has been replaced by the Actionscript API
    http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=ActionScript

    The new version of Unity 3.5 allows actionscript to be compiled with your unityfolders.
    Allowing you to call native flash functions when the files are exported.

    Cannon has sorted this out on page 6 (can't get the forum to link there directly to his post with a file in)


    However if you want to load up the resulting swf file into an existing project and call "function" in your normal stage -
    Cannon Actionscript is a flashproxy - a bridge if you will, which I've been using.

    Ralph's orginal example of what functions are exposed with unityContent is still valid, so both his example and Cannons call this function in the flash project you just loaded your unity.swf into.

    [actionscript project code ] _unity_loader.unityContent.sendMessage("Startup", "SetHost", {host:this});
    sends a message to the unity.swf to establish a conneciton

    [unity FlashProxy.cs] receives the SetHost command and calls the internally compiled AS file
    [unity FlashProxy.as] to set where the stage is.
    From here I've been putting functions in the FlashProxy.as script to called a similar function on the stage.

    hope this helps.

    where the stage is, so you can call functions on the stage (i.e. your flash project).
    Last edited: May 11, 2012
  3. precociousmouse

    precociousmouse

    New Member

    Joined:
    May 3, 2012
    Messages:
    6
    Thanks for the reply Boff - will have a go at it today after I get a ton of other projects out the way!
  4. catburton

    catburton

    New Member

    Joined:
    Mar 29, 2011
    Messages:
    43
    Hi guys,

    I've updated Ralph's initial example so that it should now work correctly. It simply required the FlashHelper code to be replaced with calls to the ActionScript API (as Boff correctly mentioned).

    I've bundled everything you need into one .zip file. You'll find 3 folders in there:
    • AS3-PreloaderSrc - The ActionScript source, loading image and UnityShared.swc required to build the custom Flash preloader.
    • AS3Communication - The Unity project which will be built to a SWF and loaded by the preloader.
    • BuiltExample - A folder containing the built SWFs. To see the demo running, open SimplePreloader.html.

    View attachment $FlashPreloaderExample.zip


    As with other Flash export examples, this updated demo works in the current Unity build but will be subject to change as the feature is worked on.

    Cheers,
    Cat
  5. carllooper

    carllooper

    New Member

    Joined:
    Aug 8, 2009
    Messages:
    64
    This is working well for me, except that I'm getting the following message at the bottom of the screen:

    Content uses capabilities that require a license. Visit adobe.com/go/fpl.

    The address redirects to: http://www.adobe.com/devnet/flashplayer/articles/premium-features.html

    There it speaks of something adobe is going to do on August 1, 2012, except that it's not yet that date at the time of writing this.

    UPDATE
    The message only appears when using the debug version of the flash player. Doesn't appear in standard flash player. So that's okay.

    Carl
    Last edited: May 15, 2012
  6. breadman

    breadman

    New Member

    Joined:
    Jun 5, 2012
    Messages:
    2
    Thanks, Cat! I have setup my own preloader project following this example but I run into an error when the content loader complete event fires. The unityContent property of the UnityContentLoader object is null.

    private function onUnityContentLoaderComplete(event:Event):void
    {
    addChild(_unityContentLoader);
    _unityContentLoader.unityContent.setContentHost(this); // Fails here due to null unityContent
    }

    I tried using AS3Communication.swf as my content to load instead of my swf and I get the same message. What am I missing?

    Using Flex 4.6 sdk

    Thanks,
    bread
  7. breadman

    breadman

    New Member

    Joined:
    Jun 5, 2012
    Messages:
    2
    More investigation shows that I only have a null unityContent error when the content swf I'm loading is served up from my local server. If I use AS3Communication.swf or my swf in the local project, they work fine. Is there a security restriction and a workaround?
  8. precociousmouse

    precociousmouse

    New Member

    Joined:
    May 3, 2012
    Messages:
    6
    Hi Cannon, for some reason I can't extract the archive you linked to as it seems to be damaged

    thanks!
  9. cannon

    cannon

    Member

    Joined:
    Jun 5, 2009
    Messages:
    695
    That's odd. Might've broken during the past forum maintenance. Direct uploads on the Unity forum also seem to be broken. I've attached the project sans built swfs here.

    Attached Files:

  10. BrentR

    BrentR

    New Member

    Joined:
    May 12, 2012
    Messages:
    2
    Hi, can some one tell me how I should build my fla to use this? Perhaps it's because I'm tired but I can't quite seem to get my head around it.
  11. precociousmouse

    precociousmouse

    New Member

    Joined:
    May 3, 2012
    Messages:
    6
    Thanks!

  12. precociousmouse

    precociousmouse

    New Member

    Joined:
    May 3, 2012
    Messages:
    6
    Ok, so I have the project up and running to a certain point. I have the unity swf being pulled into the main swf (built in flash), and I am dynamically building a UI on the flash side. My issue now is that a button on the flash side is triggering a function compiled in the unity swf and passing it an object, and that is working ok but I can't seem to access the contents of that object. eg:

    Flash side:

    var myString:String = "100";
    _unity_loader.unityContent.sendMessage("Startup", "SetCam", {thisCam:myString});


    Unity side:

    public void SetCam(object cam)
    {
    Log("SetCam called : " + cam.thisCam);
    }


    This won't compile as thisCam obviously can't be found in the compile in the context of the unity swf, but how would I return thisCam from the cam object passed from the fla? Logging just cam returns the usual [object object] so I know that the call is working ok.

    I think I am missing something obvious here but not quite sure what it is ;)
  13. precociousmouse

    precociousmouse

    New Member

    Joined:
    May 3, 2012
    Messages:
    6
    Fixed it! When the object is received in Unity, I pass it through as an expression to a function in the flash proxy which takes the object, grabs the content and returns it in the appropriate format. eg:

    unity side:

    public void SetCam(object cam)
    {
    #if UNITY_FLASH
    string thisNum = ActionScript.Expression<string>("FlashProxy.returnNum({0})",cam);
    Log("SetCam called : " + thisNum);
    #endif
    }

    fires a call to this:

    public static function returnNum(rn:Object):String
    {
    var myNum:String = rn.thisCam as String;
    return myNum;
    }

    Thanks to everybody for their help
  14. Sylos

    Sylos

    New Member

    Joined:
    Dec 14, 2009
    Messages:
    6
    Is there anyway to combine the preloader .swf and unity exported .swf into a single file?
    I've tried embedding the unity swf into a Flash Develop project and it doesn't seem to work.
    Maybe I'm doing something wrong.
  15. ds1987

    ds1987

    New Member

    Joined:
    Jul 4, 2012
    Messages:
    1
    Hi,, I made some changes on the Action script..but dont know how to recompile all the action scripts and the fla..any help would be great..i am a beginner in flash and unity..thank you
  16. catburton

    catburton

    New Member

    Joined:
    Mar 29, 2011
    Messages:
    43
    There are many tools available for this such as Flash Builder (trial), FDT etc. Perhaps pick one of those and follow their online documentation to get started. They have plenty of tutorials which should be enough to help you get the project building.
  17. Miyavi

    Miyavi

    Member

    Joined:
    Mar 10, 2012
    Messages:
    47
    Good morning,

    I'm having a problem while trying to load a Unity compiled project into an AS3 project.
    I just tried to do it the same way "catburton" does in her project, but when I add a .fla, and relate it to simplePreloader.swf, when I'm publishing it, it gives me an error saying that it can't locate the interface called "IUnityContentHost".

    I've been looking up the internet for "IUnityContentHost", but haven't been able to find anything related to it, which was of any help at all.

    So.. would anyone explain this part to me, and why it gives me problem locating it? I guess I need the library related to the interface, but as I said, I can't find it anywhere.

    I'm using Adobe Flash CS5, by the way.

    Is there any other way to load an Unity-compiled-to-swf project in AS3?

    Thanks a lot,

    Miyavi.
  18. catburton

    catburton

    New Member

    Joined:
    Mar 29, 2011
    Messages:
    43

    IUnityContentHost is an interface in the UnityShared.swc. This file will be output next to the swf when you build a Flash export of a Unity project.
  19. Miyavi

    Miyavi

    Member

    Joined:
    Mar 10, 2012
    Messages:
    47
    I just realised that reading another stuff.
    Thanks anyway.

    But.. the thing is that AS seems unable to locate the file, it seems.
    If UnityShared.swc is in the same folder as the .fla project, is it alright to do an "import com.unity.IUnityContentHost"?
    Or do I have to redirect anywhere else?
  20. Miyavi

    Miyavi

    Member

    Joined:
    Mar 10, 2012
    Messages:
    47
    So yeah.. after reading and reading, I guess I can't use flash cs5 nor 5.5, so I'm currently using FlashDevelop+Flex4.6
    Would anyone mind quickly explaining me how to run .as files into my project?

    Thanks,

    Miyavi

    EDIT:
    Hi again.
    I already imported everything into the FlashDevelop project.
    I play Test/Build Project, and it runs with no problem, afaik.
    However, it doesn't compile anything at all, I mean, I get no .swf as result.

    I'm having some issues, however. Regarding wmode, I'm supposed to set it in a .html file, but the only .html file I have is the one from my Unity-exported project.

    Regarding "-swf-version=13", should I set it in Project>Properties>Build>Pre-Build Command Line?

    Thanks a lot :)
    Last edited: Jul 20, 2012
  21. TJHeuvel.net

    TJHeuvel.net

    Member

    Joined:
    Jul 31, 2012
    Messages:
    37
    I'm trying to send some objects to flash, but run into a lot of problems. Specificly the following error:

    Happens with the following code:
    Code (c#):
    1.  
    2.     void Start()
    3.     {        
    4.         ActionScript.Statement("UnityManager.RecieveVariations({0})", GetNames());
    5.     }
    6.  
    7.     public string[] GetNames()
    8.     {
    9.         return new string[] { "Jack", "John", "James" };
    10.     }
    11.  
    I've tried different solutions (such as assigning the result of GetNames() to a local object[]), but nothing seems to work.
    Last edited: Sep 3, 2012
  22. Eric.vG

    Eric.vG

    New Member

    Joined:
    Sep 15, 2012
    Messages:
    3
    I've got a problem with compiling, I hope someone can help within a short time although I doubt this. My deadline is this wednesday.

    I'm using FlashDevelop, created a new AS3 project, imported the excact example .as scripts, got UnityShared.swc classes working in the project, so everything should be working.

    When I compile and run however, I get this error when compiling is done:
    [Fault] exception, information=ReferenceError: Error #1056: Cannot create property parameters on flash.system.LoaderContext

    Does anyone know what causes this error?
  23. haplo_3

    haplo_3

    New Member

    Joined:
    Oct 18, 2012
    Messages:
    18
    i also get the same error

    [Fault] exception, information=ReferenceError: Error #1056: Cannot create property parameters on flash.system.LoaderContext
  24. maskedbacon

    maskedbacon

    New Member

    Joined:
    Oct 25, 2012
    Messages:
    1
    Hi guys,

    Thanks for the great code!

    Question - how do I go about resizing the viewport once it's already set? I tried changing the width/height of the UnityContentLoader and I'm not seeing it update.

    David
  25. dashmasterful

    dashmasterful

    Member

    Joined:
    Feb 3, 2010
    Messages:
    166
    what was so hard about including an .fla file in the as3_src project?
  26. Tonzie

    Tonzie

    Member

    Joined:
    Jun 2, 2011
    Messages:
    115

    I'm also getting this issue:
    Exception fault: ReferenceError: Error #1056: Cannot create property parameters on flash.system.LoaderContext.

    I'm using FDT 5, and my project is using Flex 4.6.0 and Flash 11.4 swf-version 17

    Anyone know how to solve this issue?
  27. foobuggy

    foobuggy

    New Member

    Joined:
    Feb 22, 2013
    Messages:
    3
    So far the provided scripts have worked like a charm using Flash Builder. Just create a new actionscript project and bring the source .as files in along with the unity-created swc. Make sure your project library references (set these under the project properties) the unity swc as well as the proper playerglobal.swc for the target flash player version. Modify the as code as necessary to reference your unity SWF as well as your own custom preloader and then just build!

    I am currently running into an error though, that I think may be related to the playerglobal.swc. After building the project and running in debug, I get this error as soon as the Unity swf tries to instantiate the visuals:

    ReferenceError: Error #1069: Property setProgramConstantsFromByteArray not found on flash.display3D.Context3D and there is no default value.
    at com.unity::UnityNative$/Ext_Stage3D_Context_Set_ProgramConstants()
    at com.unity::UnityNative$/_ZN9GfxDevice14BeforeDrawCallEb()
    at com.unity::UnityNative$/_ZN14GfxMolehillVBO7DrawVBOERKN9ShaderLab14ChannelAssignsEmm16GfxPrimitiveTypemm()
    at com.unity::UnityNative$/_ZN8DrawUtil11DrawMeshRawERKN9ShaderLab14ChannelAssignsER4Meshi()
    at com.unity::UnityNative$/_ZThn28_N12MeshRenderer6RenderEiRKN9ShaderLab14ChannelAssignsE()
    at com.unity::UnityNative$/_ZN13BatchRenderer5FlushEv()
    at com.unity::UnityNative$/_Z25DoForwardShaderRenderLoopR23ForwardShaderRenderLoopR17RenderLoopContextR13dynamic_arrayI16RenderObjectDataLj4EL18MemLabelIdentifier52EEbbP13RenderTextureb()
    at com.unity::UnityNative$/_Z12DoRenderLoopR10RenderLoop13RenderingPathRSt6vectorI11VisibleNodeSaIS3_EEbP6ShaderRKSs()
    at com.unity::UnityNative$/_ZN6Camera6RenderEiP6ShaderRKSs()
    at com.unity::UnityNative$/_ZN13RenderManager13RenderCamerasEv()
    at com.unity::UnityNative$/NativeExt_PlayerLoop()
    at com.unity::UnityContentInternal/playerLoop()
    at com.unity::UnityContent/onEnterFrame()


    The preloader works just fine and I receive the proper percentage values as it loads. Also, once the preloader completes, I am able to see my debug output from Unity and it seems to be initializing properly with no problems. The Unity swf also plays fine on its own just using the swf/html generated by Unity with no errors. This error seems to be a result of combining the preloader swf with the Unity swf. Also, I'm pretty sure that I have referenced everything properly. Attached are a few images of my setup.

    $Screen Shot 2013-05-30 at 2.49.08 PM.png $Screen Shot 2013-05-30 at 2.49.43 PM.png

    The "embeddingapi.swc" is the file generated by Unity. The "playerglobal.swc" is referencing the 11.2 flash player. The "core.swc" is simply the core Flex library.

    If anyone has some insight on this it would be greatly appreciated. Thanks!
  28. foobuggy

    foobuggy

    New Member

    Joined:
    Feb 22, 2013
    Messages:
    3
    I actually found a solution for this. I changed the "-swf-version=13" in my compiler arguments to "-swf-version=15" and it worked just fine. Anyone having trouble might want to give that a shot.
  29. foobuggy

    foobuggy

    New Member

    Joined:
    Feb 22, 2013
    Messages:
    3
    Also, anyone interested in learning a bit more about the setup should check this out: http://tjheuvel.net/unityflash/
    It details how to embed your player in an AIR project, but it is essentially the same.
  30. diegoleao

    diegoleao

    New Member

    Joined:
    Oct 27, 2006
    Messages:
    21
    It happened to me today, but the problem was that I was using an external compiler to create a loader for my game (which is fine), but my external compiler was using a DIFFERENT FLASH VERSION to compile to.

    Once I chose the same version, it worked. So in any case, if you have this problem, take a look at your overall versioning of Flash files used in the project.
  31. RalphH

    RalphH

    Unity Technologies

    Joined:
    Dec 22, 2011
    Messages:
    508
    What diegolageao said. The swf that loads in the unity flash content should have the corect swf version set.
  32. datasoft

    datasoft

    New Member

    Joined:
    Sep 25, 2013
    Messages:
    4
    When the apk output from this project by flash , not run on the android device
    please help me to get apk by flash from this project
    i need to import unityswf to flash and relation with my flashswf and generate apk by flash , I've done this in Windows, but I need to do it in Android
    Last edited: Oct 4, 2013