Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

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

  1. Nembrot

    Nembrot

    Joined:
    Jan 2, 2012
    Posts:
    3
    easy use of Ralph code in flex below
    please note backgroundAlpha="0"
    stage3D is drawn on the back, and flex app starts with background clip visible,

    trying to figure out how to embed unityswf into flex app, most game sites accept one swf only, so this is a must.

    cheers

    <?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="windowedapplication1_applicationCompleteHandler(event)" width="1920" height="1080" backgroundAlpha="0">
    <fx:Script>
    <![CDATA[
    import mx.events.FlexEvent;

    protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void
    {
    // TODO Auto-generated method stub
    holder.addChild(new SimplePreloader());
    }

    ]]>
    </fx:Script>
    <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <mx:UIComponent id="holder" width="100%" height="100%"/>
    </s:WindowedApplication>
     
  2. RalphH

    RalphH

    Administrator

    Joined:
    Dec 22, 2011
    Posts:
    592
    To publish a swf to a gamesite, you can just submit your generated .swf file; you do not need to submit the swc, js or html.
     
  3. Mackey@ehsiplus.com

    Mackey@ehsiplus.com

    Joined:
    Jun 27, 2009
    Posts:
    45


    Great Fix...

    All i had to do was add backgroundAlpha="0" to application (Not that i don't appreciate it BUT...You could told me that weeks ago... No need to have waited for flex specific demo)

    But... Really thanks... I just have one more question (since no demo required)...

    How to control X and Y offset ?

    The loader allows you to specify width and height (UnityLoaderParams(false,720,400,true,true,true))... But i cant seem to specify the x and y... I really need to make a VIEWPORT... for Unity Content to be displayed... The UnityLoaderParams allows me to specify size SO specifying X and Y will allow me to create a VIEWPORT on my stage to display Unity3D - Content

    NOTE: Even thou you add the UnityContent as a child of UIComponent the UnityContent is actually displayed directly on the stage... NOT WITHIN THE UIComponent that you used to do addChild...

    So a UIComponent can even be one (or Zero) pixel... THE ENTIRE CONTENT AREA YOU USE in UnityLoaderParams will be displayed on the stage DIRECTLY.

    <mx:UIComponent id="uix" width="0" height="0" />

    is really just a PLACEHOLDER, something to use JUST to added the LoadedContent to the stated VIA an addChild on what can really be a DUMMY OBJECT (UIComponent)


    SO I THINK YOU SHOULD SUPPORT X and Y (just like width and height) as params for the UnityLoaderParams class...
     
  4. Almog

    Almog

    Joined:
    Nov 20, 2011
    Posts:
    22
    I'm not understanding the workflow at can someone better explain it to me, I have my AS3 project which I add some specif functions for networking to make an HTTP Post request.

    And I have my Unity project I'm not sure what I need to add to the Unity project not the code but the files? What do I take from my AS3 project into my Unity project.
     
  5. Nembrot

    Nembrot

    Joined:
    Jan 2, 2012
    Posts:
    3
    I know, but I want to create all things 3D related in unity, and everything else in flash, so I need to embed the swf into my main flex app,

    can you please post embedding tutorial, when I try to embed I get weird error:

    [Embed(source="assets/AS3Communication.swf")]
    private var embed:Class;

    exception during transcoding: flash.swf.actions.With: 2 was read. 55512 was required. UnityTest.mxml /UnityTest/src line 9 Flex Problem
    Unable to transcode assets/AS3Communication.swf. UnityTest.mxml /UnityTest/src line 9 Flex Problem

    it'd be great if you could calrify, fix this somehow,
    cheers
     
  6. Nembrot

    Nembrot

    Joined:
    Jan 2, 2012
    Posts:
    3
    When I tried some other unity created swf i got:

    exception during transcoding: Number of bits > 32 UnityTest.mxml /UnityTest/src line 9 Flex Problem
     
  7. voidward

    voidward

    Joined:
    Jan 3, 2012
    Posts:
    8
    I'd really like some help, I'm trying to get a game submission by the 5th with scoreboards and this seems like the only way I can accomplish this with unity flash. At the moment I can't even seem to get this to compile to any useful state.
     
  8. RalphH

    RalphH

    Administrator

    Joined:
    Dec 22, 2011
    Posts:
    592
    What does the debugger say ? What you are seeing is an image that is in the folder, and if there is no black bar showing loadprogress in the bottom, I'd say it's not finding the swf you want to load. You need to run your project in debugging mode, so you can see what's going wrong.
     
  9. RalphH

    RalphH

    Administrator

    Joined:
    Dec 22, 2011
    Posts:
    592
    @nembrot You don't have to embed the [unitycontent].swf into the loading swf; using the unityshared.swc you can load it in from a relative url, which if you put the swf in the deployment directory is thus just the swf name. The example at the beginning of this thread illustrates this.

    Many portals will accept multiple swfs.
     
    Last edited: Jan 3, 2012
  10. RalphH

    RalphH

    Administrator

    Joined:
    Dec 22, 2011
    Posts:
    592
    @mackey; I wasn't aware your bug was the transparency as I didn't have time to look at it at all, busy fixing bugs ;-)
     
  11. voidward

    voidward

    Joined:
    Jan 3, 2012
    Posts:
    8
    [SWF] Users:mandi:Downloads:as3_src 2:bin-debug:SimplePreloader.swf - 51,132 bytes after decompression
    [SWF] Users:mandi:Downloads:as3_src 2:bin-debug:AS3Communication.swf - 15,805,869 bytes after decompression
    VerifyError: Error #1014: Class flash.display3D.textures::TextureBase could not be found.

    Trying to build with flex 4.5.0, set version to 11.1, adding -swf-version=13 to arguments.
    Do I need something specific that I'm missing?
     
  12. RalphH

    RalphH

    Administrator

    Joined:
    Dec 22, 2011
    Posts:
    592
    Last edited: Jan 3, 2012
  13. voidward

    voidward

    Joined:
    Jan 3, 2012
    Posts:
    8
    Thanks, seems 4.6.0 flex SDK is mandatory. Configure manually to add the sdk and point it to the directory you extracted to.
    http://fpdownload.adobe.com/pub/flex/sdk/builds/flexundefined/flex_sdk_4.6.0.23201B.zip

    Also, if it's unclear to other noobs like me setting wmode (which is also mandatory) is done in: /html-template/index.template.html
    I added:

    params.wmode = "direct";
    and
    <param name="wmode" value="direct" />
    in the sections with similar parameters. Only one is likely needed, but the other doesn't interfere so I added both.

    Also make sure you set target version to 11.1

    Everything's communicating properly now, thank you for the help Mr.Hauwert.
     
  14. Mackey@ehsiplus.com

    Mackey@ehsiplus.com

    Joined:
    Jun 27, 2009
    Posts:
    45
    Sorry Ralph... Im wasn't trying to be a d*** or anything... I know you guys real busy over there.

    What about the X and Y coordinates... Can we specify... Or in later versions can that be possible?

    BTW... We love Unity 3.5... If anything just to start working out Program Logic from a FlashBuilder Application Point of view (Using Unity3d Content as more of a child view that can be controlled via unityshare.swc...LOVE IT... My test scenes from scratch WORK PERFECT...Other projects you gotta get in a recode in a way that compiles... Gotta loose some 3rd party stuff for now... But the FUTURE implementations look SOOOOO promising that i am going ahead with my design now... waiting for later flash export to support more native unity functionality)
     
    Last edited: Jan 4, 2012
  15. Mackey@ehsiplus.com

    Mackey@ehsiplus.com

    Joined:
    Jun 27, 2009
    Posts:
    45
    Hey Ralph... Is there any way to REMOVE unity content from stage (previously loading using the UIComponent way discussed above)?
     
  16. RalphH

    RalphH

    Administrator

    Joined:
    Dec 22, 2011
    Posts:
    592
    That's a good one; haven't looked at it at all yet, but we'll have it in the future.
     
  17. AndreasBL

    AndreasBL

    Joined:
    May 2, 2010
    Posts:
    5
    Any news on this example?
     
  18. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    Hey.

    Has anyone had success in sending more than one argument from Unity to Flash?

    I have tried several approaches without success, for example:

    Code (csharp):
    1. FlashHelper.EmitAsm( "FlashCommunicator$Responder$['sendWaypoints']({0}, {1}, {2})", unitId, targetX, targetY);
    or

    Code (csharp):
    1. object[] param = new object[ 3 ];
    2. object[ 0 ] = unitId;
    3. object[ 1 ] = targetX;
    4. object[ 2 ] = targetY;
    5.  
    6. FlashHelper.EmitAsm( "FlashCommunicator$Responder$['sendWaypoints']({0})", param);
    currently I do it like this... which obviously sucks :p

    Code (csharp):
    1. string waypointString = unitId + "#" + targetX + "#" + targetY;
    2.  
    3. FlashHelper.EmitAsm( "FlashCommunicator$Responder$['sendWaypoints']({0})", waypointString );
    And then flash has to split the string into its components again. Yeah. Not nice.

    So, I hope anyone has found a easier way?
    Cheers
     
    Last edited: Jan 6, 2012
  19. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,076
    I also wonder which types we can use as parameters - both ways.

    I tried EmitAsm with Dictionary<byte, object> and couldn't cast it back to anything useful. When I trace it (like in the sample), it tells me "CLIObject".
    Object(obj).constructor traces to "[class Dictionary$2]".

    Is there a way to communicate with anything but trivial strings?
     
  20. rcrowe

    rcrowe

    Joined:
    Jan 7, 2012
    Posts:
    5
    I've been able to pass multiple variables of simple types from Unity to Flash like this:
    Unity:
    Code (csharp):
    1. FlashHelper.EmitAsm("SendToFlashScript$responder$['add2numbers']({0}, {1})", 3, 7);
    Flash:
    Code (csharp):
    1. public function add2numbers(A:Number, B:Number):void {
    2.     trace("Unity called add2numbers(" + A + ", " + B + ")");
    3.     var C:Number = A + B;
    4.     trace("A + B = " + C);
    5. }
    BTW, this works too:
    Code (csharp):
    1. void dbg(string msg) {
    2.     FlashHelper.EmitAsm("trace({0})", "Unity says: " + msg);
    3. }
     
    Last edited: Jan 7, 2012
  21. rcrowe

    rcrowe

    Joined:
    Jan 7, 2012
    Posts:
    5
    Is there a way to debug the Unity side of things? What I'd like to do is something like attaching to the FlashPlayerDebugger process in Visual Studio (or Mono) so that I can set breakpoints in C# code, inspect variables, etc., but I can't seem to get that to work. What is the best way to do this? What are the Unity developers doing?
     
  22. ilson

    ilson

    Joined:
    Jan 6, 2012
    Posts:
    2
    Can anybody help me? The problem is cross domain

    unityContentLoader = new UnityContentLoader("http://myotherdomain.com/game.swf", this, params, false);

    Is it possible?

    I tried to use Security.allowDomain and Security.loadPolicyFile, but they don't make it work :(
     
  23. Oliver-Eberlei

    Oliver-Eberlei

    Joined:
    Jun 12, 2011
    Posts:
    110
    @rcrowe: Oh, you are right. This method does work. I must have made some different error when I was trying it the first time

    Code (csharp):
    1. public void SendCreateItemInHand( int unitId, int itemType, int count )
    2. {
    3.     FlashHelper.EmitAsm( "FlashCommunicator$Responder$['sendCreateItemInHand']({0}, {1}, {2})", unitId, itemType, count );
    4. }
     
  24. rcrowe

    rcrowe

    Joined:
    Jan 7, 2012
    Posts:
    5
    I've discovered that I can pass arrays of ints to Flash from Unity, and then access the array elements like this:
    Unity (C#):
    Code (csharp):
    1. int[] int_arr = {3, 7, 9};
    2. FlashHelper.EmitAsm("SendToFlashScript$responder$['unityIntArray']({0})", int_arr);
    Flash (AS3):
    Code (csharp):
    1. public function unityIntArray(int_arr:Object):void {
    2. var A:Number = int_arr.elements[0];
    3. var B:Number = int_arr.elements[1];
    4. var C:Number = int_arr.elements[2];
    5. }
    It's not much, but it's something anyway.
     
  25. Jarnul

    Jarnul

    Joined:
    Apr 22, 2011
    Posts:
    3
    [edit]
     
    Last edited by a moderator: Jan 16, 2012
  26. cebenv

    cebenv

    Joined:
    Jan 16, 2012
    Posts:
    1
    Is this communication between Flash and Unity only possible in C# right now? Is it possible in Javascript yet? I am a Flash Developer/3D Artist who is new to Unity. Sorry if it is a totally newb question.
     
  27. greg.s

    greg.s

    Joined:
    Jan 22, 2012
    Posts:
    1
    Hello all at this board and I've question to UnityRalph (or maybe someone else know the answer :) ):

    edit: not actual

    P.S. Very good tool and have a good luck with final release :)
     
    Last edited: Jan 22, 2012
  28. J_Moller

    J_Moller

    Joined:
    Jan 24, 2012
    Posts:
    7
    So there is no way of getting everything in a single swf?
     
  29. Lord_Pall

    Lord_Pall

    Joined:
    Sep 25, 2009
    Posts:
    52
    So is all of this deprecated with the new actionscript helper classes in rc1?
     
  30. RalphH

    RalphH

    Administrator

    Joined:
    Dec 22, 2011
    Posts:
    592
    Yes, we have a couple of changes with Flash integration that should help you a lot. More on that soon.
     
  31. Lord_Pall

    Lord_Pall

    Joined:
    Sep 25, 2009
    Posts:
    52
    We're poking through them already. Holy crap it's slick.

    Already figured out how to get objects back and forth using the ActionScript expression stuff
    ActionScript.Import("UnityBridge");
    object myBridge =ActionScript.Expression<object>("new UnityBridge()");
    ActionScript.Statement("UnityBridge.startNetwork({0})",this);

    And then in the actionscript I can call (manually)
    Unitycallback_CallbackTest_String("foo"); (This is the hardcoded name pulled from the generated as files)

    It seems to work,l but I don't like using the manually entered/looked up function names.

    I see where you're going though, and it's a huge improvement. I look forward to a quick explanation so we can push forward even more.
     
  32. mikeytrw

    mikeytrw

    Joined:
    Jan 24, 2012
    Posts:
    12
    How did you get it to build to Flash without complaining "Definition "blah" could no be found"?

    I'm assuming in your case 'UnityBridge' is a custom AS3 class you have written?

    I've even tried including my AS class in the Unity project (somewhat shooting from the hip here as there's no real examples or docs yet...) but alas to no avail...
     
  33. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    Hello,

    I got most of it to compile. My movie playing in flash. But when i try to call a function in my unity swf nothing happens.
    So i tried to implement the following for bug tracking and see if i can get some trace statements but Unity will have none of it and refuses to compile.

    Code (csharp):
    1. /Users/Kevin/Documents/Work/TKD/Temp/StagingArea/Data/ConvertedDotNetCode/global/Controller.as(24): col: 4 Error: Access of undefined property Controller$responder$.
    2.  
    3. public void DisableModel( object o )
    4.     {
    5.         #if UNITY_FLASH
    6.         FlashHelper.EmitAsm("Controller$responder$['unityHelloFunction']()");  
    7.         #endif
    8.         model.renderer.enabled = false;
    9.     }
    10.  
     
  34. Lord_Pall

    Lord_Pall

    Joined:
    Sep 25, 2009
    Posts:
    52
    Look in your local docs at the actionscript class.

    import UnityEngine.flash

    and use ActionScript.expression or Actionscript.statement

    And just put your .as files in an ActionScript folder under assets.

    That's the new way to do this.

    No need for a seperate loader, it just seems to work.
     
  35. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    Could you please provide an example? Slightly at a loss at what you mean.
     
  36. mikeytrw

    mikeytrw

    Joined:
    Jan 24, 2012
    Posts:
    12
    Wow, local documentation...why didn't I think of that? I've been plastering away wildly all day.

    Thanks for the pointer, I'll get cracking with that straight away.
     
  37. tripknotix

    tripknotix

    Joined:
    Apr 21, 2011
    Posts:
    744
    In the local documentation it says to use "import UnityEngine.Flash;"

    then write the code to talk to Flash from C#, however i think this is a typo, i believe its "using UnityEngine.Flash;"

    import is the term used in as3, and would show compiler errors as opposed to "using" which is the C# equivelent
     
  38. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    What is the correct way to trigger a function in flash from the unity swf?
     
  39. tripknotix

    tripknotix

    Joined:
    Apr 21, 2011
    Posts:
    744
    Raziel im still trying to figure it out, they shouldve included a demo script, and demo .as file, that way we can see how they structure everything. The Local docs do not give you details, more like , "heres a peice, heres a peice, heres a peice" without any full pie with the crust in tact.
     
  40. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    Well i downloaded the example project and used those bits of code, but Unity is throwing a fit if i try to export a flash file with:

    Code (csharp):
    1. /Users/Kevin/Documents/Work/TKD/Temp/StagingArea/Data/ConvertedDotNetCode/global/Controller.as(24): col: 4 Error: Access of undefined property Controller$responder$.
    2.  
    3. public void DisableModel( object o )
    4. {
    5.         #if UNITY_FLASH
    6.         FlashHelper.EmitAsm("Controller$responder$['unityHelloFunction']()");  
    7.         #endif
    8.         model.renderer.enabled = false;
    9.     }
     
    Last edited: Jan 30, 2012
  41. Spiphy

    Spiphy

    Joined:
    Jan 30, 2012
    Posts:
    2
    I downloaded the example code and managed to get everything running using the pre-complied AS3Communication.swf. When I attempt to build the project to the Flash target I get "error CS0103: The name 'FlashHelper' does not exist in the current context"
    I am running the 3.5.0f1 developer preview. Any help would be appreciated.
     
  42. tripknotix

    tripknotix

    Joined:
    Apr 21, 2011
    Posts:
    744
    Yeah they haven't created an updated version yet.
     
  43. Spiphy

    Spiphy

    Joined:
    Jan 30, 2012
    Posts:
    2
    I finally found the local docs buried in the unity install directory and managed to get the example converted to the new API. I have my working code below, the test.as file needs to be in an ActionScript directory in the unity project.
    The only remaining problem I have now is passing objects does not seem to work.

    SendToFlashScript.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Flash;
    4.  
    5. public class SendToFlashScript : MonoBehaviour
    6. {
    7.     public GUIContent buttonOneMessage = new GUIContent("Send Flash a Integer");
    8.     public int count = 0;
    9.  
    10.     public SendToFlashScript()
    11.     {
    12. #if UNITY_FLASH
    13.         ActionScript.Import("test");
    14. #endif
    15.     }
    16.  
    17.     void OnGUI()
    18.     {
    19.         if (GUI.Button(new Rect(10, 10, 200, 100), buttonOneMessage))
    20.         {
    21. #if UNITY_FLASH
    22.             count = ActionScript.Expression<int>("test.testFn({0})", count);
    23.             Debug.Log(count.ToString());
    24.             buttonOneMessage.text = "Send Flash a message: " + count.ToString();
    25. #endif
    26.         }
    27.     }
    28. }
    test.as
    Code (csharp):
    1. package {
    2. public class test {
    3.     public function test() {
    4.     }
    5.  
    6.     public static function testFn(count:int):int {
    7.         return count + 1;
    8.     }
    9. }
    10. }
     
  44. mrx000

    mrx000

    Joined:
    Feb 3, 2012
    Posts:
    25
    Could we please get an updated example for the new Flash API from Unity ?
     
  45. amarneethi

    amarneethi

    Joined:
    Feb 5, 2012
    Posts:
    2
    Im new to unity and never done any C# coding...

    But quite familiar with javascript... I modified the C# code to work wen I clicked on a mesh... but it is not working properly... here is my code...

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SendToFlashScript : MonoBehaviour {
    5.    
    6.     public object responder;
    7.    
    8.     void SetResponder(object o)
    9.     {
    10. #if UNITY_FLASH
    11.         //This sets the responder objcet to the instance as sent by AS3.
    12.         FlashHelper.EmitAsm("SendToFlashScript$responder$ = {0}.responder", o);
    13. #endif
    14.     }
    15.        
    16.     void OnGUI() {
    17.         Event e = Event.current;
    18.         if(e.isMouse  e.button==0){
    19.             Debug.Log(e.clickCount);
    20.             #if UNITY_FLASH
    21.             if(responder != null){
    22.                 FlashHelper.EmitAsm("SendToFlashScript$responder$['unityHelloFunction']({0})",this);
    23.             }
    24.             #endif
    25.         }
    26.     }
    27. }
    this triggers trace msg wherever I click in the scene... i want it to trigger only wen i click the object which has this script attached... both c# and javascript solution is welcome... thanks in advance... :)
     
  46. cannon

    cannon

    Joined:
    Jun 5, 2009
    Posts:
    751

    Under the new system there's no need to look up the generated/mangled function names.

    Look for [NotConverted] and [NotRenamed] under attributes in the new documentation, they basically allow you to create C# proxy classes that will be replaced on Flash export by .as files placed in the Actionscript folder. Your classes call the C# proxy class during edit, but once converted they end up calling the .as files instead.
     
  47. Jarnul

    Jarnul

    Joined:
    Apr 22, 2011
    Posts:
    3
    It's work =)) Very nice; c# --> as3 transfer complete! ;-)
    But how to call c# func from as3 class? as3 -->c# (CallBack ).
     
    Last edited: Feb 7, 2012
  48. GabrielTaylor02

    GabrielTaylor02

    Joined:
    Feb 8, 2012
    Posts:
    1
    unityContentLoader seems to process swf's only using Url's , would it be possible to use embedded swf's instead of Urls ?

    i'm able to embed the game like this :

    [Embed(source = 'game.swf', mimeType="application/octet-stream")] public static const game_swf:Class;
    public var game = new game_swf();

    and then how to achieve something similar to
    unityContentLoader = new UnityContentLoader(game, this, params, true);

    or, maybe it would be possible to embed those both swfs into a third ? not unlike Nembrot's flex exemple ?
     
    Last edited: Feb 8, 2012
  49. keraj37

    keraj37

    Joined:
    Nov 25, 2011
    Posts:
    28
    Many thanks to Unity3D Tech. for this greate update.

    If sameone could answer this simple question - where can I find FlashHelper class in newest beta 6? If it isn't possible, please state that. I quess (but only quess), that this procedure was replaced with ActionScript class methods, am I right? So, it seems that beta 6 has some Flex complier in it... So... it is meant to not use external swf?

    Regards
    keraj37
     
  50. tripknotix

    tripknotix

    Joined:
    Apr 21, 2011
    Posts:
    744
    @GabrielTaylor02
    yeah thats exactly why i havent done flash networking for the new version of the flash tools, because i used Embed heavily in the interface, for music, backgrounds..ect. [embed] is actually a Flex/Flash Builder 4.5 only feature.

    @keraj37
    FlashHelper is no longer included, they changed everything, and have not updated any examples. in these last couple of forum posts they give you instructions, but its hard without a template.