EnumFlagPropertyDrawer
From Unify Community Wiki
(Difference between revisions)
ChemiKhazi (Talk | contribs) |
(usage explanation) |
||
Line 3: | Line 3: | ||
Unity3d property drawer for automatically making enums flags into mask fields in the inspector. | Unity3d property drawer for automatically making enums flags into mask fields in the inspector. | ||
− | Usage: Put the .cs | + | Usage: Put the second .cs file in an Editor folder, and put the second somewhere *other* than the Editor folder. When you have an enum field you want to turn into a mask field in the inspector, add the EnumFlag attribute over the field. Eg: |
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
Line 13: | Line 13: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | [https:// | + | The enum declaration needs to have bit flag values assigned to it, as shown in [https://docs.unity3d.com/ScriptReference/EditorGUILayout.EnumFlagsField.html this example code]. Then use the HasFlag() method on the enum value. |
'''EnumFlagAttribute.cs''' | '''EnumFlagAttribute.cs''' | ||
Line 51: | Line 51: | ||
EditorGUI.BeginProperty(position, label, property); | EditorGUI.BeginProperty(position, label, property); | ||
− | Enum enumNew = EditorGUI. | + | Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum); |
property.intValue = (int) Convert.ChangeType(enumNew, targetEnum.GetType()); | property.intValue = (int) Convert.ChangeType(enumNew, targetEnum.GetType()); | ||
EditorGUI.EndProperty(); | EditorGUI.EndProperty(); |
Latest revision as of 21:57, 28 May 2018
Unity3d property drawer for automatically making enums flags into mask fields in the inspector.
Usage: Put the second .cs file in an Editor folder, and put the second somewhere *other* than the Editor folder. When you have an enum field you want to turn into a mask field in the inspector, add the EnumFlag attribute over the field. Eg:
[EnumFlag] MyCustomEnum thisEnum; // This lets you give the field a custom name in the inspector. [EnumFlag("Custom Inspector Name")] MyCustomEnum anotherEnum;
The enum declaration needs to have bit flag values assigned to it, as shown in this example code. Then use the HasFlag() method on the enum value.
EnumFlagAttribute.cs
using UnityEngine; public class EnumFlagAttribute : PropertyAttribute { public string enumName; public EnumFlagAttribute() {} public EnumFlagAttribute(string name) { enumName = name; } }
EnumFlagDrawer.cs
using System; using System.Reflection; using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(EnumFlagAttribute))] public class EnumFlagDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute; Enum targetEnum = GetBaseProperty<Enum>(property); string propName = flagSettings.enumName; if (string.IsNullOrEmpty(propName)) propName = property.name; EditorGUI.BeginProperty(position, label, property); Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum); property.intValue = (int) Convert.ChangeType(enumNew, targetEnum.GetType()); EditorGUI.EndProperty(); } static T GetBaseProperty<T>(SerializedProperty prop) { // Separate the steps it takes to get to this property string[] separatedPaths = prop.propertyPath.Split('.'); // Go down to the root of this serialized property System.Object reflectionTarget = prop.serializedObject.targetObject as object; // Walk down the path to get the target object foreach (var path in separatedPaths) { FieldInfo fieldInfo = reflectionTarget.GetType().GetField(path); reflectionTarget = fieldInfo.GetValue(reflectionTarget); } return (T) reflectionTarget; } }