There's no way to draw GUI elements behind 3D objects - unless you use Render Textures! Use a special camera for the 3D objects that you want in the front of the GUI elements, set this camera to render to a texture, and draw this texture in front of the others.
To render to a texture, click on the camera and see its settings on the Inspector; there will be a Target Texture
, assign it to a texture. The camera will now draw on this texture, and you can use your GUI.DrawTexture
code to draw it in front of other textures.
You need Unity Pro to use this feature.
To check for clicks in these bunch of textures, the textures that are clickable can be verified if you create them like this:
Rect r = new Rect(x,y,width,height);
GUI.DrawTexture(r, texture);
Input.GetMouseButton(0)
if (r.Contains(Event.current.mousePosition)) DoStuff();
Now the problem is if you want to exclude a click in a button that is behind the 3D object when you click the 3D object. This is more complicated.
You'll first need to detect the click in the 3D object texture (like the code above).
Then, if true
, you'll have to cast a ray from the 3D camera (the camera that you assigned the texture) using the mouse coordinates (I think it may be easier to use Camera.ViewportPointToRay()
than Camera.ScreenPointToRay
to map the coordinates correctly, probably).
Finally, if some 3D object was hit, you just don't check the UI textures.