- Home /
Custom PropertyDrawer to support the Flag is not change value.
public class EnumFlagAttribute : PropertyAttribute { public string enumName; public System.Type enumType;
public EnumFlagAttribute() {}
public EnumFlagAttribute(string name)
{
enumName = name;
}
public EnumFlagAttribute(System.Type type)
{
enumType = type;
}
}
[CustomPropertyDrawer(typeof(EnumFlagAttribute))] public class EnumFlagDrawer : PropertyDrawer
{ public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute; System.Enum targetEnum = GetBaseProperty (property);
string propName = flagSettings.enumName;
if (string.IsNullOrEmpty(propName))
propName = property.name;
EditorGUI.BeginProperty(position, label, property);
System.Enum enumNew = EditorGUI.EnumMaskField(position, propName, targetEnum);
property.intValue = (int) System.Convert.ChangeType(enumNew, targetEnum.GetType());
EditorGUI.EndProperty();
}
static T GetBaseProperty<T>(SerializedProperty prop)
{
string[] separatedPaths = prop.propertyPath.Split('.');
System.Object reflectionTarget = prop.serializedObject.targetObject as object;
foreach (var path in separatedPaths)
{
FieldInfo fieldInfo = reflectionTarget.GetType().GetField(path);
reflectionTarget = fieldInfo.GetValue(reflectionTarget);
}
return (T) reflectionTarget;
}
}
[System.Flags] public enum TeamFlagType { A_TEAM = (1 << 0), B_TEAM = (1 << 1), C_TEAM = (1 << 2), D_TEAM = (1 << 3), E_TEAM = (1 << 4), F_TEAM = (1 << 5), G_TEAM = (1 << 6), H_TEAM = (1 << 7), I_TEAM = (1 << 8), J_TEAM = (1 << 9) }
[SerializeField, EnumFlag(typeof(TeamFlagType))] public TeamFlagType TeamFlag;
I'm using 5.3.7 now.
when I used 5.3.4, this PropertyDrawer worked fine. but PropertyDrawer is not change value after I version up to 5.3.6 and 5.3.7.
what should I do?
Your answer
Follow this Question
Related Questions
How can I display another enum? 1 Answer
Loop through values assigned to Enum var (using [Flags]) 0 Answers
Custom Editors : EnumFields, Proper Use Of 0 Answers
How to create a new SerializedProperty? 0 Answers
Custom editors, enums and arrays 0 Answers