- Home /
How to NOT select an object behind a GUI?
Hey
I am selecting objects with raycasts but I would like to disable this behavior when the object is behind a GUILayout.
What is the best way to deal with this situation?
Is there a way to detect if the mouse is inside a GUILayout?
Should I put a GUITexture behind all my gui and use GUIElement.HitTest?
Or is using Rect.Contains() for each GUI windows the fastest way?
Note that I still want to be able to select the object if it is not behind the object so I can't simply disable the raycast when the gui is displayed.
Answer by CHPedersen · Nov 25, 2011 at 10:02 AM
One of the other guys might have a better idea, but the solution I'd go with is the Rect.Contains one. In case you're wondering, when you're using GUILayout, you have to use GUILayoutUtility.GetLastRect().Contains() to get the most recently drawn rect to test for the mouse position. Since you can't get to arbitrary rects with GUILayout but only the most recently drawn one, it means the code that disables the raycast needs to be put in each OnGUI that draws a rect, over which raycasting needs disabling. This could be multiple places, unfortunately.
I disagree. Seems to me it's just the least bad solution.
With such an emphasis on the difference between game surface and GUI, the engine should make this much, much easier.
I agree, actually. It is the least bad solution. But unfortunately, Unity's GUI system has quite a few shortco$$anonymous$$gs, most of which probably stem from the fact that real-time computer graphics is inherently an immediate-mode state machine. It's really hard to build something that resembles and behaves like a proper OO GUI system around that. So often, you get code that tends to look messy and requires strict discipline to not get out of hand.
Your answer
Follow this Question
Related Questions
Detecting a GUI element under the mouse? 1 Answer
Raycast needs to hit GUI 3 Answers
OnMouseUp() not working like meant 1 Answer
C# mouse Raycast question 3 Answers
Move selected object. 1 Answer