- Home /
Dynamic GUI Columns in GUI Area? (C#)
I'm having a big issue with my GUI. The basic style I'm using is:
GUILayout.BeginArea();
GUILayout.FlexibleSpace();
if(GUILayout.Button){}
if(GUILayout.Button){}
if(GUILayout.Button){}
if(GUILayout.Button){}
GUILayout.FlexibleSpace();
GUILayout.EndArea();
Each time a button is clicked, it is eliminated from the menu. In the end, only one button will remain. It works perfectly if the buttons are listed vertically in one column, but I want the buttons to be larger and in two columns.
My buttons have a pre-set width and height. My GUI Area has a width of 800, my buttons each have a width of 300. How can I make them take up the full space of the GUI Area?
TL;DR: A GUILayout area has buttons that disappear. How can these buttons be divided into columns?
Answer by perchik · Jul 15, 2013 at 04:56 PM
You'll want to look at GUILayout.BeginHorizontal() and GUILayout.BeginVertical()
Begin Vertical creates rows, beginHorizontal creates columns.
Your final script will probably look like this :
GUILayout.BeginArea();
GUILayout.BeginHorizontal(); //side by side columns
GUILayout.BeginVertical(); //Layout objects vertically in each column
... button 1 ...
... button 2 ...
GUILayout.EndVertical();
GUILayout.BeginVertical();
... button 3 ...
... button 4 ...
GUILayout.EndVertical();
GUILayout.EndHorizontal();
GUILayout.EndArea();
This will create a layout like this:
| button1 | button3 |
| button2 | button4 |
This works well enough for me. With another FlexibleSpace on either side of my rows, I can create two-button columns. It seems a little awkward to have a hopscotch effect (2 buttons, 1 button, 2 buttons) after one is eli$$anonymous$$ated, but it's the best outcome I could hope for at the moment.
Thank you for your help!
When you remove the button you could replace it with a GUILayout.Space and maintain the sizes of everything.
So that
| 1 | 3 |
| 2 | 4 |
would become
| | 3 |
| 2 | 4 |
when you click on 1. (If I understand your problem correctly)
Is it possible to do so when you have a generic function that create button according to and integer?
for(i, i < list.lenght, i++){ GUI.Button(Rect....) }
I would like them to be ordered into columns
This might be an amateur suggestion, but perhaps each row could be dependent on the integer exceeding a certain value?
For instance, if you want three columns:
If the number is more than 1, begin a horizontal space. Button 1 = List value 1. Button 2 = List value 2. Button 3 = List value 3.
If the number is more than 3, begin a horizontal space. Button 1 = List value 4. Button 2 = List value 5. Button 3 = List value 6.
Though if you wanted to use the GUILayout.Space, I suppose you'd have to check at every value... Right?
Your answer
