Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mazvel · Mar 15, 2014 at 01:03 AM · guitoggleselectiongrid

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ggmhm · Mar 25, 2014 at 09:16 PM 0
Share

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.

avatar image Mazvel · Mar 25, 2014 at 09:18 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges