优化ui绑定侧逻辑

This commit is contained in:
2026-07-25 23:45:29 +08:00
parent 3fbb82f40e
commit 251b8730b9
19 changed files with 782 additions and 436 deletions
@@ -4,20 +4,17 @@ using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 颜色值组件 — 处理 Color 类型的显示
/// 通过 Image.color 展示当前颜色值
/// 颜色修改可通过 RGB 子节点 InputField 或额外的颜色选择逻辑完成
/// Color 值组件 — 通过 Image 显示颜色,可选点击弹出颜色选择器
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDColorValue")]
public class XUIAvrgMDColorValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("用于展示颜色的 Image 组件(未设置则自动查找)")]
private Image _colorDisplay;
[SerializeField, Tooltip("关联的 Image 组件(未设置则自动查找)")]
private Image _colorImage;
private void Awake()
{
if (_colorDisplay == null) _colorDisplay = GetComponent<Image>();
if (_colorImage == null) _colorImage = GetComponent<Image>();
}
public override void BindToAccessor(
@@ -30,29 +27,23 @@ namespace XericUI.ReflectionUIGenerator
if (binding == null || accessor == null) return;
// 数据 → UI
binding.OnUIDataChanged += value =>
binding.OnUIDataChanged += (value, state) =>
{
if (value is Color color && _colorDisplay != null)
{
_colorDisplay.color = color;
}
UpdateValueDisplay(value, state);
};
binding.OnValidationStateChanged += OnValidationStateChanged;
// 首次同步
var initialValue = accessor.GetValue();
if (initialValue is Color initColor && _colorDisplay != null)
{
_colorDisplay.color = initColor;
}
UpdateValueDisplay(initialValue, ValidationState.Normal);
}
protected override void UpdateDisplayValue(object value)
protected override void UpdateValueDisplay(object value, ValidationState state)
{
if (value is Color color && _colorDisplay != null)
{
_colorDisplay.color = color;
}
// 值显示:刷新 Image.color
if (_colorImage != null && value is Color c)
_colorImage.color = c;
base.UpdateValueDisplay(value, state);
}
}
}
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
@@ -20,6 +20,8 @@ namespace XericUI.ReflectionUIGenerator
private Dropdown _dropdown;
#endif
private List<string> _options;
private void Awake()
{
#if dUI_TextMeshPro
@@ -29,6 +31,24 @@ namespace XericUI.ReflectionUIGenerator
#endif
}
// ============ 对象池生命周期 ============
public override void OnPoolGet(AvrgMemberMetaData metaData)
{
base.OnPoolGet(metaData);
_options = BuildOptions(metaData?.ValueType);
}
public override void OnPoolRelease()
{
if (_dropdown != null)
_dropdown.onValueChanged.RemoveAllListeners();
_options = null;
base.OnPoolRelease();
}
// ============ 核心绑定 ============
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
@@ -39,66 +59,71 @@ namespace XericUI.ReflectionUIGenerator
if (binding == null || accessor == null || _dropdown == null) return;
// 构建选项列表
var options = BuildOptions(accessor.ValueType);
// 填充选项
_dropdown.ClearOptions();
_dropdown.AddOptions(options);
_dropdown.AddOptions(_options);
// UI → 数据
_dropdown.onValueChanged.AddListener(index =>
{
if (index >= 0 && index < options.Count)
if (index >= 0 && index < _options.Count)
{
object convertedVal = ConvertOptionToValue(options[index], accessor.ValueType);
object convertedVal = ConvertOptionToValue(_options[index], accessor.ValueType);
binding.WriteFromUI(convertedVal);
scheduler?.MarkUIDirty(binding);
}
});
// 数据 → UI
binding.OnUIDataChanged += value =>
// 数据 → UI(带验证状态)
binding.OnUIDataChanged += (value, state) =>
{
int idx = FindOptionIndex(value, options);
int idx = FindOptionIndex(value);
if (idx >= 0)
_dropdown.SetValueWithoutNotify(idx);
base.UpdateValueDisplay(value, state);
};
// 验证状态变更
binding.OnValidationStateChanged += OnValidationStateChanged;
// 首次同步
var initialValue = accessor.GetValue();
int initialIdx = FindOptionIndex(initialValue, options);
int initialIdx = FindOptionIndex(initialValue);
if (initialIdx >= 0)
_dropdown.SetValueWithoutNotify(initialIdx);
UpdateValueDisplay(initialValue, ValidationState.Normal);
}
private List<string> BuildOptions(System.Type valueType)
private List<string> BuildOptions(Type valueType)
{
if (BoundMetaData?.PresetOptions != null && BoundMetaData.PresetOptions.Count > 0)
return BoundMetaData.PresetOptions.ToList();
if (valueType != null && valueType.IsEnum)
return System.Enum.GetNames(valueType).ToList();
return Enum.GetNames(valueType).ToList();
return new List<string>();
}
private int FindOptionIndex(object value, List<string> options)
private int FindOptionIndex(object value)
{
if (value == null || options.Count == 0) return -1;
if (value == null || _options == null || _options.Count == 0) return -1;
string valueStr = value.ToString();
for (int i = 0; i < options.Count; i++)
for (int i = 0; i < _options.Count; i++)
{
if (options[i] == valueStr) return i;
if (_options[i] == valueStr) return i;
}
return -1;
}
private static object ConvertOptionToValue(string option, System.Type targetType)
private static object ConvertOptionToValue(string option, Type targetType)
{
if (targetType == typeof(string)) return option;
if (targetType == typeof(int) && int.TryParse(option, out int intVal)) return intVal;
if (targetType != null && targetType.IsEnum)
return System.Enum.Parse(targetType, option);
return Enum.Parse(targetType, option);
return option;
}
}
@@ -1,12 +1,11 @@
using System;
using UnityEngine;
using UnityEngine.UI;
using XericLibrary.Runtime.SuperStyleSheet;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// InputField 值组件 — 处理 string / float / int 类型的文本输入交互
/// InputField 值组件 — 处理 string / float / int / double 类型的文本输入交互
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDInputFieldValue")]
public class XUIAvrgMDInputFieldValue : XUIAvrgMDValue
@@ -26,10 +25,7 @@ namespace XericUI.ReflectionUIGenerator
[SerializeField, Tooltip("减少按钮(点击按步进值 -)")]
private Button _decrementButton;
// ============ 样式表参数 ============
private readonly StyleMember _styleStepValue = new() { StylePath = "xuiavrg/input/stepValue" };
// ============ 生命周期 ============
private const float DefaultStep = 1f;
private void Awake()
{
@@ -40,16 +36,18 @@ namespace XericUI.ReflectionUIGenerator
#endif
}
protected override void OnEnable()
{
base.OnEnable();
_styleStepValue.Register();
}
// ============ 对象池生命周期 ============
protected override void OnDisable()
public override void OnPoolRelease()
{
base.OnDisable();
_styleStepValue.Unregister();
if (_incrementButton != null)
_incrementButton.onClick.RemoveAllListeners();
if (_decrementButton != null)
_decrementButton.onClick.RemoveAllListeners();
if (_inputField != null)
_inputField.onEndEdit.RemoveAllListeners();
base.OnPoolRelease();
}
// ============ 核心绑定 ============
@@ -64,6 +62,7 @@ namespace XericUI.ReflectionUIGenerator
if (binding == null || accessor == null || _inputField == null) return;
// 根据类型设置输入模式
#if dUI_TextMeshPro
if (accessor.ValueType == typeof(float) || accessor.ValueType == typeof(int)
|| accessor.ValueType == typeof(double))
@@ -92,33 +91,29 @@ namespace XericUI.ReflectionUIGenerator
scheduler?.MarkUIDirty(binding);
});
// 数据 → UI
binding.OnUIDataChanged += value =>
// 数据 → UI(带验证状态)
binding.OnUIDataChanged += (value, state) =>
{
_inputField.SetTextWithoutNotify(accessor.FormatDisplay(value));
base.UpdateValueDisplay(value, state);
};
// 验证状态变更
binding.OnValidationStateChanged += OnValidationStateChanged;
// 首次同步
var initialValue = accessor.GetValue();
_inputField.SetTextWithoutNotify(accessor.FormatDisplay(initialValue));
RefreshCommonNodes();
UpdateValueDisplay(initialValue, ValidationState.Normal);
}
// ============ 步进 ============
private float GetStepValue()
{
var val = GetStyleValue(_styleStepValue);
return val?.GetValueAsFloat() ?? 1f;
}
private void OnIncrementClicked()
{
if (CurrentAccessor?.IsReadOnly == true || _inputField == null) return;
if (!float.TryParse(_inputField.text, out float current)) return;
float step = GetStepValue();
float newVal = current + step;
float newVal = current + DefaultStep;
_inputField.text = CurrentAccessor.FormatDisplay(
ConvertValueForAccessor(newVal, CurrentAccessor.ValueType));
}
@@ -127,12 +122,32 @@ namespace XericUI.ReflectionUIGenerator
{
if (CurrentAccessor?.IsReadOnly == true || _inputField == null) return;
if (!float.TryParse(_inputField.text, out float current)) return;
float step = GetStepValue();
float newVal = current - step;
float newVal = current - DefaultStep;
_inputField.text = CurrentAccessor.FormatDisplay(
ConvertValueForAccessor(newVal, CurrentAccessor.ValueType));
}
// ============ 验证状态回调 ============
protected override void OnValidationStateChanged(ValidationState state)
{
base.OnValidationStateChanged(state);
// InputField 文字颜色随验证状态变化(通过 Text 组件)
if (_inputField != null)
{
var textComponent = _inputField.textComponent;
if (textComponent != null)
{
textComponent.color = state == ValidationState.Invalid
? Color.red
: state == ValidationState.Warning
? new Color(1f, 0.92f, 0.016f)
: Color.white;
}
}
}
private static object ConvertValueForAccessor(float val, Type targetType)
{
if (targetType == typeof(int)) return (int)val;
@@ -1,32 +1,19 @@
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 标签值组件 — 处理只读文本的纯展示
/// 适用于 Label / Context / Unit / ReadOnly 等展示型 Tag
/// Label 只读值组件 — 纯文本展示,无交互控件
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDLabelValue")]
public class XUIAvrgMDLabelValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("关联的 Text 组件(未设置则自动查找)")]
private UnityEngine.UI.Text _uiText;
#if dUI_TextMeshPro
[SerializeField]
[Tooltip("关联的 TMP_Text 组件(未设置则自动查找)")]
[SerializeField, Tooltip("关联的 TMP_Text 组件(标签文本 + 值文本共用一个 Text 时使用)")]
private TMPro.TMP_Text _tmpText;
#endif
private void Awake()
{
if (_uiText == null) _uiText = GetComponent<UnityEngine.UI.Text>();
#if dUI_TextMeshPro
if (_tmpText == null) _tmpText = GetComponent<TMPro.TMP_Text>();
#endif
}
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
@@ -37,31 +24,14 @@ namespace XericUI.ReflectionUIGenerator
if (binding == null || accessor == null) return;
// 数据 → UI
binding.OnUIDataChanged += value =>
binding.OnUIDataChanged += (value, state) =>
{
SetText(value);
UpdateValueDisplay(value, state);
};
binding.OnValidationStateChanged += OnValidationStateChanged;
// 首次同步
var initialValue = accessor.GetValue();
SetText(initialValue);
}
private void SetText(object value)
{
string displayStr = value?.ToString() ?? "";
#if dUI_TextMeshPro
if (_tmpText != null)
{
_tmpText.text = displayStr;
return;
}
#endif
if (_uiText != null)
_uiText.text = displayStr;
UpdateValueDisplay(initialValue, ValidationState.Normal);
}
}
}
@@ -1,19 +1,17 @@
using System;
using UnityEngine;
using UnityEngine.UI;
using XericLibrary.Runtime.SuperStyleSheet;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// Slider 值组件 — 处理 float / int / double 类型的滑块交互
/// 适用于 Shader Range 属性或标记了 [XuiavrgFieldTag(Slider)] 的字段
/// 适用 Shader Range 属性或标记了 [XuiavrgFieldTag(Slider)] 的字段
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDSliderValue")]
public class XUIAvrgMDSliderValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("关联的 Slider 组件(未设置则自动查找)")]
[SerializeField, Tooltip("关联的 Slider 组件(未设置则自动查找)")]
private Slider _slider;
[Header("Slider 专属插槽")]
@@ -24,37 +22,44 @@ namespace XericUI.ReflectionUIGenerator
private Button _decrementButton;
#if dUI_TextMeshPro
[SerializeField]
[Tooltip("值显示标签(可选,未设置则不更新)")]
[SerializeField, Tooltip("值显示标签(可选,未设置则不更新)")]
private TMPro.TMP_Text _valueLabel;
#endif
// ============ 样式表参数 ============
private readonly StyleMember _styleDefaultStep = new() { StylePath = "xuiavrg/slider/defaultStep" };
private readonly StyleMember _styleMinStep = new() { StylePath = "xuiavrg/slider/minStep" };
private readonly StyleMember _styleMaxStep = new() { StylePath = "xuiavrg/slider/maxStep" };
// ============ 生命周期 ============
private const float DefaultStep = 0.1f;
private void Awake()
{
if (_slider == null) _slider = GetComponent<Slider>();
}
protected override void OnEnable()
// ============ 对象池生命周期 ============
public override void OnPoolGet(AvrgMemberMetaData metaData)
{
base.OnEnable();
_styleDefaultStep.Register();
_styleMinStep.Register();
_styleMaxStep.Register();
base.OnPoolGet(metaData);
if (_slider == null) return;
// 从元数据初始化 Slider 参数
if (metaData?.RangeMin.HasValue == true)
_slider.minValue = metaData.RangeMin.Value;
if (metaData?.RangeMax.HasValue == true)
_slider.maxValue = metaData.RangeMax.Value;
_slider.wholeNumbers = metaData?.ValueType == typeof(int);
}
protected override void OnDisable()
public override void OnPoolRelease()
{
base.OnDisable();
_styleDefaultStep.Unregister();
_styleMinStep.Unregister();
_styleMaxStep.Unregister();
// 清除按钮监听
if (_incrementButton != null)
_incrementButton.onClick.RemoveAllListeners();
if (_decrementButton != null)
_decrementButton.onClick.RemoveAllListeners();
if (_slider != null)
_slider.onValueChanged.RemoveAllListeners();
base.OnPoolRelease();
}
// ============ 核心绑定 ============
@@ -69,17 +74,6 @@ namespace XericUI.ReflectionUIGenerator
if (binding == null || accessor == null || _slider == null) return;
// 设置 Slider 参数
_slider.wholeNumbers = accessor.ValueType == typeof(int);
if (BoundMetaData != null)
{
if (BoundMetaData.RangeMin.HasValue)
_slider.minValue = BoundMetaData.RangeMin.Value;
if (BoundMetaData.RangeMax.HasValue)
_slider.maxValue = BoundMetaData.RangeMax.Value;
}
// 增减按钮
if (_incrementButton != null)
_incrementButton.onClick.AddListener(OnIncrementClicked);
@@ -99,8 +93,8 @@ namespace XericUI.ReflectionUIGenerator
#endif
});
// 数据 → UI
binding.OnUIDataChanged += value =>
// 数据 → UI(带验证状态)
binding.OnUIDataChanged += (value, state) =>
{
float sliderVal = Convert.ToSingle(value);
_slider.SetValueWithoutNotify(sliderVal);
@@ -109,8 +103,14 @@ namespace XericUI.ReflectionUIGenerator
if (_valueLabel != null)
_valueLabel.text = accessor.FormatDisplay(value);
#endif
// 委托基类处理标签合成 + 值文本
base.UpdateValueDisplay(value, state);
};
// 验证状态变更
binding.OnValidationStateChanged += OnValidationStateChanged;
// 首次同步
var initialValue = accessor.GetValue();
if (initialValue != null)
@@ -121,23 +121,14 @@ namespace XericUI.ReflectionUIGenerator
_valueLabel.text = accessor.FormatDisplay(initialValue);
#endif
}
RefreshCommonNodes();
UpdateValueDisplay(initialValue ?? 0f, ValidationState.Normal);
}
// ============ 步进值 ============
private float GetStepValue()
{
var val = GetStyleValue(_styleDefaultStep);
if (val != null)
{
float step = val.GetValueAsFloat();
float min = GetStyleValue(_styleMinStep)?.GetValueAsFloat() ?? 0.001f;
float max = GetStyleValue(_styleMaxStep)?.GetValueAsFloat() ?? 1000f;
return Mathf.Clamp(step, min, max);
}
return _slider.wholeNumbers ? 1f : 0.1f;
return _slider.wholeNumbers ? 1f : DefaultStep;
}
private void OnIncrementClicked()
@@ -158,6 +149,28 @@ namespace XericUI.ReflectionUIGenerator
_slider.value = newVal;
}
// ============ 验证状态回调 ============
protected override void OnValidationStateChanged(ValidationState state)
{
base.OnValidationStateChanged(state);
// Slider 填充色随验证状态变化
if (_slider != null)
{
var fillArea = _slider.fillRect;
if (fillArea != null && fillArea.TryGetComponent<Image>(out var fill))
{
fill.color = state switch
{
ValidationState.Invalid => Color.red,
ValidationState.Warning => new Color(1f, 0.92f, 0.016f),
_ => Color.white
};
}
}
}
private static object ConvertValueForAccessor(float val, Type targetType)
{
if (targetType == typeof(int)) return (int)val;
@@ -4,25 +4,22 @@ using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 纹理值组件 — 处理 Texture 类型的预览显示
/// 通过 RawImage.texture 展示当前纹理
/// Texture 值组件 — 通过 RawImage 显示纹理
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDTextureValue")]
public class XUIAvrgMDTextureValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("用于展示纹理的 RawImage 组件(未设置则自动查找)")]
private RawImage _textureDisplay;
[SerializeField, Tooltip("关联的 RawImage 组件(未设置则自动查找)")]
private RawImage _rawImage;
#if dUI_TextMeshPro
[SerializeField]
[Tooltip("纹理名称标签(可选,用于显示纹理资源名)")]
[SerializeField, Tooltip("纹理名称标签(可选)")]
private TMPro.TMP_Text _nameLabel;
#endif
private void Awake()
{
if (_textureDisplay == null) _textureDisplay = GetComponent<RawImage>();
if (_rawImage == null) _rawImage = GetComponent<RawImage>();
}
public override void BindToAccessor(
@@ -35,35 +32,29 @@ namespace XericUI.ReflectionUIGenerator
if (binding == null || accessor == null) return;
// 数据 → UI
binding.OnUIDataChanged += value =>
binding.OnUIDataChanged += (value, state) =>
{
if (value is Texture tex && _textureDisplay != null)
{
_textureDisplay.texture = tex;
}
UpdateValueDisplay(value, state);
};
binding.OnValidationStateChanged += OnValidationStateChanged;
var initialValue = accessor.GetValue();
UpdateValueDisplay(initialValue, ValidationState.Normal);
}
protected override void UpdateValueDisplay(object value, ValidationState state)
{
// 值显示:刷新 RawImage.texture
if (_rawImage != null && value is Texture tex)
{
_rawImage.texture = tex;
#if dUI_TextMeshPro
if (_nameLabel != null)
{
_nameLabel.text = value is Texture t && t != null ? t.name : "None";
}
_nameLabel.text = tex?.name ?? "";
#endif
};
// 首次同步
var initialValue = accessor.GetValue();
if (initialValue is Texture initTex && _textureDisplay != null)
{
_textureDisplay.texture = initTex;
}
#if dUI_TextMeshPro
if (_nameLabel != null)
{
_nameLabel.text = initialValue is Texture t && t != null ? t.name : "None";
}
#endif
base.UpdateValueDisplay(value, state);
}
}
}
@@ -56,8 +56,8 @@ namespace XericUI.ReflectionUIGenerator
});
}
// 数据 → UI
binding.OnUIDataChanged += value =>
// 数据 → UI(带验证状态)
binding.OnUIDataChanged += (value, state) =>
{
string valueStr = value?.ToString();
for (int i = 0; i < _toggles.Count && i < _optionLabels.Count; i++)
@@ -65,11 +65,16 @@ namespace XericUI.ReflectionUIGenerator
if (_optionLabels[i] == valueStr)
{
_toggles[i].SetIsOnWithoutNotify(true);
return;
break;
}
}
base.UpdateValueDisplay(value, state);
};
// 验证状态变更
binding.OnValidationStateChanged += OnValidationStateChanged;
// 首次同步
var initialValue = accessor.GetValue();
string initStr = initialValue?.ToString();
@@ -1,43 +1,29 @@
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
{
[SerializeField]
[Tooltip("关联的 Toggle 组件(未设置则自动查找)")]
[SerializeField, 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()
public override void OnPoolRelease()
{
base.OnDisable();
_styleOnColor.Unregister();
_styleOffColor.Unregister();
if (_toggle != null)
_toggle.onValueChanged.RemoveAllListeners();
base.OnPoolRelease();
}
// ============ 核心绑定 ============
@@ -60,36 +46,53 @@ namespace XericUI.ReflectionUIGenerator
{
binding.WriteFromUI(val);
scheduler?.MarkUIDirty(binding);
ApplyToggleColor(val);
});
// 数据 → UI
binding.OnUIDataChanged += value =>
// 数据 → UI(带验证状态)
binding.OnUIDataChanged += (value, state) =>
{
bool isOn = value is bool b && b;
_toggle.SetIsOnWithoutNotify(isOn);
ApplyToggleColor(isOn);
ApplyToggleColor(isOn, state);
base.UpdateValueDisplay(value, state);
};
// 验证状态变更
binding.OnValidationStateChanged += OnValidationStateChanged;
// 首次同步
var initialValue = accessor.GetValue();
bool initialOn = initialValue is bool bv && bv;
_toggle.SetIsOnWithoutNotify(initialOn);
ApplyToggleColor(initialOn);
RefreshCommonNodes();
ApplyToggleColor(initialOn, ValidationState.Normal);
UpdateValueDisplay(initialValue, ValidationState.Normal);
}
// ============ 样式颜色 ============
// ============ 验证状态回调 ============
private void ApplyToggleColor(bool isOn)
protected override void OnValidationStateChanged(ValidationState state)
{
var target = _toggle.targetGraphic;
base.OnValidationStateChanged(state);
// Switch 验证状态也影响 targetGraphic 颜色
if (_toggle != null)
{
ApplyToggleColor(_toggle.isOn, state);
}
}
private void ApplyToggleColor(bool isOn, ValidationState state)
{
var target = _toggle?.targetGraphic;
if (target == null) return;
var colorVal = isOn ? GetStyleValue(_styleOnColor) : GetStyleValue(_styleOffColor);
if (colorVal != null)
target.color = colorVal;
if (state == ValidationState.Invalid)
target.color = Color.red;
else if (state == ValidationState.Warning)
target.color = new Color(1f, 0.92f, 0.016f);
else
target.color = isOn ? Color.green : new Color(0.5f, 0.5f, 0.5f, 1f);
}
}
}
+136 -111
View File
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using XericLibrary.Runtime.SuperStyleSheet;
namespace XericUI.ReflectionUIGenerator
{
@@ -9,11 +9,11 @@ namespace XericUI.ReflectionUIGenerator
///
/// 职责:
/// 1. 提供 EntryTag/TagIndex 供 XuiFieldTempEntry 匹配
/// 2. 持有绑定的元数据引用
/// 3. 通过 virtual BindToAccessor() 让子类自行管理 UI 交互事件和显示更新
/// 4. 初始化时广播元数据到所有子节点上的 IXUIAvrgMDValueStyle
/// 2. 持有绑定的元数据引用,管理验证状态
/// 3. 通过 virtual BindToAccessor() 让子类自行管理 UI 交互事件
/// 4. 对象池生命周期:OnPoolGet(meta) 初始化、OnPoolRelease() 重置
/// 5. 定义绑定槽(BindingSlots),供拆分属性子绑定匹配
/// 6. 管理通用 UI 插槽(标签、警告、只读、单位、值显示)+ SuperStyleSheet 样式读取
/// 6. 通过 UpdateValueDisplay() 合成标签文本(名称 + 只读 + 警告标记)
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/XUIAvrgMDValue")]
public class XUIAvrgMDValue : MonoBehaviour
@@ -47,29 +47,29 @@ namespace XericUI.ReflectionUIGenerator
[Tooltip("拆分属性子绑定槽列表 — 用于 XuiavrgSplit 的子属性匹配")]
private List<AvrgBindingSlot> _bindingSlots = new List<AvrgBindingSlot>();
// ============ 通用 UI 插槽 ============
// ============ UI 文本插槽 ============
[Header("通用 UI 插槽")]
[SerializeField, Tooltip("字段标签节点 — 显示字段名称 + 只读前缀。为 null 则不显示标签行")]
protected GameObject fieldLabelNode;
[Header("UI 文本插槽")]
[SerializeField, Tooltip("字段标签文本 — 显示 \"名称 (只读) ⚠警告\",为 null 则不显示标签")]
#if dUI_TextMeshPro
protected TMPro.TMP_Text _fieldLabelText;
#else
protected UnityEngine.UI.Text _fieldLabelText;
#endif
[SerializeField, Tooltip("警告样式节点 — 验证失败时 SetActive(true),颜色由样式表控制")]
protected GameObject warningNode;
[SerializeField, Tooltip("值显示文本 — 可为 null(Slider/Color/Texture 子类不依赖文本)")]
#if dUI_TextMeshPro
protected TMPro.TMP_Text _valueDisplayText;
#else
protected UnityEngine.UI.Text _valueDisplayText;
#endif
[SerializeField, Tooltip("只读标记节点 — IsReadOnly 时 SetActive(true)")]
protected GameObject readOnlyNode;
[SerializeField, Tooltip("单位文本节点 — 显示元数据的 UnitLabelName")]
protected GameObject unitTextNode;
[SerializeField, Tooltip("值显示文本节点 — 可为 null(子类如 Slider/Color 不依赖文本)")]
protected GameObject valueTextNode;
// ============ 样式表参数(路径硬编码,命名空间从 BoundMetaData.StyleNamespace 获取)============
private readonly StyleMember _styleStepValue = new() { StylePath = "xuiavrg/base/stepValue" };
private readonly StyleMember _styleReadOnlyPrefix = new() { StylePath = "xuiavrg/base/readOnlyPrefix" };
private readonly StyleMember _styleWarningColor = new() { StylePath = "xuiavrg/base/warningColor" };
[SerializeField, Tooltip("单位文本 — 可为 null")]
#if dUI_TextMeshPro
protected TMPro.TMP_Text _unitText;
#else
protected UnityEngine.UI.Text _unitText;
#endif
// ============ 公共属性 ============
@@ -82,26 +82,44 @@ namespace XericUI.ReflectionUIGenerator
/// <summary>当前关联的 MVVM 绑定</summary>
public AvrgMvvmBinding CurrentBinding { get; protected set; }
/// <summary>当前验证状态</summary>
public ValidationState CurrentValidationState { get; protected set; } = ValidationState.Normal;
public XuiavrgFieldTag EntryTag => _entryTag;
public int TagIndex => _tagIndex;
/// <summary>只读访问绑定槽列表</summary>
public IReadOnlyList<AvrgBindingSlot> AvailableBindingSlots => _bindingSlots;
// ============ 生命周期 ============
// ============ 对象池生命周期 ============
protected virtual void OnEnable()
/// <summary>
/// 从对象池取出时调用 — 注入元数据并初始化子属性
/// 子类可覆写以初始化专属组件参数(Slider.minValue/maxValue 等)
/// </summary>
public virtual void OnPoolGet(AvrgMemberMetaData metaData)
{
_styleStepValue.Register();
_styleReadOnlyPrefix.Register();
_styleWarningColor.Register();
BoundMetaData = metaData;
CurrentValidationState = ValidationState.Normal;
// 子类覆写点
}
protected virtual void OnDisable()
/// <summary>
/// 回收到对象池时调用 — 重置所有状态到初始值
/// 子类可覆写以清理专属组件事件
/// </summary>
public virtual void OnPoolRelease()
{
_styleStepValue.Unregister();
_styleReadOnlyPrefix.Unregister();
_styleWarningColor.Unregister();
CurrentAccessor = null;
CurrentBinding = null;
BoundMetaData = null;
CurrentValidationState = ValidationState.Normal;
// 重置 UI 文本
if (_fieldLabelText != null) _fieldLabelText.text = "";
if (_valueDisplayText != null) _valueDisplayText.text = "";
if (_unitText != null) _unitText.text = "";
}
// ============ 核心绑定接口 ============
@@ -119,44 +137,107 @@ namespace XericUI.ReflectionUIGenerator
if (binding == null || accessor == null) return;
// 默认:数据 → UI 单向同步
binding.OnUIDataChanged += value =>
// 数据 → UI(带验证状态)
binding.OnUIDataChanged += (value, state) =>
{
UpdateDisplayValue(value);
UpdateValueDisplay(value, state);
};
// 验证状态变更
binding.OnValidationStateChanged += OnValidationStateChanged;
// 首次同步
var initialValue = accessor.GetValue();
UpdateDisplayValue(initialValue);
UpdateValueDisplay(initialValue, ValidationState.Normal);
}
// 刷新通用插槽
RefreshCommonNodes();
// ============ UI 更新 ============
/// <summary>
/// 根据值和验证状态更新 UI 显示
/// 子类可覆写以处理专属组件更新(Slider.value, Image.color 等)
/// 基类负责合成标签文本、值文本颜色、单位文本
/// </summary>
protected virtual void UpdateValueDisplay(object value, ValidationState state)
{
// 1. 字段标签合成
if (_fieldLabelText != null)
{
string label = BuildLabel(state);
_fieldLabelText.text = label;
}
// 2. 值显示
if (_valueDisplayText != null)
{
string display = CurrentAccessor?.FormatDisplay(value) ?? value?.ToString() ?? "";
_valueDisplayText.text = display;
_valueDisplayText.color = state == ValidationState.Invalid
? Color.red
: state == ValidationState.Warning
? new Color(1f, 0.92f, 0.016f) // yellow
: Color.white;
}
// 3. 单位文本
if (_unitText != null)
_unitText.text = BoundMetaData?.UnitLabelName ?? "";
}
/// <summary>
/// 更新 UI 显示值(基类默认仅查找 Text/TMP_Text 设置文本)
/// 子类可覆写以实现专属更新逻辑(如 Slider.value, Image.color 等)
/// 合成标签文本:名称 + (只读) + 警告标记
/// </summary>
protected virtual void UpdateDisplayValue(object value)
protected string BuildLabel(ValidationState state)
{
if (value == null) return;
var meta = BoundMetaData;
var sb = new StringBuilder();
sb.Append(meta?.LabelName ?? meta?.MemberName ?? "");
#if dUI_TextMeshPro
var tmpText = GetComponent<TMPro.TMP_Text>();
if (tmpText != null)
{
tmpText.text = CurrentAccessor?.FormatDisplay(value) ?? value.ToString();
return;
}
#endif
// 必填标记
if (meta != null && meta.Required)
sb.Append(" *");
var uiText = GetComponent<UnityEngine.UI.Text>();
if (uiText != null)
// 只读标记
if (CurrentAccessor?.IsReadOnly == true)
sb.Append(" (只读)");
// 警告/无效标记(用富文本颜色)
if (state == ValidationState.Warning)
sb.Append(" <color=yellow>⚠警告</color>");
else if (state == ValidationState.Invalid)
sb.Append(" <color=red>⚠无效值</color>");
return sb.ToString();
}
// ============ 验证状态回调 ============
/// <summary>
/// 验证状态变更回调 — 子类可覆写以实现专属样式切换
/// 如 Slider 填充色变黄/红、InputField 文字变色等
/// </summary>
protected virtual void OnValidationStateChanged(ValidationState state)
{
CurrentValidationState = state;
// 刷新标签文本(警告/无效标记在标签上)
if (_fieldLabelText != null)
_fieldLabelText.text = BuildLabel(state);
// 值文本颜色
if (_valueDisplayText != null)
{
uiText.text = CurrentAccessor?.FormatDisplay(value) ?? value.ToString();
_valueDisplayText.color = state == ValidationState.Invalid
? Color.red
: state == ValidationState.Warning
? new Color(1f, 0.92f, 0.016f)
: Color.white;
}
}
// ============ 元数据广播 ============
/// <summary>
/// 由绑定系统调用,传递元数据并广播到 IXUIAvrgMDValueStyle 子组件
/// </summary>
@@ -166,7 +247,6 @@ namespace XericUI.ReflectionUIGenerator
if (metaData == null) return;
// 广播元数据到子节点样式组件
var styles = GetComponentsInChildren<IXUIAvrgMDValueStyle>(true);
foreach (var style in styles)
{
@@ -176,60 +256,5 @@ namespace XericUI.ReflectionUIGenerator
style.ApplyMetaData(metaData);
}
}
// ============ 样式读取 ============
/// <summary>
/// 从样式表获取值(自动使用 BoundMetaData.StyleNamespace)
/// </summary>
protected StyleValue GetStyleValue(StyleMember member)
{
if (member == null) return null;
string ns = BoundMetaData?.StyleNamespace ?? "default";
return StyleManager.GetValue(ns, member.StylePath);
}
// ============ 通用 UI 插槽刷新 ============
/// <summary>刷新所有通用 UI 插槽(标签、单位、只读、警告标记)</summary>
protected void RefreshCommonNodes()
{
var meta = BoundMetaData;
if (meta == null) return;
// 1. 字段标签节点
if (fieldLabelNode != null)
{
string prefix = "";
if (CurrentAccessor?.IsReadOnly == true)
{
var pv = GetStyleValue(_styleReadOnlyPrefix);
prefix = pv?.GetValueAsString() ?? "[只读] ";
}
SetNodeText(fieldLabelNode, prefix + (meta.LabelName ?? meta.MemberName));
}
// 2. 单位文本节点
if (unitTextNode != null)
SetNodeText(unitTextNode, meta.UnitLabelName ?? "");
// 3. 只读标记节点
if (readOnlyNode != null)
readOnlyNode.SetActive(CurrentAccessor?.IsReadOnly == true);
// 4. warningNode 由外部验证系统触发,此方法不直接控制
}
/// <summary>在节点及其子节点上查找 Text / TMP_Text 并设置文本</summary>
protected static void SetNodeText(GameObject node, string text)
{
if (node == null) return;
#if dUI_TextMeshPro
var t = node.GetComponentInChildren<TMPro.TMP_Text>(true);
if (t != null) { t.text = text; return; }
#endif
var ut = node.GetComponentInChildren<UnityEngine.UI.Text>(true);
if (ut != null) ut.text = text;
}
}
}
@@ -1,6 +1,5 @@
using UnityEngine;
using UnityEngine.UI;
using XericLibrary.Runtime.SuperStyleSheet;
namespace XericUI.ReflectionUIGenerator
{
@@ -11,37 +10,39 @@ namespace XericUI.ReflectionUIGenerator
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDVectorValue")]
public class XUIAvrgMDVectorValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("X 分量 InputField(可选,也可通过 BindingSlots 的子节点绑定)")]
[SerializeField, Tooltip("X 分量 InputField(可选,也可通过 BindingSlots 的子节点绑定)")]
private InputField _inputX;
[SerializeField]
[Tooltip("Y 分量 InputField")]
[SerializeField, Tooltip("Y 分量 InputField")]
private InputField _inputY;
[SerializeField]
[Tooltip("Z 分量 InputField(Vector3/Vector4 时使用)")]
[SerializeField, Tooltip("Z 分量 InputField(Vector3/Vector4 时使用)")]
private InputField _inputZ;
[SerializeField]
[Tooltip("W 分量 InputField(Vector4 时使用)")]
[SerializeField, Tooltip("W 分量 InputField(Vector4 时使用)")]
private InputField _inputW;
// ============ 样式表参数 ============
private readonly StyleMember _styleLayoutDirection = new() { StylePath = "xuiavrg/vector/layoutDirection" };
// ============ 对象池生命周期 ============
// ============ 生命周期 ============
protected override void OnEnable()
public override void OnPoolGet(AvrgMemberMetaData metaData)
{
base.OnEnable();
_styleLayoutDirection.Register();
base.OnPoolGet(metaData);
int componentCount = GetComponentCount(metaData?.ValueType);
SetInputActive(_inputX, 0, componentCount);
SetInputActive(_inputY, 1, componentCount);
SetInputActive(_inputZ, 2, componentCount);
SetInputActive(_inputW, 3, componentCount);
}
protected override void OnDisable()
public override void OnPoolRelease()
{
base.OnDisable();
_styleLayoutDirection.Unregister();
if (_inputX != null) _inputX.onEndEdit.RemoveAllListeners();
if (_inputY != null) _inputY.onEndEdit.RemoveAllListeners();
if (_inputZ != null) _inputZ.onEndEdit.RemoveAllListeners();
if (_inputW != null) _inputW.onEndEdit.RemoveAllListeners();
base.OnPoolRelease();
}
// ============ 核心绑定 ============
@@ -56,18 +57,6 @@ namespace XericUI.ReflectionUIGenerator
if (binding == null || accessor == null) return;
// 根据向量类型启用/禁用对应分量
int componentCount = GetComponentCount(accessor.ValueType);
SetInputActive(_inputX, 0, componentCount);
SetInputActive(_inputY, 1, componentCount);
SetInputActive(_inputZ, 2, componentCount);
SetInputActive(_inputW, 3, componentCount);
// 样式表控制排列方向
var dirVal = GetStyleValue(_styleLayoutDirection);
string dir = dirVal?.GetValueAsString() ?? "Horizontal";
ApplyLayoutDirection(dir);
// 为每个输入注册事件
BindInputField(_inputX, 0, accessor, binding, scheduler);
BindInputField(_inputY, 1, accessor, binding, scheduler);
@@ -75,41 +64,18 @@ namespace XericUI.ReflectionUIGenerator
BindInputField(_inputW, 3, accessor, binding, scheduler);
// 数据 → UI
binding.OnUIDataChanged += value =>
binding.OnUIDataChanged += (value, state) =>
{
UpdateAllInputs(value);
base.UpdateValueDisplay(value, state);
};
binding.OnValidationStateChanged += OnValidationStateChanged;
// 首次同步
var initialValue = accessor.GetValue();
UpdateAllInputs(initialValue);
RefreshCommonNodes();
}
// ============ 布局 ============
private void ApplyLayoutDirection(string dir)
{
var layout = GetComponent<HorizontalOrVerticalLayoutGroup>();
if (layout == null) return;
if (dir == "Vertical")
{
if (layout is HorizontalLayoutGroup)
{
Destroy(layout);
gameObject.AddComponent<VerticalLayoutGroup>();
}
}
else
{
if (layout is VerticalLayoutGroup)
{
Destroy(layout);
gameObject.AddComponent<HorizontalLayoutGroup>();
}
}
UpdateValueDisplay(initialValue, ValidationState.Normal);
}
// ============ 输入绑定 ============