重构反射构建组件,添加材质反射组件

This commit is contained in:
2026-07-25 21:38:19 +08:00
parent f3672df6a7
commit b1636d5932
22 changed files with 1444 additions and 340 deletions
@@ -0,0 +1,58 @@
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 颜色值组件 — 处理 Color 类型的显示
/// 通过 Image.color 展示当前颜色值
/// 颜色修改可通过 RGB 子节点 InputField 或额外的颜色选择逻辑完成
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDColorValue")]
public class XUIAvrgMDColorValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("用于展示颜色的 Image 组件(未设置则自动查找)")]
private Image _colorDisplay;
private void Awake()
{
if (_colorDisplay == null) _colorDisplay = GetComponent<Image>();
}
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
AvrgMvvmScheduler scheduler)
{
CurrentAccessor = accessor;
CurrentBinding = binding;
if (binding == null || accessor == null) return;
// 数据 → UI
binding.OnUIDataChanged += value =>
{
if (value is Color color && _colorDisplay != null)
{
_colorDisplay.color = color;
}
};
// 首次同步
var initialValue = accessor.GetValue();
if (initialValue is Color initColor && _colorDisplay != null)
{
_colorDisplay.color = initColor;
}
}
protected override void UpdateDisplayValue(object value)
{
if (value is Color color && _colorDisplay != null)
{
_colorDisplay.color = color;
}
}
}
}
@@ -0,0 +1,100 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// Dropdown 值组件 — 处理 string / int / enum 类型的下拉选择交互
/// 预设选项来源:元数据的 PresetOptions 或 enum 类型的枚举名
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDDropdownValue")]
public class XUIAvrgMDDropdownValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("关联的 Dropdown 组件(未设置则自动查找)")]
private Dropdown _dropdown;
private void Awake()
{
if (_dropdown == null) _dropdown = GetComponent<Dropdown>();
}
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
AvrgMvvmScheduler scheduler)
{
CurrentAccessor = accessor;
CurrentBinding = binding;
if (binding == null || accessor == null || _dropdown == null) return;
// 构建选项列表
var options = BuildOptions(accessor.ValueType);
_dropdown.ClearOptions();
_dropdown.AddOptions(options);
// UI → 数据
_dropdown.onValueChanged.AddListener(index =>
{
if (index >= 0 && index < options.Count)
{
object convertedVal = ConvertOptionToValue(options[index], accessor.ValueType);
binding.WriteFromUI(convertedVal);
scheduler?.MarkUIDirty(binding);
}
});
// 数据 → UI
binding.OnUIDataChanged += value =>
{
int idx = FindOptionIndex(value, options);
if (idx >= 0)
_dropdown.SetValueWithoutNotify(idx);
};
// 首次同步
var initialValue = accessor.GetValue();
int initialIdx = FindOptionIndex(initialValue, options);
if (initialIdx >= 0)
_dropdown.SetValueWithoutNotify(initialIdx);
}
private List<string> BuildOptions(System.Type valueType)
{
// 1. 从元数据 PresetOptions 获取
if (BoundMetaData?.PresetOptions != null && BoundMetaData.PresetOptions.Count > 0)
return BoundMetaData.PresetOptions.ToList();
// 2. enum 类型:自动提取枚举名
if (valueType != null && valueType.IsEnum)
return System.Enum.GetNames(valueType).ToList();
// 3. 回退
return new List<string>();
}
private int FindOptionIndex(object value, List<string> options)
{
if (value == null || options.Count == 0) return -1;
string valueStr = value.ToString();
for (int i = 0; i < options.Count; i++)
{
if (options[i] == valueStr) return i;
}
return -1;
}
private static object ConvertOptionToValue(string option, System.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 option;
}
}
}
@@ -0,0 +1,67 @@
using System;
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// InputField 值组件 — 处理 string / float / int 类型的文本输入交互
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDInputFieldValue")]
public class XUIAvrgMDInputFieldValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("关联的 InputField 组件(未设置则自动查找)")]
private InputField _inputField;
private void Awake()
{
if (_inputField == null) _inputField = GetComponent<InputField>();
}
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
AvrgMvvmScheduler scheduler)
{
CurrentAccessor = accessor;
CurrentBinding = binding;
if (binding == null || accessor == null || _inputField == null) return;
// 设置输入类型
if (accessor.ValueType == typeof(float) || accessor.ValueType == typeof(int)
|| accessor.ValueType == typeof(double))
{
_inputField.contentType = InputField.ContentType.DecimalNumber;
}
// UI → 数据
_inputField.onEndEdit.AddListener(val =>
{
object convertedVal = ConvertStringToValue(val, accessor.ValueType);
binding.WriteFromUI(convertedVal);
scheduler?.MarkUIDirty(binding);
});
// 数据 → UI
binding.OnUIDataChanged += value =>
{
_inputField.SetTextWithoutNotify(accessor.FormatDisplay(value));
};
// 首次同步
var initialValue = accessor.GetValue();
_inputField.SetTextWithoutNotify(accessor.FormatDisplay(initialValue));
}
private static object ConvertStringToValue(string s, Type targetType)
{
if (targetType == typeof(string)) return s;
if (targetType == typeof(float) && float.TryParse(s, out float fVal)) return fVal;
if (targetType == typeof(int) && int.TryParse(s, out int iVal)) return iVal;
if (targetType == typeof(double) && double.TryParse(s, out double dVal)) return dVal;
return s;
}
}
}
@@ -0,0 +1,57 @@
using UnityEngine;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 标签值组件 — 处理只读文本的纯展示
/// 适用于 Label / Context / Unit / ReadOnly 等展示型 Tag
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDLabelValue")]
public class XUIAvrgMDLabelValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("关联的 Text 组件(未设置则自动查找)")]
private UnityEngine.UI.Text _uiText;
[SerializeField]
[Tooltip("关联的 TMP_Text 组件(未设置则自动查找)")]
private TMPro.TMP_Text _tmpText;
private void Awake()
{
if (_uiText == null) _uiText = GetComponent<UnityEngine.UI.Text>();
if (_tmpText == null) _tmpText = GetComponent<TMPro.TMP_Text>();
}
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
AvrgMvvmScheduler scheduler)
{
CurrentAccessor = accessor;
CurrentBinding = binding;
if (binding == null || accessor == null) return;
// 数据 → UI
binding.OnUIDataChanged += value =>
{
SetText(value);
};
// 首次同步
var initialValue = accessor.GetValue();
SetText(initialValue);
}
private void SetText(object value)
{
string displayStr = value?.ToString() ?? "";
if (_tmpText != null)
_tmpText.text = displayStr;
else if (_uiText != null)
_uiText.text = displayStr;
}
}
}
@@ -0,0 +1,86 @@
using System;
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// Slider 值组件 — 处理 float / int / double 类型的滑块交互
/// 适用于 Shader Range 属性或标记了 [XuiavrgFieldTag(Slider)] 的字段
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDSliderValue")]
public class XUIAvrgMDSliderValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("关联的 Slider 组件(未设置则自动查找)")]
private Slider _slider;
[SerializeField]
[Tooltip("值显示标签(可选,未设置则不更新)")]
private TMPro.TMP_Text _valueLabel;
private void Awake()
{
if (_slider == null) _slider = GetComponent<Slider>();
}
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
AvrgMvvmScheduler scheduler)
{
CurrentAccessor = accessor;
CurrentBinding = binding;
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;
}
// UI → 数据
_slider.onValueChanged.AddListener(val =>
{
object convertedVal = ConvertValueForAccessor(val, accessor.ValueType);
binding.WriteFromUI(convertedVal);
scheduler?.MarkUIDirty(binding);
if (_valueLabel != null)
_valueLabel.text = accessor.FormatDisplay(convertedVal);
});
// 数据 → UI
binding.OnUIDataChanged += value =>
{
float sliderVal = Convert.ToSingle(value);
_slider.SetValueWithoutNotify(sliderVal);
if (_valueLabel != null)
_valueLabel.text = accessor.FormatDisplay(value);
};
// 首次同步
var initialValue = accessor.GetValue();
if (initialValue != null)
{
_slider.SetValueWithoutNotify(Convert.ToSingle(initialValue));
if (_valueLabel != null)
_valueLabel.text = accessor.FormatDisplay(initialValue);
}
}
private static object ConvertValueForAccessor(float val, Type targetType)
{
if (targetType == typeof(int)) return (int)val;
if (targetType == typeof(double)) return (double)val;
return val;
}
}
}
@@ -0,0 +1,63 @@
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 纹理值组件 — 处理 Texture 类型的预览显示
/// 通过 RawImage.texture 展示当前纹理
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDTextureValue")]
public class XUIAvrgMDTextureValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("用于展示纹理的 RawImage 组件(未设置则自动查找)")]
private RawImage _textureDisplay;
[SerializeField]
[Tooltip("纹理名称标签(可选,用于显示纹理资源名)")]
private TMPro.TMP_Text _nameLabel;
private void Awake()
{
if (_textureDisplay == null) _textureDisplay = GetComponent<RawImage>();
}
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
AvrgMvvmScheduler scheduler)
{
CurrentAccessor = accessor;
CurrentBinding = binding;
if (binding == null || accessor == null) return;
// 数据 → UI
binding.OnUIDataChanged += value =>
{
if (value is Texture tex && _textureDisplay != null)
{
_textureDisplay.texture = tex;
}
if (_nameLabel != null)
{
_nameLabel.text = value is Texture t && t != null ? t.name : "None";
}
};
// 首次同步
var initialValue = accessor.GetValue();
if (initialValue is Texture initTex && _textureDisplay != null)
{
_textureDisplay.texture = initTex;
}
if (_nameLabel != null)
{
_nameLabel.text = initialValue is Texture t && t != null ? t.name : "None";
}
}
}
}
@@ -0,0 +1,114 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// ToggleGroup 值组件 — 处理 string / int / enum 类型的单选组交互
/// 适用于标记了 [XuiavrgFieldToggleGroupValue] 的字段
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDToggleGroupValue")]
public class XUIAvrgMDToggleGroupValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("关联的 ToggleGroup 组件(未设置则自动查找)")]
private ToggleGroup _toggleGroup;
private List<Toggle> _toggles = new List<Toggle>();
private List<string> _optionLabels = new List<string>();
private void Awake()
{
if (_toggleGroup == null) _toggleGroup = GetComponent<ToggleGroup>();
if (_toggleGroup != null)
{
_toggles = _toggleGroup.GetComponentsInChildren<Toggle>(true).ToList();
}
}
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
AvrgMvvmScheduler scheduler)
{
CurrentAccessor = accessor;
CurrentBinding = binding;
if (binding == null || accessor == null || _toggleGroup == null) return;
// 构建选项
BuildOptions(accessor.ValueType);
// 为每个 Toggle 注册事件
for (int i = 0; i < _toggles.Count && i < _optionLabels.Count; i++)
{
int idx = i;
_toggles[i].onValueChanged.AddListener(isOn =>
{
if (isOn)
{
object convertedVal = ConvertOptionToValue(_optionLabels[idx], accessor.ValueType);
binding.WriteFromUI(convertedVal);
scheduler?.MarkUIDirty(binding);
}
});
}
// 数据 → UI
binding.OnUIDataChanged += value =>
{
string valueStr = value?.ToString();
for (int i = 0; i < _toggles.Count && i < _optionLabels.Count; i++)
{
if (_optionLabels[i] == valueStr)
{
_toggles[i].SetIsOnWithoutNotify(true);
return;
}
}
};
// 首次同步
var initialValue = accessor.GetValue();
string initStr = initialValue?.ToString();
for (int i = 0; i < _toggles.Count && i < _optionLabels.Count; i++)
{
if (_optionLabels[i] == initStr)
{
_toggles[i].SetIsOnWithoutNotify(true);
break;
}
}
}
private void BuildOptions(System.Type valueType)
{
_optionLabels.Clear();
// 1. 从元数据 PresetOptions
if (BoundMetaData?.PresetOptions != null && BoundMetaData.PresetOptions.Count > 0)
{
_optionLabels.AddRange(BoundMetaData.PresetOptions);
return;
}
// 2. enum 类型
if (valueType != null && valueType.IsEnum)
{
_optionLabels.AddRange(System.Enum.GetNames(valueType));
return;
}
}
private static object ConvertOptionToValue(string option, System.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 option;
}
}
}
@@ -0,0 +1,52 @@
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// Toggle 值组件 — 处理 bool 类型的开关交互
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDToggleValue")]
public class XUIAvrgMDToggleValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("关联的 Toggle 组件(未设置则自动查找)")]
private Toggle _toggle;
private void Awake()
{
if (_toggle == null) _toggle = GetComponent<Toggle>();
}
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
AvrgMvvmScheduler scheduler)
{
CurrentAccessor = accessor;
CurrentBinding = binding;
if (binding == null || accessor == null || _toggle == null) return;
// 只读模式下禁用交互
_toggle.interactable = !accessor.IsReadOnly;
// UI → 数据
_toggle.onValueChanged.AddListener(val =>
{
binding.WriteFromUI(val);
scheduler?.MarkUIDirty(binding);
});
// 数据 → UI
binding.OnUIDataChanged += value =>
{
_toggle.SetIsOnWithoutNotify(value is bool b && b);
};
// 首次同步
var initialValue = accessor.GetValue();
_toggle.SetIsOnWithoutNotify(initialValue is bool bv && bv);
}
}
}
@@ -0,0 +1,137 @@
using UnityEngine;
using UnityEngine.UI;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 向量值组件 — 处理 Vector2 / Vector3 / Vector4 类型的多字段输入
/// 子节点按 BindingSlots 绑定各分量(如 "x", "y", "z", "w")
/// </summary>
[AddComponentMenu("Xeric Library/UI Action Vessel/Values/XUIAvrgMDVectorValue")]
public class XUIAvrgMDVectorValue : XUIAvrgMDValue
{
[SerializeField]
[Tooltip("X 分量 InputField(可选,也可通过 BindingSlots 的子节点绑定)")]
private InputField _inputX;
[SerializeField]
[Tooltip("Y 分量 InputField")]
private InputField _inputY;
[SerializeField]
[Tooltip("Z 分量 InputField(Vector3/Vector4 时使用)")]
private InputField _inputZ;
[SerializeField]
[Tooltip("W 分量 InputField(Vector4 时使用)")]
private InputField _inputW;
public override void BindToAccessor(
IAvrgValueAccessor accessor,
AvrgMvvmBinding binding,
AvrgMvvmScheduler scheduler)
{
CurrentAccessor = accessor;
CurrentBinding = binding;
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);
// 为每个输入注册事件
BindInputField(_inputX, 0, accessor, binding, scheduler);
BindInputField(_inputY, 1, accessor, binding, scheduler);
BindInputField(_inputZ, 2, accessor, binding, scheduler);
BindInputField(_inputW, 3, accessor, binding, scheduler);
// 数据 → UI
binding.OnUIDataChanged += value =>
{
UpdateAllInputs(value);
};
// 首次同步
var initialValue = accessor.GetValue();
UpdateAllInputs(initialValue);
}
private void BindInputField(InputField input, int componentIndex,
IAvrgValueAccessor accessor, AvrgMvvmBinding binding, AvrgMvvmScheduler scheduler)
{
if (input == null) return;
input.onEndEdit.AddListener(val =>
{
if (float.TryParse(val, out float num))
{
var currentVal = accessor.GetValue();
var modified = SetVectorComponent(currentVal, componentIndex, num);
binding.WriteFromUI(modified);
scheduler?.MarkUIDirty(binding);
}
});
}
private void UpdateAllInputs(object value)
{
if (value == null) return;
if (_inputX != null && _inputX.gameObject.activeSelf)
_inputX.SetTextWithoutNotify(GetVectorComponent(value, 0).ToString("F2"));
if (_inputY != null && _inputY.gameObject.activeSelf)
_inputY.SetTextWithoutNotify(GetVectorComponent(value, 1).ToString("F2"));
if (_inputZ != null && _inputZ.gameObject.activeSelf)
_inputZ.SetTextWithoutNotify(GetVectorComponent(value, 2).ToString("F2"));
if (_inputW != null && _inputW.gameObject.activeSelf)
_inputW.SetTextWithoutNotify(GetVectorComponent(value, 3).ToString("F2"));
}
private static int GetComponentCount(System.Type type)
{
if (type == typeof(Vector2)) return 2;
if (type == typeof(Vector3)) return 3;
if (type == typeof(Vector4)) return 4;
return 0;
}
private static float GetVectorComponent(object vec, int index)
{
return vec switch
{
Vector2 v2 => index == 0 ? v2.x : v2.y,
Vector3 v3 => index == 0 ? v3.x : index == 1 ? v3.y : v3.z,
Vector4 v4 => index == 0 ? v4.x : index == 1 ? v4.y : index == 2 ? v4.z : v4.w,
_ => 0f
};
}
private static object SetVectorComponent(object vec, int index, float value)
{
return vec switch
{
Vector2 v2 => index == 0 ? new Vector2(value, v2.y) : new Vector2(v2.x, value),
Vector3 v3 => index == 0 ? new Vector3(value, v3.y, v3.z)
: index == 1 ? new Vector3(v3.x, value, v3.z) : new Vector3(v3.x, v3.y, value),
Vector4 v4 => index == 0 ? new Vector4(value, v4.y, v4.z, v4.w)
: index == 1 ? new Vector4(v4.x, value, v4.z, v4.w)
: index == 2 ? new Vector4(v4.x, v4.y, value, v4.w)
: new Vector4(v4.x, v4.y, v4.z, value),
_ => vec
};
}
private static void SetInputActive(InputField input, int index, int componentCount)
{
if (input != null)
input.gameObject.SetActive(index < componentCount);
}
}
}