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 BogdanV · Mar 04, 2014 at 02:58 AM · enumignorebitwise

GUILayout items draw regardless of "IF" statements

Within my UI class I have the following enum set:

 >      public enum MessageBoxFlags
 >                 {
 >                     //Special
 >                     None = 0x0,
 >                     //Standard MsgBox templates
 >                     Diplomacy = 0x1,
 >                     ForeignNews = 0x2,
 >                     HomeNews = 0x4,
 >                     
 >                     //Flag options for ForeignNews MsgBox
 >                     ForeignDefeat = 0x8,
 >                     ForeignVictory = 0x16,
 >                     ForeignDeclaredWar = 0x32,
 >                     ForeignSurrendered = 0x64,
 >                     
 >                     //Flag options for HomeNews MsgBox
 >                     
 >                 }
 >                 
 >         public MessageBoxFlags msgTest;

This is used in OnGUI() to fire various windows depending on the enum value. One sets the window type and content through bitwise operations. Here I'm declaring the GUILayout.Window and its associated function:

 >  if((msgTest & MessageBoxFlags.ForeignNews) != 0)        {
 >             Time.timeScale = 0.001f;
 >             GUILayout.Window(0,windowRect,
 >                   news_fBox,
 >                   "Our Foreign Minister Reports:");
 >                                                         }

The problem is within the function "news_fBox". It simply acts as if all the IF statements are true and dumps one big window with everything in it. Here's news_fBox:

             void news_fBox(int windowID)
 {
     Debug.LogWarning("msgTest value is: "+msgTest.ToString());
     if((msgTest & MessageBoxFlags.ForeignDefeat) != 0)
     {
     GUILayout.Label("Following intense fights, "+countryActedUpon+" has 
                              capitulated to "+countryInstigator+".");
         GUILayout.BeginHorizontal();
         if(GUILayout.Button("At least we're still alive."))
         {
             Time.timeScale = 1.0f; 
             msgTest = 0x0;
         }
         GUILayout.EndHorizontal();
     }

     if((msgTest & MessageBoxFlags.ForeignVictory) != 0)
     {
         GUILayout.Label("The brave people of "+countryInstigator+" have 
                                      defeated vile "+countryActedUpon+".\n May 
                                      their treacherous schemes never plague our 
                                      nations.");
         GUILayout.BeginHorizontal();
         if(GUILayout.Button("Serves them well."))
         {
             Time.timeScale = 1.0f; 
             msgTest = 0x0;
         }
         GUILayout.EndHorizontal();
     }
     
     if((msgTest & MessageBoxFlags.ForeignDeclaredWar) != 0)
     {
         GUILayout.Label(countryInstigator+" has declared war on 
                                     "+countryActedUpon+".");
         GUILayout.BeginHorizontal();
         if(GUILayout.Button("Ok"))
         {
             Time.timeScale = 1.0f; 
             msgTest = 0x0;
         }
         GUILayout.EndHorizontal();
     }
     
     if((msgTest & MessageBoxFlags.ForeignSurrendered) != 0)
     {
         GUILayout.Label("With morale shattered, having enough of all 
                                      the blood shed"+countryActedUpon+" has decided 
                                      to surrender to the "+countryInstigator);
         GUILayout.BeginHorizontal();
         if(GUILayout.Button("Fate help them all."))
         {
             Time.timeScale = 1.0f; 
             msgTest = 0x0;
         }
         GUILayout.EndHorizontal();
     }
 }

What happens is that, when sending the following value "Announcements.MessageBoxFlags.ForeignNews|Announcements.MessageBoxFlags.ForeignDeclaredWar" to msgTest, everything within news_fBox is dumped on the screen, regardless of the IF statement checks.

Also, when sending msgText.ToString() to the log, I only get "ForeignDeclaredWar" without "ForeignNews".

Am I using the GUI methods wrong or is there something wrong with my enum? Hm... for some reason I can't add anything related to GUI/UI as a tag because I lack the neccessary reputation. You'd think there's more than one person with a UI problem out there.

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
2
Best Answer

Answer by rutter · Mar 04, 2014 at 03:04 AM

Your bit flags don't appear to be set up correctly.

For example:

 ForeignVictory = 0x16,

This gives 00010110, which matches several of the bitmasks you provided.

If you're trying to mask individual bits, the left shift operator is usually simpler:

 1 << 0 //(1st bit)
 1 << 1 //(2nd bit)
 1 << 2 //(3rd bit)
 1 << 3 //(4th bit)
 1 << 4 //(5th bit)
 
Comment
Add comment · Show 1 · 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
avatar image BogdanV · Mar 04, 2014 at 02:51 PM 0
Share

Oh, damn I'm stupid. Hex numbers go from 0-9 and A-F, no wonder I failed miserably. Your solution is much more elegant though so I'll mark it as an answer :)

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

If Check for serialized flag enums in Editor scripting 1 Answer

Use Enum of another script in an if statement C# 2 Answers

Editorscript: Generate enum from string[] 4 Answers

Optimising if else statements to switch and enums, or are there better methods? 1 Answer

Using an enum inside of a scriptable Object - why don't I see it in the inspector? 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