41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
|
|
using Lean.Pool;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.Events;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
namespace Ichni.Editor
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 按钮 Drawer,不绑定数据字段。
|
|||
|
|
/// 创建 DynamicUIButton 并绑定 ButtonAction。
|
|||
|
|
/// </summary>
|
|||
|
|
public class ButtonDrawer : IPropertyDrawer
|
|||
|
|
{
|
|||
|
|
public int DefaultSpan => 1;
|
|||
|
|
public float DefaultHeight => LayoutPacker.DefaultRowHeight;
|
|||
|
|
|
|||
|
|
public DynamicUIElement Draw(DrawContext ctx)
|
|||
|
|
{
|
|||
|
|
var go = LeanPool.Spawn(ctx.Registry.GetPrefab("button"), ctx.Parent);
|
|||
|
|
var element = go.GetComponent<DynamicUIButton>();
|
|||
|
|
|
|||
|
|
element.SetText(ctx.Label);
|
|||
|
|
element.Initialize(ctx.Target, ctx.Label, string.Empty);
|
|||
|
|
|
|||
|
|
if (ctx.ButtonAction != null)
|
|||
|
|
{
|
|||
|
|
UnityAction action = () => ctx.ButtonAction();
|
|||
|
|
element.ApplyFunction(action);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
DynamicUIElement.DisableNavigation(element.button);
|
|||
|
|
|
|||
|
|
int span = ctx.OverrideSpan ?? DefaultSpan;
|
|||
|
|
float height = ctx.OverrideHeight ?? DefaultHeight;
|
|||
|
|
element.SetLayoutSize(span * LayoutPacker.TotalRowWidth / LayoutPacker.MaxSpanPerRow, height);
|
|||
|
|
|
|||
|
|
return element;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|