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!

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:

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.