I asked this same question a few weeks ago and was told there was no way to easily do this.
Instead I created a static dictionary and when ever I make a GUI area, I register it with the dictionary. My camera ignores any Rects in the Dictionary by checking to see if the mouse is in any of them before doing any work.
I made a static bool which returns true or false if the mouse is over a rect.
Here is my code after hours of trail and error and C# websites…
/// <description>
/// Uses the RegisteredRects global static Dictionary to determine
/// if the mouse is over any registered GUI rects.
/// </description>
public static bool isMouseOverGUI
{
get
{
// Note: Not going to bother checking if there are any rects.
// There should always be, so the check is a waste of time.
// Check each rect to see if the mouse is over one. If it is, Quit
foreach (KeyValuePair<string, Rect> entry in registeredRects)
{
Rect rect = entry.Value;
// Invert the Y so 0,0 is in the upper left of the GUI.
Vector3 pos = Input.mousePosition;
pos.y = Screen.height - pos.y;
if (rect.Contains(pos))
{
return true;
}
}
return false;
}
}
To add items to the dictionary I just do (the above stuff is in a Static class called UI):
UI.registeredRects["MainMenu"] = mainMenuRect
I can do this from any script which uses a Rect and there is only a need to register the biggest, or outer, Rect.
Static classes and methods can be called from anywhere without getting a reference because you don’t need an instance. You use them directly. They are globally accessible.
If you aren’t sure how to create a static property which returns a Singleton Dictionary, there is a tone of info on the web on this.
Dictionaries are strongly typed so they are much faster than Hashtables (or so I’ve read) and you don’t have to cast the type when you retrieve stuff. I hope this helps. It certainly took me a lot of effort to figure this out.
Oh by the way, I originally used an ArrayList, but had to switch to a Dictionary so I could access and overwrite specific items. For example, in cases where a GUI Rect moves based on an object in the scene. In such a case, the Rect position changes and has to be updated in the Dictionary.
This should all be extremely fast.