Files
ichni_Creator_Studio/Assets/Scripts/DynamicUI/DynamicUIElements/Simple/DynamicUIToggle.cs
SoulliesOfficial e97646ae48 修复
2026-06-09 07:00:19 -04:00

67 lines
2.2 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)
{
// [对象池安全] 重置可交互状态
if (toggle != null) toggle.interactable = true;
// [对象池安全] 精准解绑业务代理,不动预制体原生的展示事件!
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));
}
public override DynamicUIElement AddListenerFunction(UnityAction action)
{
customAction += _ => action();
return this;
}
}
}