Files
ichni_Creator_Studio/Assets/Scripts/DynamicUI/Drawers/EnumDropdownDrawer.cs
SoulliesOfficial 5fc1392747 新Head
2026-06-09 01:43:55 -04:00

50 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using Ichni.RhythmGame;
using Lean.Pool;
using UnityEngine;
namespace Ichni.Editor
{
/// <summary>
/// 处理所有 Enum 类型的 Drawer。
/// 通过反射获取字段的枚举类型,创建 DynamicUIEnumDropdown。
/// </summary>
public class EnumDropdownDrawer : IPropertyDrawer
{
public int DefaultSpan => 1;
public float DefaultHeight => LayoutPacker.DefaultRowHeight;
public DynamicUIElement Draw(DrawContext ctx)
{
var go = LeanPool.Spawn(ctx.Registry.GetPrefab("enumDropdown"), ctx.Parent);
var element = go.GetComponent<DynamicUIEnumDropdown>();
// 获取枚举类型:优先使用 DrawContext 中的 EnumTypefallback 到反射
Type enumType = ctx.EnumType;
if (enumType == null && ctx.Target != null && !string.IsNullOrEmpty(ctx.FieldName))
{
var field = ctx.Target.GetType().GetField(ctx.FieldName);
if (field != null) enumType = field.FieldType;
else
{
var prop = ctx.Target.GetType().GetProperty(ctx.FieldName);
if (prop != null) enumType = prop.PropertyType;
}
}
if (enumType != null)
{
element.SetUpEnum(enumType);
}
element.Initialize(ctx.Target, ctx.Label, ctx.FieldName);
int span = ctx.OverrideSpan ?? DefaultSpan;
float height = ctx.OverrideHeight ?? DefaultHeight;
element.SetLayoutSize(span * LayoutPacker.TotalRowWidth / LayoutPacker.MaxSpanPerRow, height);
return element;
}
}
}