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

GUI draw order ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by acme3D, Aug 4, 2009.

  1. acme3D

    acme3D

    Joined:
    Feb 4, 2009
    Posts:
    199
    Hi, I have a Unity scene that has different GUI textures that are reside on two layers (basically a background and some button icons over this background); I've put all the background elements in one gameObject and all the buttons into another gameObject; I can't figure out with what criteria Unity is using to draw the two things: sometimes the draw order is correct, but sometimes you compile again, without changing nothing and the draw order is reversed (background on top of buttons).
    Has anyone ideas on how to control this in a deterministic way ?
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Ultimately it is up to the order of execution for each GameObject which you don't have precise control over. If you want to force a particular layering order then look into using GUI.depth.
     
    samana1407 likes this.
  3. acme3D

    acme3D

    Joined:
    Feb 4, 2009
    Posts:
    199
    I was suspecting that; while waiting for an answer from the forum I started shuffling the Unity documentation and found GUI.depth, but doesn't seem to work either.

    I tried to attach the script

    var myDepth : int;

    function Awake() {
    GUI.depth = myDepth;
    }

    to the GUI texture, but with no results; changing the number on the two textures that I'm using doesn't change the overlapping order.....
     
  4. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    You need to set GUI.depth in OnGUI() every run.

    -Jeremy
     
  5. acme3D

    acme3D

    Joined:
    Feb 4, 2009
    Posts:
    199
    Sorry, but the issue concerns GUITextures, how can I use OnGUI to set parameters of GUITextures ? UnityGUI and GUI are supposed to be two different items....
     
  6. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    This is why it's good to clarify whether you're using UnityGUI (which supports GUI.depth) or GUIText/GUITextures (or for those like us helping it's good for us to ask up front if we're not clear). Your initial post confused me a bit as you wrote "GUI textures" would led me to believe you were using UnityGUI and thus my suggestion. If you're using GUITextures then you're right, things like GUI.depth won't help at all as that's an entirely different system. In the case of GUITextures a read of the documentation should prove helpful:

    GUITexture

    Near the bottom of that page you'll see the following:

    The depth of each layered GUI Texture is determined by its individual Z Transform position, not the global Z position.

    So control your layering by setting the z-position for your various GUIText/GUITexture objects and you should be all set!
     
  7. grimmy

    grimmy

    Joined:
    Feb 2, 2009
    Posts:
    409
    But what if the guiText and GuiTexture are components of the same object? Then they share the same z position and its impossible to change them.

    I find this a problem because this means I need to have two objects (drawcalls) for every button (guitexture) that has text(guitext) on top.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,387
    That would use two draw calls anyway; just making the GUIText and GUITexture components of the same object doesn't combine them in any way.

    --Eric
     
  9. grimmy

    grimmy

    Joined:
    Feb 2, 2009
    Posts:
    409
    Ive been trying this recently and the visual ordering is fine but when it comes to detecting touches with the ipod sometimes textures on top arent detected (the one under it is)...but then if you just reassign the box collider it works fine!.... but then other guitexture button detections in the same scene stop working. Its a really wierd bug.

    Does anyone know a bulletproof way of tracking guitexture touches on iphone?