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.

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.