Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by DocteurCox · Jul 04, 2013 at 02:48 PM · c#editorenum

Default Editor : Enum as flags ?

Hello there !

I juste have a little question regarding Unity default editor with enums. By default, when you declare an enum, it will create an editor when you can choose the enum value but you'll only be able to pick one of those.

Is there a way like an special attribute to tell Unity "this is a flag, enable me to pick as many values as I want" without writing a custom editor ?

Thanks in advance for your answers :)

Comment
Add comment · Show 3
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 DocteurCox · Jul 04, 2013 at 03:05 PM 0
Share

Yes I guess I could do that, but I've always found bool inelegant for context like that. I'll wait to see if there's a better solution but I'll probably go for yours if there's nothing else to do. Thanks for your answer anyway :)

avatar image Em3rgency · Jul 04, 2013 at 03:14 PM -1
Share

Well, I mean, by definition, each enum variable can hold only 1 value. You could technically make several enums of the same type, but that's WAYYY messier than just using bools for the flags :)

avatar image DocteurCox · Jul 04, 2013 at 03:29 PM 0
Share

Well I never had any trouble using enum as flags with bitwise operator and I've always found that method more elegant. :) But it does not seem Unity provide a simple way to show it in editor unless you use $$anonymous$$askField (which requires a lot of things so there's no way Unity does that himself).

7 Replies

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

Answer by froodydude · Aug 13, 2013 at 11:59 AM

This bugs me too but after some quick experimentation I think you can do it pretty trivially using the property drawer stuff. The main thing I noticed is to be aware of how Unity treats "Everything" as this sets all bits. I suspect it will also run in to problems if you leave gaps in your flags. I couldn't find a way to get the actual System.Type from the SerializedProperty, if it were possible to get this then you could make it more robust and also decide whether you want everything to mean set all bits or not. Anyway the code:

 public class EnumFlagsAttribute : PropertyAttribute
 {
     public EnumFlagsAttribute() { }
 }
 
 [CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
 public class EnumFlagsAttributeDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect _position, SerializedProperty _property, GUIContent _label)
     {
         _property.intValue = EditorGUI.MaskField( _position, _label, _property.intValue, _property.enumNames );
     }
 }

To use this you do the following:

 [System.Flags]
 public enum MyMaskedEnum
 {
     Flag0 = (1 << 0),
     Flag1 = (1 << 1),
     Flag2 = (1 << 2),
     Flag3 = (1 << 3),
 }
 
 class MyObject : MonoBehaviour
 {
     [SerializeField] [EnumFlagsAttribute] MyMaskedEnum m_flags;
 }
Comment
Add comment · Show 5 · 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 s_guy · Oct 18, 2013 at 10:32 PM 2
Share

I like this! Probably worth mentioning that the attribute class must exist in a non-editor script and folder, and the property drawer class must exist in an editor script and folder. I had them in the same file as seems to be implied above. That tripped me up and it took some staring at the PropertyDrawer docs to figure it out.

For some reason the built-in "Everything" option is uncheckable for me. I use a custom definition for "All" in the enum, which is nice to have control over. I also like to define "None" in my enum as well as some combos that I use for mask testing, but don't intend for the field to be set to. Guess I'll mess with the editor UI a bit to see if I can suppress certain options.

UPDATE: found an alternative complete solution

http://answers.unity3d.com/questions/393992/custom-inspector-multi-select-enum-dropdown.html

avatar image yoyo · Jul 07, 2014 at 05:55 PM 0
Share

Note that this solution works if and only if your enum values are consecutive bits (1, 2, 4, 8, ...). If you have a 0 value ("None") or combined values, then EditorGUI.$$anonymous$$askField isn't sufficient.

For a complete solution, see the link mentioned in previous comment (http://answers.unity3d.com/questions/393992/custom-inspector-multi-select-enum-dropdown.html)

avatar image phort99 · Jun 23, 2016 at 12:52 AM 0
Share

Warning: The code in this answer isn't friendly to multiple-selection. As soon as you select two objects, the enum flags get overwritten without warning and with no way to undo. Here's my version of OnGUI that checks for changes before assigning. This is way safer.

     public override void OnGUI(Rect _position, UnityEditor.SerializedProperty _property, GUIContent _label)
     {
         // Change check is needed to prevent values being overwritten during multiple-selection
         UnityEditor.EditorGUI.BeginChangeCheck ();
         int newValue = UnityEditor.EditorGUI.$$anonymous$$askField( _position, _label, _property.intValue, _property.enumNames );
         if (UnityEditor.EditorGUI.EndChangeCheck ()) {
             _property.intValue = newValue;
         }
     }
 
avatar image laurentlavigne · Jul 01, 2016 at 05:14 AM 0
Share

Very elegant but doesn't handle multi selection. Add this right after OnGUI{

 EditorGUI.show$$anonymous$$ixedValue = _property.has$$anonymous$$ultipleDifferentValues;
avatar image v01pe_ · Apr 20, 2021 at 09:01 AM 0
Share

Thanks a ton, I used this in the past. Luckily since Unity some 2019 version, this is not needed any more, b/c Unity now does this by default!

avatar image
12

Answer by Democide · Feb 17, 2015 at 04:44 PM

I've also had this issue, and decided to build a different solution: Toggle Buttons.

alt text

It provides a better overview over the flags set but takes up more space in the Editor. You can check it out here:

http://www.sharkbombs.com/2015/02/17/unity-editor-enum-flags-as-toggle-buttons/

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 chilly-15 · Oct 21, 2016 at 03:40 AM 0
Share

I made a few modifications to $$anonymous$$artin's code (posted to comments of the link provided), which provide stacked buttons if you have a long list of flags in your enum.

![Stacked Toggle Buttons][1] [1]: /storage/temp/80586-screen-shot-2016-10-21-at-23229-pm.png

screen-shot-2016-10-21-at-23229-pm.png (15.4 kB)
avatar image
8

Answer by Cookie042 · Feb 16, 2020 at 09:14 PM

As of writing this, just tag the enum definition as [System.Serializable, System.Flags] and unity will make the inspector ui work like a LayerMask. Makes everything on this page irrelevant. (using 2019.3 but i'm sure it has been there for a while)

Comment
Add comment · Show 2 · 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 guneyozsan · Apr 07, 2020 at 01:06 PM 0
Share

Does not work in Unity 2018.4.

avatar image v01pe_ · Apr 20, 2021 at 08:56 AM 0
Share

Only has been a recent (and very welcome) addition to Unity default functionality.

avatar image
3

Answer by TonyLi · Jul 04, 2013 at 08:41 PM

(( Doh, sorry, I have to apologize. I thought it did what I described below, but I forgot that I had written a custom inspector just to handle this one case. Never mind! :-) ))

You just need to use [System.Flags]. Unity will pick this up and treat the enum drop-down as a mask.

For example, in one of my projects, I defined a bit flag enum like this:

 [System.Flags]
 public enum QuestStatus {
     Unassigned = 0x1,
     Active = 0x2,
     Success = 0x4,
     Failure = 0x8
 }

Then I have a component similar to this:

 public class Quest : MonoBehaviour {
     QuestStatus questStatus;
 }

And the default inspector looks like the attached screenshot: alt text


enummask.png (8.0 kB)
Comment
Add comment · Show 5 · 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 Em3rgency · Jul 04, 2013 at 08:49 PM 0
Share

Wait... Does this let you pick several at once?

avatar image TonyLi · Jul 05, 2013 at 02:33 AM 0
Share

Doh, sorry, I have to apologize. I thought it did, but I forgot that I had written a custom inspector just to handle this one case. Never $$anonymous$$d! :-)

avatar image fafase · Nov 15, 2013 at 02:10 PM 0
Share

Old thread but well, this does allow you to get several at once, then you need a quick bit manipulation to figure out what is on.

 QuestStatus _status = QuestStatus.Active | QuestStatus.Success;

then you check what is on with:

 if((_status & QuestStatus.Active)==QuestStatus.Active)
     print("Active");
 else
     print("No active");
avatar image Ash-Blue · Dec 29, 2016 at 04:53 AM 0
Share

Please note that although this solution looks magical, it doesn't appear to be working in Unity 5.5+

avatar image TonyLi Ash-Blue · Dec 29, 2016 at 01:40 PM 1
Share

bunny83's answer below is a good solution. I think at the time I posted my response Unity Answers there wasn't a good way to retract/delete posts. I had forgotten that I'd written a custom inspector similar to bunny83's answer.

avatar image
1

Answer by s_guy · Oct 19, 2013 at 02:05 AM

The answer posted by bunny83 solves this fully.

http://answers.unity3d.com/questions/393992/custom-inspector-multi-select-enum-dropdown.html

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
  • 1
  • 2
  • ›

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

DropDownList with string array in Editor Inspector 5 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Assigned enum value changes when a new type is added to the enum 2 Answers


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