Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Lord Simpson · Jun 10, 2011 at 10:17 AM · guibooleantoggleenum

Best way to combine GUI.Toggles and Flaged Enum

Hi basically I'm using [Flag] enum's to determine the allowed options for some objects e.g what types of objects there allowed to target. I'm trying to expose these options to the user so they can decide how they want the object to function. since I want it to be possible to have multiple options on at a time the only obvious GUI option is GUI.Toggle

however to use them I'm having to use the following code that seam far to long winded as i have to convert the enum to an array of bools but I'm sure there's an easier way but I cant seam to find it.

 //enum setup
 [Flags]
 public enum MyEnum
 {
    Group1 = 1,
    Group2 = 2, //etc
 
 //get the current enum setup into the bools
 for(int i = 0; i < 10; i++) //assuming 10 items in the enum
    bools[i] = ((enumInstance & (MyEnum) Mathf.Pow(i,2)) == (MyEnum) Mathf.Pow(i,2));
 
 //user gui options and save any changes
 bool changed = false;
 for (int j = 0; j < 10; j++)
 {
    bool temp = bools[i];
    bools[j] = GUI.Toggle (new Rect (220, (Screen.height - 190) + (15 * (j)), 100, 15), bools[j], ((TurretGroup)Mathf.Pow (2, j)).ToString ());
    if(selectedgroup[j] != temp)
       changed = true;
 }
 if(changed)
 {
    object.enumInstance = 0;
    for(int k = 0; k < 10; k++)
    if(bools[k])
    object.enumInstance = object.enumInstance | (TurretGroup)Mathf.Pow (2, k);
 }
Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Jun 10, 2011 at 10:37 AM

You could do it that way:

 TurretGroup enumInstance;
 private TurretGroup ChangeFlag(TurretGroup aOldValue, TurretGroup aFlag, bool aState)
 {
     return (aState)?aOldValue | aFlag:aOldValue & ~aFlag;
 }
 
 private void OnGUI()
 {    
     for (int j = 0; j < 10; j++)
     {
         TurretGroup currentItem = (TurretGroup)(1<<j);  // Much faster than Mathf.Pow
         enumInstance = ChangeFlag(enumInstance,currentItem,
             GUI.Toggle (new Rect (220, (Screen.height - 190) + (15 * (j)), 100, 15), ((int)enumInstance & (1<<j)) > 0, (currentItem).ToString ()));
     }
 }


Another way would be to use your own guistyles for on/off

 TurretGroup enumInstance;
 GUIStyle flagOn;
 GUIStyle flagOff;
 private void OnGUI()
 {
     for (int j = 0; j < 10; j++)
     {
         TurretGroup currentItem = (TurretGroup)(1<<j);  // Much faster than Mathf.Pow
         GUIStyle style = ((int)(enumInstance & currentItem)>0)?flagOn:flagOff;
         if (GUI.Button(new Rect (220, (Screen.height - 190) + (15 * (j)), 100, 15), (currentItem).ToString (),style))
         {
             enumInstance = enumInstance ^ currentItem;
         }
     }
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

Using an Enum for GUI.Toggle? 1 Answer

Pause Button Problems 2 Answers

EnumPopup in Javascript? 2 Answers

change the style of a gui by a key press 0 Answers

Toggle and Skin 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