- Home /
GUI.BeginGroup for GUILayout elements
Hey guys. I'm trying to use GUI.BeginGroup() and GUI.EndGroup() to create 'nodes' for a tree editor. Im testing creating these individual areas to use the GUILayout elements at, but I seem to have the wrong idea about GUI.BeginGroup(). Can anyone set me straight? As this code suggests, the lower box is rendered first, then the upper box. But it seems that the first line in the top box is skipped, and I believe it has to do with the fact that a first line was written to in the lower box. Not sure why this is. Not sure how to fix it.
for (int index = 0; index < 2; index++) {
GUI.Box(new Rect(0, 50 + (index * -50), 50, 50), "_");
GUI.BeginGroup(new Rect(0, 50 + (index * -50), 50, 50));
GUILayout.Label(index.ToString());
GUI.EndGroup();
}
for (int index = 0; index < 2; index++) {
GUI.BeginGroup(new Rect(0, 50 + (index * -50), 50, 50));
GUI.Box(new Rect(0, 50 + (index * -50), 50, 50), "_");
GUILayout.Label(index.ToString());
GUI.EndGroup();
}
See what happen
Answer by Bunny83 · Sep 28, 2018 at 12:15 PM
The layout system works somewhat indepentently from the normal GUI system, Mixing isn't really recommended. However if you need a layout area inside "normal" GUI code you have to use GUILayout.BeginArea / EndArea.
Note that there is no reason to use GUI.Box explicitly. Layout groups as well as the BeginArea accepts a custom style. So you can just do
GUILayout.BeginArea(new Rect(0, 50 + (index * -50), 50, 50), "box");
GUILayout.Label(index.ToString());
GUILayout.EndArea();
Though if you have overlapping nodes you may want to actually use a GUILayout.Window instead. Though it's up to you how you implement your nodes.
You might want to have a look at my I$$anonymous$$GUI crash course. The original post is here but since UnityAnswers has messed up it's mark down rendering i've copied it into a github repository as it actually renders mark down properly ^^.