Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Render UI to texture

Discussion in 'Immediate Mode GUI (IMGUI)' started by mstrg, Feb 5, 2010.

  1. mstrg

    mstrg

    Joined:
    Feb 5, 2010
    Posts:
    7
    Is there a way i can render GUI elements like GUI.Button or GUI.Box on a texture? I can do it with GUI Text but it dosnt work with stuff drawn with OnGUI()...
     
  2. mstrg

    mstrg

    Joined:
    Feb 5, 2010
    Posts:
    7
    Oh well, I'm pretty sure it can't be done with the current version (2.6). I'll have to use GUI Texture and GUI Text.
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    I'd like to be proven wrong, but I don't think there is any way of doing this, unfortunately.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Sadly / Happily, OnGUI gui is a post - post render FX, that means its the very last thing drawn in the scene.

    As such it does not exist for anything else in the scene renderingwise at all and you can't use it on other textures
     
  5. koichisenada

    koichisenada

    Joined:
    Nov 4, 2009
    Posts:
    10
    The on-screen GUI is rendered the last, that is true.
    But graphics libraries allow to render to texture, which can be a dynamically allocated and stored object in memory, thus the very next frame the texture is still accessible and stores what has been rendered to it in the previous frame.
    Besides, there can be introduced an architecture, where whole the GUI gets rendered to texture when changes occur, and then those rendered GUI textures used for displaying as on-screen or in-scene faces textures.
    Remember those Doom3 and Quake4 consoles with interactive screens all around the scenes?
    Another example of in-scene GUI here:
    http://www.cafu.de/images/phocagallery/guisys/thumbs/phoca_thumb_l_3D_GUI_4.png
     
  6. taoa

    taoa

    Joined:
    Dec 10, 2009
    Posts:
    88
    I'm curious about how you perform this actually :)
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    GUIText is a regular world 3D object

    OnGUI isn't a such and does not offer any of the related callbacks.
    Its executed on its own after the scene is rendered and finalized (including post fx) and happens past that point. It does not appear in any render texture.

    This case does not care the least bit about what graphics api do and allow. You can't use it in Unity thats what matters and what all this is about.
     
  8. taoa

    taoa

    Joined:
    Dec 10, 2009
    Posts:
    88
    Point taken :) new post created in 'Scripting'
     
  9. facundo

    facundo

    Joined:
    Apr 8, 2010
    Posts:
    5
    Actually, you can render UnityGUI to a texture.

    You need to set RenderTexture.active to the texture to which you want to write. Here's the documentation:

    http://unity3d.com/support/documentation/ScriptReference/RenderTexture-active.html

    Though by this time you have probably implemented your thing with GUIText. This solution was not trivial to find. I don't see what good reason there is for not rendering Unity GUI into camera textures.
     
    webitube2 likes this.
  10. koichisenada

    koichisenada

    Joined:
    Nov 4, 2009
    Posts:
    10
    facundo, that's a very great trick, thank you!
    For me the use is to render some interactive elements into a texture and apply it to a floating box.
     
    webitube2 likes this.
  11. vollnull

    vollnull

    Joined:
    Aug 26, 2010
    Posts:
    70
    Sorry to reactivate this thread, but how exacly do I use this in a script?
     
  12. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Posts:
    153
    Yeah, if someone could take the time to explain this a little better it'd be good, I was just about to post a thread asking pretty much the same question as the OP.
     
  13. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can set up a camera away from the main scene (pointing at a background image on a plane, say) with its GUI layer enabled, while other cameras have the layer disabled. Then, GUIText and GUITexture objects will render on that camera but not on the others. If you set the GUI camera to render to a texture, you can put the rendered GUI onto an object in the scene. Naturally, the GUI elements in the texture don't respond to user interaction unless you code this behaviour yourself. This technique isn't generally very useful (it's usually easier just to draw the desired GUI image in Photoshop) but it is handy for generating text that is gradually "typed" into a texture.
     
  14. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Posts:
    153
    Okay, I was looking at creating some Doom 3 style computer screens throughout a scene that would act as switches for various things (open/unlock doors, elevators, etcetera), I suppose this sort of thing could even be compared to the Pip-Boy 3000 in Fallout; rendering lists and such to a shader rendered in 3D space. As a start point, how does one render the elements generated in 'OnGUI' to a single camera 'or' layer so that they aren't rendered through by other cameras present in the scene? Other ideas?
     
  15. mindlace

    mindlace

    Joined:
    Mar 31, 2007
    Posts:
    24
    RoxSilverFox: EZGui makes this easy to do, from what I saw.
     
  16. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Posts:
    153
    That's all well and good, but I don't have $199 to be throwing around at the moment.