Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

[Flash] Example Flash UI over Unity SWF Content

Discussion in 'Flash' started by catburton, May 16, 2012.

  1. catburton

    catburton

    Joined:
    Mar 29, 2011
    Posts:
    43
    I've seen a few people asking how you can create a UI/HUD in Flash and have that appear on top of your Unity published SWF content.

    Here is a basic example which was based upon the existing Flash Preloader example.

    The basic preloader screen, button, text fields etc seen in the demo were created in Flash and published to a swc. I've then used the same sendMessage functionality from the preloader demo to show you how the HUD/UI can communicate with your Unity SWF content.

    The contents of the zip file are in three folders:
    • AS3-UI - The AS3 source and SWCs required to build the UnityContentHost.swf file found in the BuiltExample folder. This swf contains the preloader and ui. I've included the fla (CS6) for reference purposes.
    • BuiltExample - The full demo. Open the FlashUIExample.html file in your browser to run the demo.
    • UnityContent - The Unity project used to generate UnityContent.swf in the BuiltExample folder.

    This should hopefully give you a good starting point for how you could build UIs in Flash. Please note that whilst it is possible to show Flash content over your Unity built SWFs, this method of creating your UIs potentially comes with a large performance impact.

    As mentioned before, the Flash export is still being developed so this example is subject to change at a later date.

    Enjoy! :)
     

    Attached Files:

    Last edited: May 16, 2012
    Sir_Everard likes this.
  2. Triipaxx

    Triipaxx

    Joined:
    May 28, 2012
    Posts:
    4
    Thank you for sharing your knowledge with us. :)
    But is it possible to post your fla file as a CS5 project?

    kind regards,
    jan
     
  3. catburton

    catburton

    Joined:
    Mar 29, 2011
    Posts:
    43
  4. Triipaxx

    Triipaxx

    Joined:
    May 28, 2012
    Posts:
    4
    Hi Cat,

    thank you very much. :)

    best regars
    jan
     
  5. Triipaxx

    Triipaxx

    Joined:
    May 28, 2012
    Posts:
    4
    Hi Cat,

    the *.fla file is just a blank scene. is that correct?

    kind regard
     
  6. catburton

    catburton

    Joined:
    Mar 29, 2011
    Posts:
    43
    Yep. It's just got a couple of items in the library which are instantiated in the AS3 code.
     
  7. Triipaxx

    Triipaxx

    Joined:
    May 28, 2012
    Posts:
    4
    aaahhhhh i see!!
    but the main problem was, that the scene class file wasn't set to UnityContentHost.as and the UnityShared.swc file wasn't set in the library path. So i did not understand what this fla file is for. :)
    I'm a total flash beginner, that's why i dind't realised it in the beginning. :)
    but now everthing works perfect.

    thanks again for sharing!
     
  8. brettcook99

    brettcook99

    Joined:
    Nov 26, 2008
    Posts:
    58
    [Solved] I did not have flash player 11 installed as one of my publish targets in CS5. TextureBase is apparently a part of flash player 11.

    Hey catburton, thanks for sharing this. I'm getting stuck on trying to publish the fla from CS5. I've set the actionscript settings according to triipaxx notes, but am receiving this error when trying to publish the ui-cs5.fla you provided:

    1046: Type was not found or was not a compile-time constant: TextureBase.

    I can't find TextureBase anywhere in the code, is there a library I'm missing maybe?

    Thanks!
     
    Last edited: Jun 21, 2012
  9. Neroshiny

    Neroshiny

    Joined:
    Jul 3, 2012
    Posts:
    7
    Thanks. But is it possible to post your fla file as a CS4 project?
     
  10. Clark990

    Clark990

    Joined:
    Jul 11, 2012
    Posts:
    20
    Thanks for the CS5 version but can i get this in CS4? I shall be very thankful if i can get CS4.
     
  11. manuelv

    manuelv

    Joined:
    Feb 24, 2012
    Posts:
    14
    just create blank fla with reference with script
     
  12. ibps13

    ibps13

    Joined:
    Oct 6, 2012
    Posts:
    109
    Hi,

    Trying to use with unity4 b11, but I have an error 2142 sandbox violation, I use crossdomain.xml, autorized local folder... but nothing, UnityContent.swf is never loaded due to error in local and online preloader stay at 100%

    It's the same code of example, just changed embeddingapi.swc instead UnityShared.swc

    I use flash builder 4.5.6, flash 11.4, unity 4b11

    What is wrong ?

    Thanks

    EDIT

    After imported old "UnityContent.swf" and deleted "embeddingapi.swc" swf is loaded correctly but when I try to clic on button nothing happens.

    "updateScore" function is never called.

    here is the flex debugger trace:

    and here the modified code I used :

    Code (csharp):
    1. package
    2. {
    3.     import fl.controls.Button;
    4.    
    5.     import com.unity.IUnityContent;
    6.    
    7.     import flash.display.Sprite;
    8.     import flash.events.MouseEvent;
    9.    
    10.     public class BasicUI extends Sprite
    11.     {
    12.         private var unityContentRef : IUnityContent;
    13.        
    14.         private var getScoreButton : Button;
    15.         private var scoreText : lnkGetScore;       
    16.        
    17.         public function BasicUI(unityContent : IUnityContent)
    18.         {
    19.             unityContentRef = unityContent;
    20.            
    21.             getScoreButton = new Button();
    22.             getScoreButton.label = "Get Score";
    23.             getScoreButton.width = 120;
    24.             getScoreButton.x = 10;
    25.             getScoreButton.y = 10;         
    26.             getScoreButton.addEventListener(MouseEvent.MOUSE_UP, onChangeGetScoreButtonClicked);
    27.             addChild(getScoreButton);
    28.            
    29.             scoreText = new lnkGetScore();
    30.             scoreText.count.text = "0";
    31.             scoreText.x = 10;
    32.             scoreText.y = 40;
    33.             addChild(scoreText);
    34.         }
    35.  
    36.         // Call into the UnityContent swf. Instructs it to get the score
    37.         private function onChangeGetScoreButtonClicked(event : MouseEvent) : void
    38.         {
    39.             trace("a");
    40.             unityContentRef.sendMessage("FlashUICommunicator", "GetScoreOfPlayer");
    41.         }
    42.                
    43.            
    44.         // Called from the UnityContent swf once it's get the score
    45.         public function updateScore(totalScorePlayer : int):void
    46.         {
    47.             trace("b");
    48.             scoreText.count.text = totalScorePlayer.toString();
    49.         }  
    50.     }
    51. }
    52.  
    and the c#

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.Flash;
    3.  
    4. //Responsible for handling communication between the Unity published swf and the flash host swf (which
    5. //contains the UI).
    6. public class FlashUICommunicator : MonoBehaviour
    7. {  
    8.     public object flashUI;
    9.    
    10.     private int totalScorePlayer = 0;
    11.    
    12.     // Stores a reference to the FlashUI from the Flash host swf. Enables us to later call
    13.     // functions on the BasicUI class (in the flash project).
    14.     void SetFlashUI(object o)
    15.     {
    16.         #if UNITY_FLASH
    17.             ActionScript.Statement("FlashUICommunicator$flashUI$ = {0}.flashUI", o);
    18.         #endif
    19.     }
    20.    
    21.     // Called via sendMessage from the Flash host swf when the get score flash button is pressed.
    22.     // get the score and then requests for the host swf to update the value in the UI
    23.     public void GetScoreOfPlayer()
    24.     {
    25.         ScoreScreen scoreScreen = GetComponent<ScoreScreen>();
    26.        
    27.         totalScorePlayer = scoreScreen.getScorePlayer();
    28.    
    29.         #if UNITY_FLASH
    30.         if(flashUI != null)
    31.         {
    32.             ActionScript.Statement("FlashUICommunicator$flashUI$['updateScore']({0})", totalScorePlayer);
    33.         }
    34.         #endif
    35.     }
    36. }
     
    Last edited: Oct 26, 2012
  13. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    So I finally took a look at this. I couldn't get past the security sandbox violation locally, even if I added the local folder to trusted location. But I would like to get the new embeddingapi.swc to work, since I think it's renewed for a reason. So to get rid of the local error, I uploaded my stuff into remote server (and also found out that web sharing is removed from OS X 10.8, way to go Apple). The violation is gone of course, but It would crash to some error, didn't bother writing it down. I figured I probably have my Flash Builder 4.6 set to use too old SDK or something like that, so I switched over straight to FB 4.7, which is prerelease beta at the moment. Didn't get past the sandbox violation, but after uploading the stuff to server, at least the loading works.

    I haven't yet tested the communication between Flash and Unity, will get into it next. Kind of sucks that it seems impossible to debug things locally, any devs have hints for bypassing that? I've never had any issuses loading stuff locally in FB projects.
     
  14. ibps13

    ibps13

    Joined:
    Oct 6, 2012
    Posts:
    109
    thx mtoivo,

    Strange because "embeddingapi.swc" don't work too online for me, loading stay at 100% and the builded swf by unity is never loaded.
    Maybe catburton or RalphH have a solution :) or new example project that work with U4.

    to be continued
     
  15. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    The "not working locally" thing is a bug, fixed in the next beta according to Ralph. I've succesfully built a project doing the load with embeddingapi.swc, but I used FB 4.7, don't know what to tweak in 4.6 to make it work without crashing (I'm guessing the "preload stuck @100% might be because of unity crash behind the scenes). I'll post an example later, now I still gotta do some iOS testing and submit an update ASAP (living on the edge and using latest 4.0 beta for that also).
     
  16. ibps13

    ibps13

    Joined:
    Oct 6, 2012
    Posts:
    109
    Thx mtoivo,

    I try 4.7 SDK today perhaps resolved my problem with function.
    Maybe you can build a working example to communicate with parent swf.

    I also go to IOS now :)
     
  17. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    Okay, so my findings so far:
    - Trying to call unityContent.setContentHost gives null reference exception.
    - Because of the former, neither unityInitStart or unityInitComplete get never called.

    The combination I'm using: Unity 4.0 b11, Flash Builder 4.7 pre-release 2, embeddingapi.swc

    Loading itself goes fine, but would be nice to get a trigger when unity initialization is complete. Kind of hard to start communicating to unity, if one does not know when it's ready...
     
  18. ibps13

    ibps13

    Joined:
    Oct 6, 2012
    Posts:
    109
    Thx mtoivo for feedback, Ralph or catburton, any chance to have a working version or simple example ? :)
     
  19. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    Ralph tweeted, that things should work in the next build. embeddingapi.swc seems unusable at the moment. I tried the old unityshared.swc too with FB 4.7, and there seems to be some problems with communicating to Unity from Flash at least. I tried replicating the score-example as much as I could. I have had a little different approach to pass the messages. It's a bit more complicated, but the only way I managed to get it working when I was in a horry (and still doing code in US).

    I have (once again!) a static class in Unity called 'FlashProxy'. I pass the reference of the implemented IUnityContentHost to Unity via sendMessage, encapsulated inside object, like this:
    Code (csharp):
    1. unityContentLoader.unityContent.sendMessage("FlashUICommunicator","SetResponder",{host:this});
    In Unity, I forward that object to FlashProxy. The AS3-version of FlashProxy can then act as a proxy (doh) between Unity and Flash and call the functions of the main SWF (IUnityContentHost). This has worked so far, but now when I try this method, I get this:
    Code (csharp):
    1. Error #1034: Type Coercion failed: cannot convert Object@113f85af1 to UnityEngine._Object.
    So I guess we have no solid way establishing a working communication link between Flash / Unity untill we get the next update on beta. Hopefully things work there, would be cool if the methods previously worked would start working again...

    I took a major leap of faith and converted my current project to 4.0, mainly to get better control on iOS resolutions and stuff. I've now done some changes in there, but of course cannot publish a working Flash version of it now (due to the broken communcation channel). Luckily the Flash-version works, and even if I have to do something in there, I have the 3.5.6 -version of the project, and supporting two version is not that big thing (compared to all the other fights with this cursed project that are now in the past).

    "Live on the edge, use beta-software for client jobs"
     
  20. ibps13

    ibps13

    Joined:
    Oct 6, 2012
    Posts:
    109
    Hi mtoivo,

    Same thing on my side, I have 2 versions of my project... U4 have better function and my flash export works well ( without scoring :) )...
    I hope that this beta'll quickly turn into release candidate we are finally full potential Flash/unity.
    Thanks for you feedback man !!!
     
  21. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    Yeah. I think that no matter how perfect the Flash-export gets, there's going to be situations when using functionality directly from Flash is the best way to go. For 2D GUI, you cannot beat flash, with all it's vector graphics and stuff, it's the easiest way to go. If I were to create a flash-only project with Unity, I wouldn't even think of using UnityGUI for anything at it's current state. I sure hope ArenMook (the creator of NGUI-plugin) gets things working there when he begins to work on the new GUI-system. And I don't think all the .Net stuff will ever work fully in Flash anyway.

    BTW, If the score-system is the only thing you need from Flash, I would try and implement it with the NGUI, for example. It's Flash-compatible, but the free version doesn't work with Flash export.
     
  22. ibps13

    ibps13

    Joined:
    Oct 6, 2012
    Posts:
    109
    I send you pm mtoivo.
     
  23. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    Now (as of 4.0.0f3) the communcation between unity and flash seems to work. iTween is still giving some trouble, I'll try updating it (had a tweaked version of it installed, since the stock version didn't work with 3.5 flash export).
     
  24. ibps13

    ibps13

    Joined:
    Oct 6, 2012
    Posts:
    109
    Hi mtoivo,

    Can you provide simple example of communication ?
     
  25. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    I've been playing with this once again. My method of passing things from unity to flash seems to be broken in C# version (movieclip wrapped inside object causes "cannot convert Object@128647af1 to UnityEngine._Object."). I'm trying to put the communication things inside js-class, since it seems to work. I'll see if I can update on this tomorrow.
     
  26. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    It seems that the error I previously got was due to using Object instead of object... Took a while to figure that one out. It doesn't help that in ActionScript it's Object. Anyhow, everything works now as supposed to, once again. Didn't change a byte in the code, the embeddingapi.swc is indeed fully compatible with unityshared.swc.

    Here's a demonstration of it: http://w.supermikko.fi/unity/flashcom/preview/

    The input field is created in Unity, the button is created in Flash, as is the text field below the button. So pressing the button calls a function in Unity, which then returns the number in the text field to Flash function.

    Whole project is zipped here: http://w.supermikko.fi/unity/flashcom/FlashCom.zip. It includes the Unity 4.0 project as well as project folders for Flash Builder 4.6 4.7. There's no difference between the two at the code level, though. Since there was a fl.controls.Button component used in the previous example, I had to create a simple .swc to represent that component, fl.* -components cannot be accessed from Flash Builder by any other means.

    I don't know if it serves any purpose to paste the code in here too, shouldn't be too hard to figure things out from the sources in the zip. I tried to insert short comments to help you along the way.
     
  27. codes

    codes

    Joined:
    Nov 26, 2012
    Posts:
    10
    hi guys , i'm so confused about this function

    Code:
    void SetFlashUI(object o)
    {
    #if UNITY_FLASH
    ActionScript.Statement("FlashUICommunicator$flashUI$ = {0}.flashUI", o);
    #endif
    }

    who calls this function and when?
    and is o.flashUI declared in flash project? i cant find anything about it
    thank you guys, waiting for your help
     
  28. okokok

    okokok

    Joined:
    Aug 1, 2012
    Posts:
    24
    Last edited: Dec 3, 2012
  29. foquien

    foquien

    Joined:
    Dec 3, 2012
    Posts:
    2
  30. foquien

    foquien

    Joined:
    Dec 3, 2012
    Posts:
    2
    como ago para jugar esta verga
     
  31. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    397
    Hi there,

    Recently i've had the (dis)pleasure to have the task of embedding our Unity project in an existing Flash application. It was a fairly difficult task, but in the end i did complete it and learned a lot along the way.

    To share this information i've made a tutorial explaining everything in detail, from building and embedding your Unity project to communicating between Flash and unity. You can find the tutorial here: http://tjheuvel.net/unityflash/.

    I hope it solves some of your problems, or at least shed some light on Flash development :)
     
  32. iBadBoy

    iBadBoy

    Joined:
    Feb 27, 2013
    Posts:
    2
    Thanks for sharing good knowledge. I am a beginner may have had a lot of experience.

    Thank you again!.
     
  33. verteye

    verteye

    Joined:
    Nov 9, 2013
    Posts:
    4
    Hello everyone,
    I'm a beginner in unity environment, and I have a exporting problem, so please bare with me, thanks :)

    - First of, thanks catburton, for your support !

    and second, I'd like to ask, if someone can help me with this problem :
    when I try to export for flash in Unity, even thought when I created a completely new project,
    with an empty scene (only a camera there), I always get thees two errors :
    Os win7 | Unity 4 | Flash player - updated | JRE - installed |
    - or may be do I need a flex plu-gin ?..

    I've allso posted this here : http://forum.unity3d.com/threads/21...layer-Exception-java-Failed-C-flex-config-xml
    Tanks :)
     
    Last edited: Nov 14, 2013
  34. payalsharma9988

    payalsharma9988

    Joined:
    Oct 31, 2013
    Posts:
    25
    Such a great knowledge, Thanks catburton coz you share with us good knowledge
     
  35. verteye

    verteye

    Joined:
    Nov 9, 2013
    Posts:
    4
    any one, please help ? I still didn't get the flash export to work, neither does the web player export work ether. :(
     
    Last edited: Nov 14, 2013
  36. verteye

    verteye

    Joined:
    Nov 9, 2013
    Posts:
    4
    Fixed !

    I sismply updated java once agayn, and lounched Unity as administrator (in win 7 ). And that's it, as simple as that, but it fixed it all :) chears.
     
  37. WhiteMaegi

    WhiteMaegi

    Joined:
    Nov 20, 2013
    Posts:
    1
    Hi there,

    I've been trying to build the example that mtoivo provided above, but unity is refusing the connections from flash for some reason.

    I updated the unity project to 4.3.0 and am running the actionscript with flash professional CC. The unity .swf loads fine, but none of the function calls between the two seem to be doing anything.
    It's giving this output when the .swf loads:

    Unity loaded
    unityInitStart
    PlayerConnection initialized from (debug = 0)
    PlayerConnection disabled - listening mode not supported
    Initialize engine version: 4.3.0f4 (e01000627d60)
    MolehillGfxDevice:Software Hw_disabled=unavailable (Embedded)
    unityInitComplete

    I can provide the project folder tomorrow morning (9am EST) when I get back to my office if that would be more helpful.
    Any ideas as to why this is happening would be much appreciated.

    Thanks in advance