Search Unity

How to set GUITexture behind of 3d object?

Discussion in 'Immediate Mode GUI (IMGUI)' started by leegod, Jan 15, 2012.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    I want to make game's background image as GUITextrue object, but

    How to make GUITexture be behind of other normal 3d gameObject like sphere or planes?

    Currently, GUITexture covers all other objects.

    Thanks.
     
    xuhao likes this.
  2. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Posts:
    153
    First off you'll want to add a new layer in the inspector, and set the GUITexture's layer to this new layer. Now select your Main Camera, click on 'Culling Mask' and uncheck the layer you just made, this will stop the main camera rendering objects (GUITextures) on this layer. Now go and create a second Camera and set its 'Clear Flags' to 'Depth Only' and its 'Culling Mask' to only render the layer you set up earlier for the GUITextures, then finally change this Camera's 'Depth' to -1. This should put the GUITextures behind the 3D shapes.
     
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    oh it works. Thank you.
    Main camera and second camera all needs to be set as 'Depth Only'
     
  4. c0dehunter

    c0dehunter

    Joined:
    Nov 23, 2011
    Posts:
    36
    Does this work for drawing a GUI.Box too? Because I can't make it work, I only see the GUI elements but not my 3D object.

    I've set my 3D object to another layer, set main camera's culling to everything except this new layer with 3D object and set depth to 1. Then I created another camera, set clear flags to Depth only, culling mask only to the new layer with my 3D object and depth to -1.

    Please help!
     
  5. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Posts:
    153
    Anything drawn in OnGUI will be rendered in front of everything else (3D or otherwise), so there isn't currently a way to have 3D objects render in front of OnGUI elements such as GUI.Box.
     
    Last edited: Jan 16, 2012
  6. c0dehunter

    c0dehunter

    Joined:
    Nov 23, 2011
    Posts:
    36
    Alright, thanks for reply.
     
  7. sona.viswam

    sona.viswam

    Joined:
    Nov 20, 2012
    Posts:
    2
    "how can i give layer to gui elements that i created in gui function"
     
  8. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    You can use GUI.depth, but it will work only if each "layer" is being rendered from different script.

    If in the same script (same OnGUI() method), the layering is determined by the order your components are rendered (back to front).

    Also note that there is the "processing order" bug, which makes overlapping controls handling input (mouse, keys) unclickable.
     
  9. orihq

    orihq

    Joined:
    May 25, 2016
    Posts:
    7
    Create a canvas, and in the Inspector, set its Render Mode to Screen Space - Camera. And make your main Camera the Render Camera. Add something to the canvas. Maybe buttons. You'll see that you can now move your GUI elements back or in front of the rest of the game objects as you like. You can also change UI Scale Mode to Scale with screen size if you want that.