46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
using TMPro;
|
||
using UnityEngine;
|
||
|
||
namespace Cielonos.Settings.UI
|
||
{
|
||
/// <summary>
|
||
/// 不带 [Range] 特性的 int 字段的设置条目 — 使用 TMP_InputField(整数模式)。
|
||
/// </summary>
|
||
public class SettingsEntryIntInput : SettingsEntryBase
|
||
{
|
||
[SerializeField] private TMP_InputField inputField;
|
||
|
||
protected override void SetupUI()
|
||
{
|
||
if (inputField == null) return;
|
||
|
||
inputField.contentType = TMP_InputField.ContentType.IntegerNumber;
|
||
inputField.onEndEdit.AddListener(OnInputEndEdit);
|
||
}
|
||
|
||
public override void RefreshValue()
|
||
{
|
||
if (inputField != null)
|
||
inputField.SetTextWithoutNotify(GetFieldValue().ToString());
|
||
}
|
||
|
||
private void OnInputEndEdit(string text)
|
||
{
|
||
if (int.TryParse(text, out int value))
|
||
{
|
||
SetFieldValue(value);
|
||
}
|
||
else
|
||
{
|
||
RefreshValue();
|
||
}
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
if (inputField != null)
|
||
inputField.onEndEdit.RemoveListener(OnInputEndEdit);
|
||
}
|
||
}
|
||
}
|