- Home /
How to use GUI.depth to bring a GUI.Box in front of other GUI elements?
I have a GUI.Box that is a selection box for units in my RTS game. Im wondering how to use GUI.depth to change the depth of this box so that it goes behind my other gui elements. I've tried setting the depth to positive and negative values but its not working going behind my other gui elements that are on the default sorting layer at depth 0 on the Canvas component. Please tell me what Im doing wrong. Thanks
void OnGUI()
{
if (UserIsDragging) {
boxWidth = Camera.main.WorldToScreenPoint (MouseDownPoint).x - Camera.main.WorldToScreenPoint (currentMousePoint).x;
boxheight = Camera.main.WorldToScreenPoint (MouseDownPoint).y - Camera.main.WorldToScreenPoint (currentMousePoint).y;
boxLeft = Input.mousePosition.x;
boxTop = (Screen.height - Input.mousePosition.y) - boxheight;
GUI.Box (new Rect (boxLeft, boxTop, boxWidth, boxheight), "", MouseDragSkin);
GUI.depth = 1;
}
}
}
Comment
Answer by Bunny83 · Oct 16, 2018 at 12:47 AM
The immediate mode GUI is drawn step by step. So you should setting GUI.depth at the beginning of OnGUI. Also note that GUI.depth can only affect the drawing oder of immediate mode GUI elements. The new UI system has nothing to do with the immediate mode gui.