- Home /
GUILayout formatting help
Using a series of nested BeginVertical and BeginHorizontal GUILayouts, Ive created a nice flexible content section in my app that changes size depending on what is drawn inside each section.
My GUILayout consists of two vertical layouts side by side but the left vertical is divided into two verticals.
Ive attached a picture to show what I mean,
What Id like to do as shown in the picture is make it so that when the right vertical section is small enough, the bottom vertical layout should expand all the way over.
Are there any ideas on configurations that could achieve this?
Thanks,
Here is my code for the layout
public delegate void Content();
public static void ThreeDownOneSide(Content[] panel1, Content[] panel2, Content[] panel3)
{
GUILayout.BeginHorizontal();
GUILayout.BeginVertical();
GUILayout.BeginVertical();
{
foreach(Content content in panel1)
{
if(!content.isNull())
{
content();
}
}
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
{
foreach(Content content in panel2)
{
if(!content.isNull())
{
content();
}
}
}
GUILayout.EndVertical();
GUILayout.EndVertical();
GUILayout.BeginVertical();
{
foreach(Content content in panel3)
{
if(!content.isNull())
{
content();
}
}
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
}
Answer by iwaldrop · May 07, 2013 at 06:17 AM
There really isn't a way to just 'expand all the way over'; you have to chose one or the other, but of course you know that.
So what you need is a conditional statement to choose which layout to use. I usually like to breakup my layouts into functions that I can use interchangeably. You can run the below script and switch between the two (very simple) examples by un/checking the layout flag in the inspector.
using UnityEngine;
using System.Collections;
public class InterchangableGUI : MonoBehaviour
{
public bool layout;
private delegate void DrawLayout();
private DrawLayout layoutOne;
private DrawLayout layoutTwo;
void Awake()
{
layoutOne = new DrawLayout(LayoutOne);
layoutTwo = new DrawLayout(LayoutTwo);
}
void OnGUI()
{
DrawLayout draw = layout ? layoutOne : layoutTwo;
draw();
}
void LayoutOne()
{
GUILayout.Label("LayoutOne");
}
void LayoutTwo()
{
GUILayout.Label("LayoutTwo");
}
}
A slight modification if you have more than one layout in $$anonymous$$d; the nice thing about delegates is you can assign methods from other scripts to be drawn in the same GUI function even though I've included them all in the same class for the sake of brevity.
using UnityEngine;
using System.Collections;
public class InterchangableGUI : $$anonymous$$onoBehaviour
{
public int layout;
private delegate void DrawLayout();
private DrawLayout[] layouts = new DrawLayout[3];
void Awake()
{
layouts[0] = new DrawLayout(LayoutOne);
layouts[1] = new DrawLayout(LayoutTwo);
layouts[2] = new DrawLayout(LayoutThree);
}
void OnGUI()
{
DrawLayout draw = layouts[layout];
draw();
}
void LayoutOne()
{
GUILayout.Label("LayoutOne");
}
void LayoutTwo()
{
GUILayout.Label("LayoutTwo");
}
void LayoutThree()
{
GUILayout.Label("LayoutThree");
}
}
Dang, thats what I was afraid of, having switch between different layouts manually. Well I suppose I can create a modified factory to do it automatically. However, the only way I can think of automatic switching is to be able to detect the size of each region and to switch between layouts depending on certain conditions. I have yet figured out how to detect the size of a GUILayout. Do you have any ideas?
Your answer
![](https://koobas.hobune.stream/wayback/20220613101651im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Smoothly "add another GUI box"? 0 Answers
GUI.Window error. InvalidOperationException: Hashtable.Enumerator: snapshot out of sync. 0 Answers
know GUILayout current screen position 2 Answers
"'UnityEngine.GUI.DoTextField' is inaccessible due to its protection level." 1 Answer
Why can't I get my tooltip to show only when there is a tooltip set? 2 Answers