• 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 "t$$anonymous$$s 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
avatar image Em3rgency · Jul 04, 2013 at 03:14 PM -1
Share
avatar image DocteurCox · Jul 04, 2013 at 03:29 PM 0
Share

7 Replies

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

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

T$$anonymous$$s bugs me too but after some quick experimentation I t$$anonymous$$nk you can do it pretty trivially using the property drawer stuff. The main t$$anonymous$$ng I noticed is to be aware of how Unity treats "Everyt$$anonymous$$ng" as t$$anonymous$$s 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 t$$anonymous$$s then you could make it more robust and also decide whether you want everyt$$anonymous$$ng 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 t$$anonymous$$s 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
avatar image yoyo · Jul 07, 2014 at 05:55 PM 0
Share
avatar image phort99 · Jun 23, 2016 at 12:52 AM 0
Share
avatar image laurentlavigne · Jul 01, 2016 at 05:14 AM 0
Share
avatar image v01pe_ · Apr 20, 2021 at 09:01 AM 0
Share
avatar image
12

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

I've also had t$$anonymous$$s 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
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 t$$anonymous$$s, just tag the enum definition as [System.Serializable, System.Flags] and unity will make the inspector ui work like a LayerMask. Makes everyt$$anonymous$$ng on t$$anonymous$$s page irrelevant. (using 2019.3 but i'm sure it has been there for a w$$anonymous$$le)

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
avatar image v01pe_ · Apr 20, 2021 at 08:56 AM 0
Share
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 t$$anonymous$$s one case. Never mind! :-) ))

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

For example, in one of my projects, I defined a bit flag enum like t$$anonymous$$s:

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

Then I have a component similar to t$$anonymous$$s:

 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
avatar image TonyLi · Jul 05, 2013 at 02:33 AM 0
Share
avatar image fafase · Nov 15, 2013 at 02:10 PM 0
Share
avatar image Ash-Blue · Dec 29, 2016 at 04:53 AM 0
Share
avatar image TonyLi Ash-Blue · Dec 29, 2016 at 01:40 PM 1
Share
avatar image
1

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

The answer posted by bunny83 solves t$$anonymous$$s 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Multiple Cars not working 1 Answer

DropDownList with string array in Editor Inspector 6 Answers

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