- Home /
Staggering buttons with GUI.SelectionGrid
I'm trying to offset buttons so they are layed out at a diagonal from each other. Each one corresponds to a different view of the screen; so, it would make sense to use GUI.SelectionGrid
to have the buttons provide feedback for which screen the user is currently on. Is there anyway to do this with one SelectionGrid
? I was thinking placing them in a GUILayout
would be the way to go. If that's the right path would it just take nesting the SelectionGrid
between GUILayout.BeginHorizontal
and BeginVertical
?
Here's a screen shot of what I'm attempting to accomplish:
The SelectionGrid
just seems like the class I want to use to have selected buttons highlighted.
Answer by robhuhn · Dec 06, 2013 at 08:51 AM
There is no way to align the items within a selection grid afaik. I used toggle buttons with a button skin for that and controlled their state by an array.
just a small example:
bool[] myStates = new bool[3]{true, false, false};
...
GUILayout.BeginHorizontal(); //your custom layout instead
{
if(GUILayout.Toggle(myStates[0], "Pay", myButtonStyle))
{
CheckState(myStates, 0);
}
if(GUILayout.Toggle(myStates[1], "Politics", myButtonStyle))
{
CheckState(myStates, 1);
}
if(GUILayout.Toggle(myStates[2], "Strike", myButtonStyle))
{
CheckState(myStates, 2);
}
}
GUILayout.EndHorizontal();
...
protected void CheckState(bool[] values, int selectedIndex)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = i == selectedIndex;
}
}
oh yeah, and I could just skin the toggles to look more like a button.
I have just one question about your code. I noticed commenting out values[i] = i == selectedIndex;
stops the selected toggle from switching. How is values[i]
changing the value of myStates
without passing by reference?
Your answer
Follow this Question
Related Questions
Styling an individual button in SelectionGrid 0 Answers
SelectionGrid is not returning a value!! 1 Answer
GUILayout not working for me C# 1 Answer