- Home /
Can I have toggle buttons in a selection grid?
I am trying to create a group of buttons where the user is able to have up to x buttons toggled at the same time. Is is possible to use selection grid to do this or must I create some custom code.
My idea for a custom solution would be to take in string array and then create new toggle button within foreach loop. Then somehow keep track of which buttons are toggled and have threshold for how many buttons can be toggled at the same time.
It is not an option to hard-code the buttons in because I will use this solution in many places with different string array each time.
Does anyone have experience with similar problem and is it possible to solve it in easier way than I described?
I'm trying to make the same thing, a toggle buttons inside of SelectionGrid. But I don't know how to render a "toggled state" for the GUIContent. If someone know, please show here.
You need to have an array of boolean values to represent the status of every toggle button. Then each array value is associated with each button. Take a look at my answer, it should be clear.
Answer by Mazvel · Mar 16, 2014 at 03:42 AM
I did my custom solution for this. I used toggled buttons and had a function to check if the button could be toggled or not.
//This function is called after click on a toggled button. If the toggled limit was reached then it will be untoggled.
//However if the limit has not been reached then the counter is increased and the button remains toggled.
//And at last if the button changed to untoggled then the counter is decreased.
void checkToggleLimit(int position, int maximumAllowed, ref int counter, ref bool[] previous, ref bool[] current){
if (previous [position]) {
counter--;
previous [position] = false;
return;
}
if (!previous [position] && counter < maximumAllowed) {
counter++;
previous [position] = true;
return;
}
if (!previous [position] && counter == maximumAllowed) {
current[position] = false;
}
}
Then I created boolean array for buttonStates and for previous buttonStates. The arrays were initalized to false. I also had counter for number of toggles and maximum number of toggles. Here is example of where the GUI is drawn and where I call the function after the toggleState of a button changes.
for(int i = 0; i < stringArray.Length; i++){
toggledButtons[i] = GUI.Toggle(new Rect(75 + 170*(i%4),80 + 30*(i/4), 150, 25), toggledButtons[i], stringArray[i], "button");
if(toggledButtons[i] != previousToggledButtons[i]){
checkToggleLimit(i, maximumAllowedToggles,ref numberOfToggles,ref previousToggledButtons, ref toggledButtons);
}
}
I was placing the buttons in a grid with 4 buttons in each row.
Your answer
Follow this Question
Related Questions
Is there a multiple selection ugui toggle group? 1 Answer
Weapon Selection Script 1 Answer
Whats wrong with my GUI.Toggle? 2 Answers
Can you use an Axis to navigate a Selection Grid easily? 1 Answer
3 sets of button activations GUI 1 Answer