• 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 arlenner22 · Apr 07, 2017 at 03:01 PM · enumcustom-inspectorpropertydrawerattributeenumpopup

problems writing an enum range attribute

So I was working on an attribute for enum popups that will force the enum popup to limit its displayed names to a specific set. Seems like it should work but it gets hung up (maybe because i am serializing another enum popup field in the same object) but here it is...

the attribute:

     [AttributeUsage(AttributeTargets.Field)]
     public class EnumRangeAttribute : PropertyAttribute
     {
         public readonly int Min;
         public readonly int Max;
 
         public EnumRangeAttribute(int min, int max)
         {
             Min = min;
             Max = max;
         }
     }

the propertydrawer:

     [CustomPropertyDrawer(typeof(EnumRangeAttribute))]
     public class EnumRangeAttributeDrawer : PropertyDrawer
     {
         EnumRangeAttribute att { get { return (EnumRangeAttribute)attribute; } }
 
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             int useInt = property.enumValueIndex;
             if(property.enumValueIndex < att.Min)
             {
                 useInt = att.Min;
             }
             if(property.enumValueIndex > att.Max)
             {
                 useInt = att.Max;
             }
             Enum num = (Keyword)useInt;
             num = EditorGUI.EnumPopup(position, label, (Keyword)property.enumValueIndex );
         }
     }
 

usage would be like this:

 public class MyClass : ScriptableObject
 {
        [EnumRange(x,y)]
         public MyEnum enumvalue;
 }

But I get this error -

The same field name is serialized multiple times in the class or its parent class. This is not supported: Base(ChangeIntValueOpData) Function

i think it's simply because of other enum popups in the class...suggestions?

Edit:

So this totally looks like its gonna work right? but it fails in the editor at line 23. Apparently theres some kind of internal conversion going on trying to cast it to the original type (enum Keyword) which, because its a filtered set of values, isn't happenin. Might have to extend the propertyhandler to solve this??

     [CustomPropertyDrawer(typeof(EnumRangeAttribute))]
     public class EnumRangeAttributeDrawer : PropertyDrawer
     {
         EnumRangeAttribute att { get { return (EnumRangeAttribute)attribute; } }
 
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             property.serializedObject.Update();
             var filteredEnum = (Enum)Enum.GetValues(typeof(Keyword))
                 .Cast<Enum>().Where(e => (Keyword)e >= (Keyword)att.Min && (Keyword)e <= (Keyword)att.Max);
             if (property.enumValueIndex > att.Max) property.enumValueIndex = att.Max;
             if (property.enumValueIndex < att.Min) property.enumValueIndex = att.Min;
             var filterIndex = EditorGUI.EnumPopup(position, label, filteredEnum);
             property.enumValueIndex = (int)(Keyword)filterIndex;
         }
 

Note: I did already submit my own answer, and technically it works, but i realized that an improvement would be to filter the displayed set of names...so now my question is really more about that.

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
0

Answer by arlenner22 · Apr 08, 2017 at 10:10 PM

Changed some things and this works:

     [CustomPropertyDrawer(typeof(EnumRangeAttribute))]
     public class EnumRangeAttributeDrawer : PropertyDrawer
     {
         EnumRangeAttribute att { get { return (EnumRangeAttribute)attribute; } }
 
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             property.serializedObject.Update();
             if (property.enumValueIndex > att.Max) property.enumValueIndex = att.Max;
             if (property.enumValueIndex < att.Min) property.enumValueIndex = att.Min;
             property.enumValueIndex = (int)(Keyword)(EditorGUI.EnumPopup(position, label, (Keyword)property.enumValueIndex));
         }
     }
 
 

...now to work on modifying the inspector to show only the selected range...

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

alternative for header atrribute for enums 1 Answer

EditorGUI.IntField always 0 after GameStart 0 Answers

Adding an attribute/type to a transform or object 0 Answers

Inspector - Show/hide property within custom class using enum 1 Answer

PropertyDrawer not updating in custom inspector. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges