x


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

more ▼

asked Aug 12 '12 at 12:27 AM

hog gravatar image

hog
3 2 2 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Aug 12 '12 at 12:39 AM

Bunny83 gravatar image

Bunny83
45k 11 48 206

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.ComponentModel, and can pass through Mono compiler. Seems Unity Inspector just didn't implement it yet.

Aug 12 '12 at 03:50 AM hog

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. Mostly 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. Maybe i find some time for some tests on that topic ;)

Aug 12 '12 at 12:36 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
enum MyEnum { 
 Option1, 
 Option2 
}

public var test:MyEnum;
more ▼

answered Aug 12 '12 at 12:38 AM

Bluk gravatar image

Bluk
301 1 2 4

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# ;)

Aug 12 '12 at 12:40 AM Bunny83

Actually it is C#. Thanks anyway

Aug 12 '12 at 03:35 AM hog
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x463
x129
x3

asked: Aug 12 '12 at 12:27 AM

Seen: 1170 times

Last Updated: Aug 12 '12 at 12:36 PM