- Home /
GUI window gets drawn behind other gui elements.
This a strange bug where GUI window of mine gets drawn behind other elements of gui.
PS: all the elements are drawn from same file and irrespective of the GL contents, it still gets drawn behind the other elements. especially begin groups / boxes. Tried everything from changing drawing order.
Could you show us the code you use for 3 windows that overlap themselves, and tell us what code corresponds to which window ?
it is a bit more complicated actually and the logic resides across various files but gets called from a single OnGUI function...
and only of of it is window which you see at the bottom of the graph.
assume that each component u see in the window comes from a different file but the manager calls all these files from its ongui
void OnGUI ()
{
GUI.depth = 2;
foreach(GUIPage page in allPages)
{
page.DrawGUIObject(Vector2.zero);
}
}
the above is the script from manager that calls all the gui elements.....
and the below is where the gui elements gets drawn for each page (a page can be a begin area or window or might refer global gui as well!)
public override void DrawGUIObject (Vector2 offset)
{
objectsList.RemoveAll(obj=>obj==null);
bool pageEnabled = false;
pageEnabled = (parentPage == null)?isEnabled:parentPage.isEnabled && isEnabled;
offset = (parentPage == null)?Vector2.zero:parentPage.boundingRect.Top();
Event e = Event.current;
if(pageEnabled )
{
if(pageType != GUIPageType.Window)
{
if(hasArea)
{
if(pageType == GUIPageType.BeginAreaWithBG)
{
GUI.Box(boundingRect.SubstractWith (offset),"");
}
else if(pageType == GUIPageType.RoundedCorners)
{
if(e.type == EventType.repaint)
{
GLExtensions.GLDraw.DrawRoundedBox(boundingRect.SubstractWith (offset),boundingRect.width*0.075f,Color.white,boundingRect.width*0.0025f);
}
}
GUI.BeginGroup(boundingRect.SubstractWith (offset));
}
if( !isAutoLayout)
{
GUISkin skinBckp = GUI.skin;
if(skin != null)
{
GUI.skin = skin;
}
foreach( GUIObject obj in objectsList)
{
if(obj!= null)
{
// Debug.Log(obj.name);
if(obj.enabled && obj.GetType() != typeof(GUIPage) )
{
obj.DrawGUIObject( hasArea ? boundingRect.SubstractWith (offset).Top() : Vector2.zero);
}
}
}
GUI.skin = skinBckp;
}
if(hasArea)
{
GUI.EndGroup();
}
}
else
{
GUI.Window(depth,boundingRect,Window,_name); //Note that for windows, ID will be taken from depth!
}
}
}
void Window(int id )
{
foreach( GUIObject obj in objectsList)
{
if(obj != null)
{
if(obj.isEnabled)
{
obj.DrawGUIObject(boundingRect.Top());
// Event.current.Use();
}
}
}
}
Answer by flamy · May 14, 2014 at 06:48 AM
It is some bug with unity or the way GUI windows work! i dont know but the problem was with events of unity3d, When GUI Layout is getting called, the GUI window is supposed to be drawn, otherwise the window wont be displayed. And also repaint for the window gets calls as soon as first call to GUI.Window is being made that is the layouting event, so the window gets drawn first and then the other objects repaint event, hence the window goes behind everything.
this doesnt happen when I tried to call the whole gui from same script! But happens when i have the gui components across different scripts and try to call all of them from a single ongui.
And i solved it by using this
if(e.type == EventType.layout)
return;
for other components and by calling GUI window as the last call.
Still not clear on why this happens but will mark this as answer until i get an exact one!
Answer by Kacheek · May 07, 2014 at 08:00 AM
without the code i can unfortunaly not really help but i think it may help if i tell you that the gui elements are drawn in the order you type the code
for example
draw gui A;
draw gui B;
itll first draw gui A and then gui B on top of it
I know that very well i am calling the gui window at the end of all calls