65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Ichni.RhythmGame;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Ichni.Editor
|
|
{
|
|
public class DynamicUIToggle : DynamicUIElement
|
|
{
|
|
public Toggle toggle;
|
|
|
|
private UnityAction<bool> customAction;
|
|
|
|
public override void Initialize(IBaseElement baseElement, string title, string parameterName)
|
|
{
|
|
// [对象池安全] 精准解绑业务代理,不动预制体原生的展示事件!
|
|
toggle.onValueChanged.RemoveListener(OnToggleValueChanged);
|
|
customAction = null;
|
|
|
|
base.Initialize(baseElement, title, parameterName);
|
|
if (parameterName != string.Empty)
|
|
{
|
|
var val = ReflectionHelper.GetDeepValue(connectedBaseElement, parameterName);
|
|
if (val != null)
|
|
{
|
|
toggle.isOn = (bool)val;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[DynamicUI] 尝试绑定 {title} ({parameterName}) 失败,由于其值或路径无效。");
|
|
toggle.isOn = false;
|
|
}
|
|
toggle.onValueChanged.AddListener(OnToggleValueChanged);
|
|
}
|
|
else
|
|
{
|
|
toggle.isOn = false;
|
|
toggle.onValueChanged.AddListener(OnToggleValueChanged);
|
|
}
|
|
}
|
|
|
|
private void OnToggleValueChanged(bool value)
|
|
{
|
|
if (parameterName != string.Empty)
|
|
{
|
|
ApplyParameters(value);
|
|
}
|
|
customAction?.Invoke(value);
|
|
}
|
|
|
|
private void ApplyParameters(bool value)
|
|
{
|
|
Ichni.Editor.Commands.CommandManager.ExecuteCommand(new Ichni.Editor.Commands.ChangeValueCommand(connectedBaseElement, parameterName, value));
|
|
connectedBaseElement.Refresh();
|
|
}
|
|
|
|
public override DynamicUIElement AddListenerFunction(UnityAction action)
|
|
{
|
|
customAction += _ => action();
|
|
return this;
|
|
}
|
|
}
|
|
} |