Add: AlchemyEditorUtility

This commit is contained in:
AnnulusGames 2024-02-17 13:33:31 +09:00
parent 1a645e2a81
commit 7e848c8b3c
5 changed files with 44 additions and 12 deletions

View File

@ -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<CustomGroupDrawerAttribute>()
.FirstOrDefault(x => x.GetCustomAttribute<CustomGroupDrawerAttribute>().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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 890788e0ea34e47319ee5f653021c4dd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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;
}
}
}

View File

@ -46,10 +46,7 @@ namespace Alchemy.Editor.Elements
}
else if (targetType.TryGetCustomAttribute<PropertyGroupAttribute>(out var groupAttribute)) // custom group
{
var drawerType = TypeCache.GetTypesWithAttribute<CustomGroupDrawerAttribute>()
.FirstOrDefault(x => x.GetCustomAttribute<CustomGroupDrawerAttribute>().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<object>(), name => property.FindPropertyRelative(name), depth + 1);

View File

@ -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<CustomGroupDrawerAttribute>()
.FirstOrDefault(x => x.GetCustomAttribute<CustomGroupDrawerAttribute>().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);
}