- Home /
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.
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...
Your answer
Follow this Question
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