Search Unity

GUI Element Layering

Discussion in 'Immediate Mode GUI (IMGUI)' started by Aubrey-Falconer, Jul 21, 2008.

  1. Aubrey-Falconer

    Aubrey-Falconer

    Joined:
    Feb 13, 2008
    Posts:
    438
    From everything I've been reading, the "layer" of GUI elements should be controlled by the Z parameter of the transform of the GameObject they are attached to. Unfortunately, this doesn't seem to be the case for me. No matter what I try, certain buttons always show up on top of other buttons - and everything shows up on top of my custom cursor graphic. :)

    I am sure I am doing something wrong, but if noone has an idea of what it is I will create an example Unity project to demonstrate.

    Thanks Everyone!
     
  2. TJB

    TJB

    Joined:
    Mar 20, 2007
    Posts:
    146
    For a custom cursor try out the script in this thread:
    http://forum.unity3d.com/viewtopic.php?t=9554

    As for layering, I think the z position thing is just for the old GUI elements found in Unity 1. (They are still available in Unity 2, but few use them)

    As demontrated with the cursor script at the link above i think to change order of windows you can use:
    http://unity3d.com/support/documentation/ScriptReference/GUI.BringWindowToFront.html

    For other stuff i think (but am not positive) that the only way to change the order is to have one gui elements code before or after anothers in a script. Haven't tried it, just seems logical.
     
  3. Aubrey-Falconer

    Aubrey-Falconer

    Joined:
    Feb 13, 2008
    Posts:
    438
    Thanks for your ideas! I just stumbled across the answer in the manual:

    A GUI element's depth is controlled by (surprise surprise) the GUI.depth property :)

    By manipulating GUI.depth, you can superimpose GUI elements from separate scripts on top of each other however you desire.

    Hope this saves others some time...
     
  4. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Just beware that layering controls that receive user input is not supported currently.

    The reason is because of the immediate-mode GUI. Controls in the back have already received, handled and used events before the higher layer controls even "exist", so they won't get the events.

    So if you have a button behind a button, the button in the back will always get the event.

    I am not positive how that would work with layering from seperate scripts, but I think the results would be random.

    -Jeremy
     
  5. Aubrey-Falconer

    Aubrey-Falconer

    Joined:
    Feb 13, 2008
    Posts:
    438
    I ran into a problem with the Cursor.js script, in that it didn't allow me to center my custom cursor over the cursor position (Since the script used a GUIWindow, the window captured all the clicks).

    I rewrote the script to be much more simple, and now it works perfectly for crosshair cusrors and the like which need to be centered over the cursor position.

    In fact, it takes advantage of the layering issue Jeremy pointed out :)
     

    Attached Files: