Passion & UI
This commit is contained in:
59
Assets/Scripts/Settings/UI/SettingsEntrySlider.cs
Normal file
59
Assets/Scripts/Settings/UI/SettingsEntrySlider.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Reflection;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cielonos.Settings.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 带 [Range] 特性的 int 字段的设置条目 — 使用 Slider + 数值文本。
|
||||
/// </summary>
|
||||
public class SettingsEntrySlider : SettingsEntryBase
|
||||
{
|
||||
[SerializeField] private Slider slider;
|
||||
[SerializeField] private TMP_Text valueText;
|
||||
|
||||
protected override void SetupUI()
|
||||
{
|
||||
if (slider == null) return;
|
||||
|
||||
var rangeAttr = fieldInfo.GetCustomAttribute<RangeAttribute>();
|
||||
if (rangeAttr != null)
|
||||
{
|
||||
slider.minValue = rangeAttr.min;
|
||||
slider.maxValue = rangeAttr.max;
|
||||
}
|
||||
|
||||
slider.wholeNumbers = true;
|
||||
slider.onValueChanged.AddListener(OnSliderChanged);
|
||||
}
|
||||
|
||||
public override void RefreshValue()
|
||||
{
|
||||
if (slider == null) return;
|
||||
|
||||
int currentValue = (int)GetFieldValue();
|
||||
slider.SetValueWithoutNotify(currentValue);
|
||||
UpdateValueText(currentValue);
|
||||
}
|
||||
|
||||
private void OnSliderChanged(float value)
|
||||
{
|
||||
int intValue = Mathf.RoundToInt(value);
|
||||
SetFieldValue(intValue);
|
||||
UpdateValueText(intValue);
|
||||
}
|
||||
|
||||
private void UpdateValueText(int value)
|
||||
{
|
||||
if (valueText != null)
|
||||
valueText.text = value.ToString();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (slider != null)
|
||||
slider.onValueChanged.RemoveListener(OnSliderChanged);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user