- Home /
What is a GUILayoutOption[]
I am trying to write an editor script but I don't know what a GUILayoutOption[] is, and what to feed the parameter.
Answer by Bunny83 · May 07, 2014 at 12:54 AM
The optional "options" array of all GUILayout controls allows you to override certain aspects of the layout behaviour. It is a "params" array which always has to be the last parameter of a method. Possible "values" you can feed to a control are generated by those methods:
GUILayout.Width - overrides the fixedWidth of the used style
GUILayout.Height - overrides the fixedHeight of the used style
GUILayout.MaxHeight - adds an additional size-constraint
GUILayout.MaxWidth - adds an additional size-constraint
GUILayout.MinHeight - adds an additional size-constraint
GUILayout.MinWidth - adds an additional size-constraint
GUILayout.ExpandWidth - overrides stretchWidth of the used style
GUILayout.ExpandHeight - overrides stretchHeight of the used style
You can either setup an array yourself, add any of the above mentioned "values" and pass the array to the control, or, which is the actual intended usage, just pass the desired options as "additional parameters" like this:
if (GUILayout.Button("Button Text", GUILayout.MinWidth(100),GUILayout.Width(30)))
The "params" keyword allows to pass the array members as optional parameters. That's why it has to be the last parameter. All additional parameters become an item in the options array. If you don't need / want to override the GUIStyles behaviour, don't pass anything:
if (GUILayout.Button("Button Text"))
What if I wanted to change position? or even relative position?
It's not possible to change the position directly since GUILayout elements are layouted automatically. You can use GUILayout.Space to add empty spaces between elements, but usually you should setup the margins of you GUIStyle to affect it's position. I strongly recommend to use your own custom GUISkin if you don't use one yet.
You might also want to take a look at my generic Unity GUI crashcourse in **this answer**
The great thing with the GUISkin is you can change it at runtime while testing and it will keep the changes(so watch out what you change). This allows you to tweak the values / settings of your styles which the GUI is rendered and you immediately see the result.
Answer by toomasio · Jun 18, 2018 at 05:18 PM
You can create your own layout options in a stored variable if you want to use the same ones repeatedly. eg:
private GUILayoutOption[] textAreaOptions;
public void OnEnable()
{
textAreaOptions = new GUILayoutOption[]
{
GUILayout.Height(48),
//add more layout options
};
}
Answer by Jeff-Kesselman · May 07, 2014 at 12:58 AM
It is an array of objects of type GUILayoutOption.
for instance:
windowRect = GUILayout.Window(0, windowRect, ScalingWindow, "resizeable", GUILayout.MinWidth(80), GUILayout.MaxWidth(200));
GUILayout.MinWidth(...) and GUILayout.MaxWdth(...) are the GUILayoutOptions