- Home /
GUI Depth Sorting
I am trying to create a menu structure which exclusively uses GUILayout so that it automatically sets itself out on any screen resolution. So far so good. Part of this system is a generic 'Drop-Down List' method, which uses the GUIUtility to reserve an slot for its base, and then when it is clicked draws a menu with all the elements in a generic list around the one currently selected- like
item1
line of normal text- item2
item3
item4
Then when you click on one of them, it disappears the list and returns the element of the list that you indicated. All well and good.
The problem is that when I have several of these lists on top of one-another, when I open one list, instead of clicking the buttons in the list, it tries to open up the other lists (or in fact, any clickable GUI element) which are below it! I'm using GUI.depth to make sure the list always draws on top, but it doesn't seem to help with the clickable stuff. Does anyone have a neat solution to this? I know exactly where the rectangles are on the screen (I use them along with the mouse position to automatically close the lists if the user mouses away from them), but I can't think of a way to stop it from clicking my other buttons!
Could you check wether they are clicked in the correct order? (Debug a click number or something). If yes, you could simply block the others from getting the click after the first click went to the correct control.
They don't get clicked in the right order, no. They get clicked in the order they are drawn in script, not in order of depth. The problem is actually that it already does that!
(I should mention that while the specific project I was working on when I asked this question has been finished now, I never really found anything better than a shitty workaround for this problem, so I'm still looking for a solution!)
Here's a bad solution you may have already seen that probably won't help:
http://answers.unity3d.com/questions/59898/how-to-draw-gui-controls-on-top-of-each-other.html
Another side note: the documentation for GUI.depth states that for this value to do anything, each depth layer needs its own script.
Answer by CHPedersen · Nov 16, 2011 at 10:37 AM
This problem is a total pain in the ass. It's been bothering people for a while, now. Most people refer to it as "GUI click through", and I've seen multiple questions attempt to address it, and very few reliable solutions.
I'm afraid the answer is that this is one of the pitfalls in Unity's GUI system that they simply haven't properly fixed yet, and therefore, people are left to resort to shitty workarounds (like you mentioned) instead of something robustly implemented in the framework.
One of the best (=least shitty) solutions I've seen talks about GUI.Button being broken in that it doesn't honor an indicator that was supposed to prevent it processing the GUI event when controls are layered. It provides a custom version of a GUI button that fixes this issue, and it's available in this forum post:
http://answers.unity3d.com/questions/16774/preventing-mouse-clicks-from-passing-through-gui-c.html
If you're using buttons to capture the button press in those DropDownLists, maybe it can be of use to you. If not, perhaps the way it fixes the button can work for your DropDownList as well.
Alternatively, you can use Event.Use() in order to make one script signal that it has spent a GUI event, and the event does not need to be processed by anyone else. But then you need to make sure you draw the lists in the order you want them to spend GUI events, (i.e. in GUI.depth ascending order), so that controls with lower GUI.depth (those drawn last) get to call Event.Use() first.
That last bit is a little tricky, I'm afraid- the entire point of the system depends on being able to make the calls in an arbitrary order, and leave the rest to the GUI system. In any case, thanks for the information- I'll try to implement some of this stuff in a reasonably general way.
I know, it's annoying. :-/ If you do come up with a solution that somehow addresses this issue better than the ones I've found when researching it, please do post it. I'd be interested to see a proper workaround that doesn't trigger everyone's code-smell-detector. :-)
Your answer
Follow this Question
Related Questions
Using treeview OnGUI with GUIlayout 0 Answers
Method is called, but GUI doesn't show up 1 Answer
Using an On-Screen GUI Button to show a GUI? 1 Answer
Multiple Cars not working 1 Answer
Can you use GUIStyle with GUILayout? 0 Answers