diff options
author | chai <215380520@qq.com> | 2024-05-23 10:08:29 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-05-23 10:08:29 +0800 |
commit | 8722a9920c1f6119bf6e769cba270e63097f8e25 (patch) | |
tree | 2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/PackageTools/Editor/EnumFlagDrawer.cs | |
parent | 3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff) |
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/PackageTools/Editor/EnumFlagDrawer.cs')
-rw-r--r-- | Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/PackageTools/Editor/EnumFlagDrawer.cs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/PackageTools/Editor/EnumFlagDrawer.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/PackageTools/Editor/EnumFlagDrawer.cs new file mode 100644 index 0000000..8e14970 --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/PackageTools/Editor/EnumFlagDrawer.cs @@ -0,0 +1,39 @@ +using System; +using System.Reflection; +using UnityEditor; +using UnityEngine; + +namespace Pathfinding { + [CustomPropertyDrawer(typeof(EnumFlagAttribute))] + public class EnumFlagDrawer : PropertyDrawer { + public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) { + Enum targetEnum = GetBaseProperty<Enum>(property); + + EditorGUI.BeginProperty(position, label, property); + EditorGUI.BeginChangeCheck(); +#if UNITY_2017_3_OR_NEWER + Enum enumNew = EditorGUI.EnumFlagsField(position, label, targetEnum); +#else + Enum enumNew = EditorGUI.EnumMaskField(position, label, targetEnum); +#endif + if (EditorGUI.EndChangeCheck() || !property.hasMultipleDifferentValues) { + 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, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + reflectionTarget = fieldInfo.GetValue(reflectionTarget); + } + return (T)reflectionTarget; + } + } +} |