55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Ichni.RhythmGame;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Ichni.Editor
|
|
{
|
|
public class DynamicUIButton : DynamicUIElement
|
|
{
|
|
public Button button;
|
|
public TMP_Text buttonText;
|
|
public void SetText(string buttonText)
|
|
{
|
|
this.buttonText.text = buttonText;
|
|
}
|
|
|
|
private UnityAction customAction;
|
|
private UnityAction appliedFunction;
|
|
|
|
public void ApplyFunction(UnityAction function)
|
|
{
|
|
// [对象池安全] 改为只清除我们自己挂载的动作,避免杀掉预制体内置的动效
|
|
if (appliedFunction != null) button.onClick.RemoveListener(appliedFunction);
|
|
button.onClick.RemoveListener(OnButtonClick);
|
|
|
|
customAction = null;
|
|
appliedFunction = function;
|
|
|
|
button.onClick.AddListener(appliedFunction);
|
|
button.onClick.AddListener(OnButtonClick);
|
|
}
|
|
|
|
private void OnButtonClick()
|
|
{
|
|
if (connectedBaseElement != null) connectedBaseElement.Refresh();
|
|
customAction?.Invoke();
|
|
}
|
|
|
|
public override void Initialize(IBaseElement baseElement, string title, string parameterName)
|
|
{
|
|
// 精确地仅重置 Button 的可交互状态(为了解决从对象池取回被禁用的废弃按钮问题)
|
|
if (button != null) button.interactable = true;
|
|
base.Initialize(baseElement, title, parameterName);
|
|
}
|
|
|
|
public override DynamicUIElement AddListenerFunction(UnityAction action)
|
|
{
|
|
customAction += action;
|
|
return this;
|
|
}
|
|
}
|
|
} |