From 7e848c8b3c60abe70aff23db620f74457a82d0d1 Mon Sep 17 00:00:00 2001 From: AnnulusGames Date: Sat, 17 Feb 2024 13:33:31 +0900 Subject: [PATCH] Add: AlchemyEditorUtility --- .../Alchemy/Editor/AlchemyEditorUtility.cs | 25 +++++++++++++++++++ .../Editor/AlchemyEditorUtility.cs.meta | 11 ++++++++ .../Alchemy/Editor/AlchemyGroupDrawer.cs | 7 +++++- .../Editor/Elements/AlchemyPropertyField.cs | 5 +--- .../Editor/Internal/InspectorHelper.cs | 8 +----- 5 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 Alchemy/Assets/Alchemy/Editor/AlchemyEditorUtility.cs create mode 100644 Alchemy/Assets/Alchemy/Editor/AlchemyEditorUtility.cs.meta diff --git a/Alchemy/Assets/Alchemy/Editor/AlchemyEditorUtility.cs b/Alchemy/Assets/Alchemy/Editor/AlchemyEditorUtility.cs new file mode 100644 index 0000000..f4c3df4 --- /dev/null +++ b/Alchemy/Assets/Alchemy/Editor/AlchemyEditorUtility.cs @@ -0,0 +1,25 @@ +using System; +using System.Linq; +using System.Reflection; +using UnityEditor; +using Alchemy.Inspector; + +namespace Alchemy.Editor +{ + public static class AlchemyEditorUtility + { + public static Type FindGroupDrawerType(PropertyGroupAttribute attribute) + { + return TypeCache.GetTypesWithAttribute() + .FirstOrDefault(x => x.GetCustomAttribute().targetAttributeType == attribute.GetType()); + } + + internal static AlchemyGroupDrawer CreateGroupDrawer(PropertyGroupAttribute attribute, Type targetType) + { + var drawerType = FindGroupDrawerType(attribute); + var drawer = (AlchemyGroupDrawer)Activator.CreateInstance(drawerType); + drawer.SetUniqueId("AlchemyGroupId_" + targetType.FullName + "_" + attribute.GroupPath); + return drawer; + } + } +} \ No newline at end of file diff --git a/Alchemy/Assets/Alchemy/Editor/AlchemyEditorUtility.cs.meta b/Alchemy/Assets/Alchemy/Editor/AlchemyEditorUtility.cs.meta new file mode 100644 index 0000000..3205440 --- /dev/null +++ b/Alchemy/Assets/Alchemy/Editor/AlchemyEditorUtility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 890788e0ea34e47319ee5f653021c4dd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Alchemy/Assets/Alchemy/Editor/AlchemyGroupDrawer.cs b/Alchemy/Assets/Alchemy/Editor/AlchemyGroupDrawer.cs index 54fb0bc..6fd6d49 100644 --- a/Alchemy/Assets/Alchemy/Editor/AlchemyGroupDrawer.cs +++ b/Alchemy/Assets/Alchemy/Editor/AlchemyGroupDrawer.cs @@ -9,6 +9,11 @@ namespace Alchemy.Editor public virtual VisualElement GetGroupElement(Attribute attribute) => null; public string UniqueId => _uniqueId; - internal string _uniqueId; + string _uniqueId; + + internal void SetUniqueId(string id) + { + this._uniqueId = id; + } } } \ No newline at end of file diff --git a/Alchemy/Assets/Alchemy/Editor/Elements/AlchemyPropertyField.cs b/Alchemy/Assets/Alchemy/Editor/Elements/AlchemyPropertyField.cs index c928042..a954d10 100644 --- a/Alchemy/Assets/Alchemy/Editor/Elements/AlchemyPropertyField.cs +++ b/Alchemy/Assets/Alchemy/Editor/Elements/AlchemyPropertyField.cs @@ -46,10 +46,7 @@ namespace Alchemy.Editor.Elements } else if (targetType.TryGetCustomAttribute(out var groupAttribute)) // custom group { - var drawerType = TypeCache.GetTypesWithAttribute() - .FirstOrDefault(x => x.GetCustomAttribute().targetAttributeType == groupAttribute.GetType()); - var drawer = (AlchemyGroupDrawer)Activator.CreateInstance(drawerType); - drawer._uniqueId = "AlchemyGroupId_" + targetType.FullName + "_" + property.propertyPath; + var drawer = AlchemyEditorUtility.CreateGroupDrawer(groupAttribute, targetType); var root = drawer.CreateRootElement(labelText); InspectorHelper.BuildElements(property.serializedObject, root, property.GetValue(), name => property.FindPropertyRelative(name), depth + 1); diff --git a/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs b/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs index dd74037..fe32dbc 100644 --- a/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs +++ b/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs @@ -182,13 +182,7 @@ namespace Alchemy.Editor var next = parentNode.Find(x => x.Name == groupName); if (next == null) { - // Find drawer type - var drawerType = TypeCache.GetTypesWithAttribute() - .FirstOrDefault(x => x.GetCustomAttribute().targetAttributeType == groupAttribute.GetType()); - - var drawer = (AlchemyGroupDrawer)Activator.CreateInstance(drawerType); - drawer._uniqueId = "AlchemyGroupId_" + targetType.FullName + "_" + groupAttribute.GroupPath; - + var drawer = AlchemyEditorUtility.CreateGroupDrawer(groupAttribute, targetType); next = new GroupNode(groupName, drawer); parentNode.Add(next); }