Add: class group support

This commit is contained in:
AnnulusGames 2024-02-17 13:08:41 +09:00
parent 993a47e00d
commit 1a645e2a81
5 changed files with 31 additions and 2 deletions

View File

@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
@ -42,6 +44,18 @@ namespace Alchemy.Editor.Elements
{
element = new PropertyListView(property, depth);
}
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 root = drawer.CreateRootElement(labelText);
InspectorHelper.BuildElements(property.serializedObject, root, property.GetValue<object>(), name => property.FindPropertyRelative(name), depth + 1);
if (root is BindableElement bindableElement) bindableElement.BindProperty(property);
element = root;
}
else
{
var foldout = new Foldout() { text = labelText };

View File

@ -157,7 +157,8 @@ namespace Alchemy.Editor
var rootNode = new GroupNode("Inspector-Group-Root", null);
// Get all members
var members = ReflectionHelper.GetMembers(targetType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, true);
var members = ReflectionHelper.GetMembers(targetType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, true)
.Where(x => x is MethodInfo or FieldInfo or PropertyInfo);
// Build member nodes
foreach (var member in members)
@ -232,7 +233,7 @@ namespace Alchemy.Editor
}
#if ALCHEMY_SUPPORT_SERIALIZATION
if (serializedObject.targetObject != null &&
if (serializedObject.targetObject != null &&
serializedObject.targetObject.GetType().HasCustomAttribute<AlchemySerializeAttribute>() &&
memberInfo.HasCustomAttribute<AlchemySerializeFieldAttribute>())
{

View File

@ -9,5 +9,11 @@ namespace Alchemy.Editor
{
return memberInfo.GetCustomAttribute<T>() != null;
}
public static bool TryGetCustomAttribute<T>(this MemberInfo memberInfo, out T result) where T : Attribute
{
result = memberInfo.GetCustomAttribute<T>();
return result != null;
}
}
}

View File

@ -2,11 +2,13 @@ namespace Alchemy.Inspector
{
public sealed class GroupAttribute : PropertyGroupAttribute
{
public GroupAttribute() : base() { }
public GroupAttribute(string groupPath) : base(groupPath) { }
}
public sealed class BoxGroupAttribute : PropertyGroupAttribute
{
public BoxGroupAttribute() : base() { }
public BoxGroupAttribute(string groupPath) : base(groupPath) { }
}
@ -22,6 +24,7 @@ namespace Alchemy.Inspector
public sealed class FoldoutGroupAttribute : PropertyGroupAttribute
{
public FoldoutGroupAttribute() : base() { }
public FoldoutGroupAttribute(string groupPath) : base(groupPath) { }
}

View File

@ -7,6 +7,11 @@ namespace Alchemy.Inspector
/// </summary>
public abstract class PropertyGroupAttribute : Attribute
{
public PropertyGroupAttribute()
{
GroupPath = string.Empty;
}
public PropertyGroupAttribute(string groupPath)
{
GroupPath = groupPath;