- Home /
useGUILayout=false does not honor GUI.depth
Hi, running on iOS, setting useGUILayout to false and using GUI.depth broke the depth sorting. All is working fine (i.e. GUI widget are sorted correctly according to GUI.depth vlaues) if i enable useGUILayout again. It seems that this is a bug already discovered by others, anyone came out with a solution?
Answer by Bunny83 · Apr 29, 2015 at 12:06 AM
It's not really a bug, just not documented proberly. Unity uses the Layout event for several things. One is to determine the drawing order. If you disable the layout event Unity can't determine in which order it should call the OnGUI methods. The order of the layour events is always the same. It's based on some internal order in which the instances have have been created.
For every event that is handled by OnGUI there will be a layout event right before the actual event. Of course only if those are enabled with useGUILayout = true. When you disable the layout event several things that depend on that event won't work anymore.
edit
Here's an example how you could manually distribute an OnGUI event the way you want:
// GUIManager.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public interface ICustomGUI
{
int GUIDepth{get;}
void CustomOnGUI();
}
public class GUIManager : MonoBehaviour
{
#region singleton
private static GUIManager m_Instance;
public static GUIManager Instance
{
get{
if (m_Instance == null)
{
m_Instance = (GUIManager)FindObjectOfType(typeof(GUIManager));
if (m_Instance == null)
m_Instance = (new GameObject("GUIManager")).AddComponent<GUIManager>();
DontDestroyOnLoad(m_Instance.gameObject);
}
return m_Instance;
}
}
#endregion singleton
void Awake()
{
// This class only works when useGUILayout is false
useGUILayout = false;
m_Instance = this;
}
List<ICustomGUI> m_List = new List<ICustomGUI>();
// Use this method to "register" a new instance
public static void Add(ICustomGUI aInst)
{
Instance.m_List.Add(aInst);
}
private int Compare(ICustomGUI a, ICustomGUI b)
{
return b.GUIDepth.CompareTo(a.GUIDepth);
}
private void Prepare()
{
// remove items which are null
for(int i = m_List.Count-1; i >=0; i--)
{
// the "else if" is for detecting Unity's "null" trick when an object is destroyed
if (m_List[i] == null)
m_List.RemoveAt(i);
else if (m_List[i].Equals(null))
m_List.RemoveAt(i);
}
// sort the list according to each "depth" value
// The highest value will be the first one
m_List.Sort(Compare);
}
private void OnGUI()
{
// if there's no item in the list, just leave
if(m_List.Count == 0)
return;
Prepare();
Event e = Event.current;
if (e.type == EventType.Layout)
{
Debug.LogError("The GUIManager only works without layout event");
}
else if (e.type == EventType.Repaint)
{
// draw them from the highest depth to the lowest
for(int i = 0; i < m_List.Count; i++)
m_List[i].CustomOnGUI();
}
else
{
// other events are send from lowest depth to highest
for(int i = m_List.Count-1; i>=0; i--)
{
m_List[i].CustomOnGUI();
// stop if the event has been eaten:
if (e.type == EventType.Ignore)
break;
}
}
}
}
And here's a sample script that uses the manager:
//GUISampleScript.cs
using UnityEngine;
using System.Collections;
public class GUISampleScript : MonoBehaviour, ICustomGUI
{
public int GUIDepth { get; set; }
public int StartingGUIDepth;
void Awake()
{
// important! This will register this instance so the manager knows about it.
GUIManager.Add(this);
}
// Use "CustomOnGUI" instead of OnGUI
public void CustomOnGUI ()
{
GUIDepth = StartingGUIDepth;
Vector3 p = transform.position;
if (GUI.Button(new Rect(p.x,p.y,100,50),gameObject.name))
{
Debug.Log(gameObject.name+" has been clicked");
}
}
}
So a class that wants to use our CustomOnGUI mechanism has to implement the ICustomGUI interface. That means your class need to have those things:
a public "int" property GUIDepth
a public method called CustomOnGUI
the class need to call "GUIManager.Add(this)" somewhere in Awake / Start in order to have the CustomOnGUI method run.
Note: You don't need to attach the GUIManager somewhere. It will create an instance automatically which will be marked with DontDestroyOnLoad. So it's better to not manually attach the GUIManager script somewhere. If you do, make sure the scene where you placed the script in is only loaded once. If you load the scene again you will end up with two instances which would mess everything up.
ok thx, so there's no way to force a predefined order on script when useGUIlayout if disabled?
@aisenai: Well, i'm not sure if the usual Script Execution Order might help here. However it only can be used to order different classes. So if you have the same class several times i don't think there's a way around that.
Of course another option is to manually distribute the OnGUI call from a single class in the order you like. Though it might be a bit tricky to get everything right. $$anonymous$$eep in $$anonymous$$d that you usually want the Repaint event occure from back to front and all other events from front to back. This is important so the topmost gui stuff will receive the input first.
You could even optimise the events. If a script has already "eaten" an event, you don't have to call the following elements.
edit
I added an example GUI$$anonymous$$anager that might do what you want.
many thx Bunny83, great explanatiorn and example! Btw I think that the usual script order execution facility just order Awake, OnEnable and Update methods, so it's not viable. I'll try your example, I'll have to edit it a bit since we're using some tricks on the event chain in order to control what a user can click on the GUI widget during the tutorial stage.
Thx again!
GUI.depth should be taken into consideration even if useGUILayout is set to false. We have entered an issue for this : https://issuetracker.unity3d.com/issues/monobehaviour-dot-useguilayout-equals-equals-false-breaks-gui-dot-depth
Of course, implement your own GUI$$anonymous$$anager will give you greater control over the UI but the GUI system should be able to handle this situation :)
How could GUI.depth be taken into consideration without the Layout event? You set the GUI.depth inside the OnGUI method and the depth controls the order in which OnGUI should be called. You can't change the order of the OnGUI calls after you invoked them already. That's only possible when you call it twice.
OnGUI is an immediate mode GUI. Things are drawn immediately when the code is executed. So you can't change any order afterwards. That's also the reason why GUI,Window doesn't work when you disable the Layout event. Unlike GUI,depth the GUI.Window documentation states this:
if $$anonymous$$onoBehaviour.useGUILayout is set to false then a call to GUI.Window will not have any effect, even though it is not a GUILayout function.