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 hog · Aug 12, 2012 at 12:27 AM · inspectorenum

How can i show enum description in Inspector?

I wrote a script which has a public enum variable. When i attach this script to a gameobject, as a component, the Unity Editor would change enum variable into a dropdown list in Inspector automatically. My enum type is like this:

 enum MyEnum { 
  [Description("Description of option 1")] Option1, 
  [Description("Description of option 2")] Option2 
 }

The problem is the dropdown list in Inspertor just show me "Option1" and "Option2",the description fields are ignored. How can i make Inspertor to show my description as a choice name in a dropdown list instead of show enum item directly, so that i can make my script more user-friendly for the other guys.

Thanks

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

2 Replies

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

Answer by Bunny83 · Aug 12, 2012 at 12:39 AM

The only way is to create a custom inspector for your class and use reflection to read the attributes. I actually never saw attributes on enum members, so i'm not sure if that's even possible. It's much easier to seperate the helptext from the enum:

     enum MyEnum
     { 
         Option1, 
         Option2 
     }
     

You can simply use a EditorGUI.Popup / EditorGUILayout.Popup to display the dropdown.

     public MyEnum myValue;

In your custom inspector:

     private static string[] m_MyEnumDescriptions = new string[]
     {
         "Description of option 1",
         "Description of option 2"
     };
     
     // In OnInspectorGUI
     myValue = (MyEnum)EditorGUILayout.Popup((int)myValue, m_MyEnumDescriptions);

This of course assumes that you use a linear enum starting at 0 like the one you showed in your example.

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 hog · Aug 12, 2012 at 03:50 AM 0
Share

Thank you, that is just what i want to know, to override a default Inspector behavior. It is possible to add description in enum type, it's under System.Component$$anonymous$$odel, and can pass through $$anonymous$$ono compiler. Seems Unity Inspector just didn't implement it yet.

avatar image Bunny83 · Aug 12, 2012 at 12:36 PM 0
Share

Attributes are optional data information that can be added to classes, fields, methods. I'm not sure if it's even possible to add them to enum members as well. Anyway attributes don't really force a certain behaviour. $$anonymous$$ostly they are used by the compiler or serializer to get additional meta data about a class / member.

You can also create a new custom attribute and attach it to a field, but it always depends on the interpreting system if and how this data is used.

Again, attributes are just additional data without any behaviour.

If it's possible to add attributes to enum-members you can read them normally via reflection in your custom inspector. $$anonymous$$aybe i find some time for some tests on that topic ;)

avatar image
0

Answer by Bluk · Aug 12, 2012 at 12:38 AM

 enum MyEnum { 
  Option1, 
  Option2 
 }
 
 public var test:MyEnum;
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 Bunny83 · Aug 12, 2012 at 12:40 AM 0
Share

Uhmm should that be an answer? I don't get how that should answer his question since that's what's already in the question...

Also your code is written in UnityScript (Unity's javascript) while the code in question is clearly C# ;)

avatar image hog · Aug 12, 2012 at 03:35 AM 0
Share

Actually it is C#. Thanks anyway

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

9 People are following this question.

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

Related Questions

c# enum wont show in inspector 3 Answers

Insert enum breaking existing inspector values 2 Answers

Restrict which types of an Enum can be selected in inspector. 5 Answers

Flagged Enum representation in Inspector 1 Answer

Instantiate GameObject on Enum Selection from 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