分离具体属性组件,加入样式表系统
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XericLibrary.Runtime.SuperStyleSheet;
|
||||
|
||||
namespace XericUI.ReflectionUIGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Toggle 值组件 — 处理 bool 类型的开关交互
|
||||
/// 支持样式表控制开关颜色
|
||||
/// </summary>
|
||||
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDToggleValue")]
|
||||
public class XUIAvrgMDToggleValue : XUIAvrgMDValue
|
||||
@@ -13,11 +15,33 @@ namespace XericUI.ReflectionUIGenerator
|
||||
[Tooltip("关联的 Toggle 组件(未设置则自动查找)")]
|
||||
private Toggle _toggle;
|
||||
|
||||
// ============ 样式表参数 ============
|
||||
private readonly StyleMember _styleOnColor = new() { StylePath = "xuiavrg/toggle/onColor" };
|
||||
private readonly StyleMember _styleOffColor = new() { StylePath = "xuiavrg/toggle/offColor" };
|
||||
|
||||
// ============ 生命周期 ============
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (_toggle == null) _toggle = GetComponent<Toggle>();
|
||||
}
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
_styleOnColor.Register();
|
||||
_styleOffColor.Register();
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
base.OnDisable();
|
||||
_styleOnColor.Unregister();
|
||||
_styleOffColor.Unregister();
|
||||
}
|
||||
|
||||
// ============ 核心绑定 ============
|
||||
|
||||
public override void BindToAccessor(
|
||||
IAvrgValueAccessor accessor,
|
||||
AvrgMvvmBinding binding,
|
||||
@@ -36,17 +60,36 @@ namespace XericUI.ReflectionUIGenerator
|
||||
{
|
||||
binding.WriteFromUI(val);
|
||||
scheduler?.MarkUIDirty(binding);
|
||||
ApplyToggleColor(val);
|
||||
});
|
||||
|
||||
// 数据 → UI
|
||||
binding.OnUIDataChanged += value =>
|
||||
{
|
||||
_toggle.SetIsOnWithoutNotify(value is bool b && b);
|
||||
bool isOn = value is bool b && b;
|
||||
_toggle.SetIsOnWithoutNotify(isOn);
|
||||
ApplyToggleColor(isOn);
|
||||
};
|
||||
|
||||
// 首次同步
|
||||
var initialValue = accessor.GetValue();
|
||||
_toggle.SetIsOnWithoutNotify(initialValue is bool bv && bv);
|
||||
bool initialOn = initialValue is bool bv && bv;
|
||||
_toggle.SetIsOnWithoutNotify(initialOn);
|
||||
ApplyToggleColor(initialOn);
|
||||
|
||||
RefreshCommonNodes();
|
||||
}
|
||||
|
||||
// ============ 样式颜色 ============
|
||||
|
||||
private void ApplyToggleColor(bool isOn)
|
||||
{
|
||||
var target = _toggle.targetGraphic;
|
||||
if (target == null) return;
|
||||
|
||||
var colorVal = isOn ? GetStyleValue(_styleOnColor) : GetStyleValue(_styleOffColor);
|
||||
if (colorVal != null)
|
||||
target.color = colorVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user