Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

UniTool - Advanced Unity embedding and Flash Interfacing

Discussion in 'Made With Unity' started by duncanhall, Dec 13, 2010.

  1. duncanhall

    duncanhall

    Joined:
    Dec 13, 2010
    Posts:
    7
    Most of you have hopefully seen the incredible work of duck on these forums, in particular one of his latest pieces of work, the Vodafone Hometown GP.

    While duck worked on all the Unity side of things in this project, I developed all the Flash parts of the application. This was quite a vast project, and it involved some heavy bi-directional interfacing between Actionscript and Unity.

    It quickly became obvious that we needed a really robust system for handling messages and events between the 2, and as such I created a Javascript and Actionscript 3 framework to handle the entire process, which I'm hoping will be of use to some of you in your projects.

    Even if you aren't using Flash at all, UniTool offers an extremely streamlined and open method of embedding a Unity file into your page with 1 single line of Javascript:

    Code (csharp):
    1. var unity = uniTool.embedUnity("fileName.unity3d", "div_element_ID", 800, 500);
    • fileName.unity3d is the path to the file you want to embed.
    • div_element_ID is the ID of the div element you want to embed the object into.
    • 800 and 500 are the width and height that the object should be embedded at.

    The unity object reference then exposes many methods for things like showing, hiding, sending a message and completely unloading from the page.


    UniTool also exposes a concise method for the detection and installation of the WebPlayer:

    Code (csharp):
    1. if (!uniTool.playerIsInstalled())
    2. {
    3.     uniTool.addInstallListener(unityInstalledHandler);
    4.     uniTool.installPlugin();
    5. }
    6.  
    7.  
    8. function unityInstalledHandler ()
    9. {
    10.     alert("installation complete!");
    11. }

    If you are working on a project that involves Flash and Unity, you have all these controls and more, directly from Actionscript. You can even programmatically embed a Unity object onto the page, directly from Flash, without any Unity object or logic previously existing!

    Actionscript:
    Code (csharp):
    1.  
    2. var unity:IUObject = UniTool.embedUnity("fileName.unity3d", "div_element_ID", 800, 500);
    The parameters accepted here are exactly as with the Javascript implementation, and you can even pass in options for background color, logo image, text color etc. You then have reference to a Unity object, within Flash, that you can control directly from Actionscript!


    The full source and information can be found on the UniTool Google Code Project. The full docs and examples for Javascript are a little sparse at the moment, but if you view the source of uniTool.js you should get a good feel for what's available.

    For those working with Flash, the UniTool AS3 documentation is extensive and should provide everything you need to know about the framework. Even if you aren't working with Flash, I recommend having a look through the AS3 docs as most of the principles are the same when working directly with Javascript.

    I hope some people find this useful, I know it's definately going to be a lifesaver for me in any future projects involving Unity and Flash.

    Let me know your thoughts.
     
  2. KITT

    KITT

    Joined:
    Jul 17, 2009
    Posts:
    221
    Downloading now .. this looks good. Most assuredly, I will utilize this tool and do so with much gratitude.
    Cheers!
     
  3. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    223
    Interesting. I wonder if this would be useful in the Berkelium embedded Chrome plugin?
     
  4. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,245
    Pardon my noobish question:

    I understand that you can embed a Unity project into Flash, but does it also work the other way round?
    Can you play Flash inside a Unity project, for example a 3D room with a TV that you turn on and have an SWF animation playing as a texture on the TV screen.
     
  5. duncanhall

    duncanhall

    Joined:
    Dec 13, 2010
    Posts:
    7
    For the 1st point, you can't actually embed Unity 'into' Flash, either via the Flash IDE, or programmatically. What UniTool does is provide a Javascript bridge between Flash and Unity. So when you create a new instance of an IUObject in Flash, the call gets passed through to Javascript via Flash's ExternalInterface, then Javascript embeds the Unity Object into the HTML div specified.

    In terms of embedding a SWF into Unity, I'm not a Unity developer in any sense, so I wouldn't be 100% sure, but it seems the answer is no as far as I can tell: http://forum.unity3d.com/threads/24539-How-to-add-flash-file-in-unity3d.

    I have had some success in layering a SWF on top of a Unity file, using DOM elements and z-indexes, which kind of gives the same result. However, this was far too unstable for any kind of production release. The framerate tended to drop in Unity, Flash, or both, and some browsers simply wouldn't accept it
     
  6. duncanhall

    duncanhall

    Joined:
    Dec 13, 2010
    Posts:
    7
    Not heard of this, what is it exactly? If UniTool could be of use for it I'd definately be interested in helping out.
     
  7. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    223
    It's literally taking the whole chrome browser and embedding it inside of unity. eg in a unity gui panel, or on a 3d texture. Berkelium is another project which eases access to the chrome libraries for game engines.

    http://forum.unity3d.com/threads/53536-Berkelium-plugin-rendering-interactive-websites
    There's 2 google code repos in the thread. One for win and another branch for mac

    I ask because the main guy said the javascript to unity bridge is very simple at the moment. Not that that makes this project relevant, but it could be :)
     
  8. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Thank you!

    Will this work with chrome, safari, firefox and ie?
     
  9. duncanhall

    duncanhall

    Joined:
    Dec 13, 2010
    Posts:
    7
    Yep, I've tested pretty rigorously and it's fully cross-browser compatible and suitable for production releases. If you do happen to notice any problems, please enter them into the UniTool Issue Tracker and I'll try my best to get on to them as quickly as possible, although I'm in Asia at the moment and won't have my proper dev setup for a few weeks.

    Also, one thing to note: I've so far been committing/checking out purely with SVN, so the uniTool.js file available from the 'downloads' section is out of date, and does not include all the rest of the framework. For now, it is best to use SVN to checkout the entire project from the UniTool Source Checkout page. I will try and get the whole thing zipped up and added to the download page when I get a chance.
     
  10. duncanhall

    duncanhall

    Joined:
    Dec 13, 2010
    Posts:
    7
    Right, I've now zipped and uploaded the full source (both JS and AS3) and added it to the downloads section, so it is available without having to checkout via SVN. Anyone who previously just downloaded the uniTool.js file should instead download this zip file.
     
  11. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Hi Duncan,

    This is a very useful project, thank you very much for sharing it. I just downloaded your demo and gave it a spin (Firefox under OS X 10.6.5) and I noticed when the "embed" button was clicked and the Unity player embedded, the menu bar at the top disappeared until focus was returned to the webplayer. I haven't dug through it thoroughly yet to see what's, what, but is that to be expected?

    $Screen shot 2010-12-14 at 12.43.36 AM.jpg
     
  12. duncanhall

    duncanhall

    Joined:
    Dec 13, 2010
    Posts:
    7
    Hey, thanks for the input.

    I'm on a very limited Windows machine at the moment, so I'm unable to test this fully, but from the screen shot I think I have an idea of what's going on.

    The fact that the buttons in the SWF remain visible, but the background is masked, suggest this is a CSS issue. The example files are actually quite old and could definately do with an update. If you view the source of the HTML file you can see the CSS used for positioning both Flash and Unity is very basic. I would estimate that the CSS used is not entirely cross platform/browser compatible. This shouldn't be a major issue, as this is entirely independent of the UniTool framework itself (it's down to you how you want to structure/style your DOM elements). If possible, could you try editing the CSS and let me know if you get any results? Something like Firebug should make this very easy as you can live edit the CSS and see the results in realtime.

    I'll try and get a much more comprehensive demo done when I can, but my current location and setup make this rather difficult. It's a long story!
     
  13. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    I think you're right about it being a CSS issue. I just tried it with Safari (same machine) and it worked flawlessly. Thanks again, enjoy the weather in Thailand! ;)
     
  14. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    I'm trying the new example (flashEmbed) - but my page says "Unity plugin not installed :(" in FireFox.
    When I press "Preload Unity" - it loads something, but then I see nothing no matter what other buttons I press.

    Is there anything I might be doing wrong?

    big thanks

    EDIT: I tried Chrome, Safari and ie7 - don't work for me either.
     
    Last edited: Dec 15, 2010
  15. SamHorton

    SamHorton

    Joined:
    May 25, 2008
    Posts:
    7
    Hi Duncan,
    This is a fantastic resource. Thanks for helping put it together!

    I was noticing that it only seems to successfully embed unity content if run from body.onload. Is there a technical limitation that forces this to be the case, or is it intentional? The main reason I ask is because swfObject (which I compare this tool to) doesn't have any trouble embedding flash content outside of the onload function. In some instances with dynamic content, the onload function is not the most convenient place to do an embed.
     
    Last edited: Jan 2, 2011
  16. duncanhall

    duncanhall

    Joined:
    Dec 13, 2010
    Posts:
    7
    Thanks for the continued input guys, there's definitely some valid comments that I need to look into. I'm almost ready to start working on this again and will definitely look into these.

    In the meantime, could you try entering issues into the Google Code Issue Tracker so it's easier for me to see what needs doing?
     
  17. Vinícius Sanctus

    Vinícius Sanctus

    Joined:
    Dec 14, 2009
    Posts:
    282
    Ducanhall awesome share! I was just wondering how me and a friend could achieve something since i know nothing of flash and he knows nothing of unity! You finally are making us able to work togheter!

    Thx a lot friend! :>

    Hugs and keep up with the awesome programming.
     
  18. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    done! :)
     
  19. Robbilie2

    Robbilie2

    Joined:
    Aug 4, 2010
    Posts:
    262
    hey guys is there any need to install the unity webplayer plugin anymore when it is embedded in flash?

    if not then this wold be a possible way to make webplayer work on webos or bada mobile platform...?
     
  20. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,603
    its not embedded in flash (thats technically not possible at all), the unity webplayer still runs in the unity plugin. No unity plugin installed means no unity content
     
  21. johnboy007_2006

    johnboy007_2006

    Joined:
    Jul 14, 2011
    Posts:
    81
    Hi Duncan

    I want to be able to load my game in webplayer and have the functionality to record my voice and save it for playback, something that unity does not support.

    I was wondering if i could use this to communicate to maybe a flash app that will communicate with unity uing the microphone functionality.

    I would want this to be an offline deployment, would that be a problem?

    If this would work do you have any tutorials on how to set this up or examples?
     
    Last edited: Aug 3, 2011
unityunity