Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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.

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

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

Showing an array with enum as keys in the property inspector 2 Answers

Why is my propertydrawer being automatically disabled? 1 Answer

Component OnAwake or OnEnable with PropertyAttribute 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