171 lines
4.9 KiB
C#
171 lines
4.9 KiB
C#
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using DefaultNamespace.ProcessMode;
|
||
using Framework.Manager;
|
||
using UnityEngine.UI;
|
||
|
||
/// <summary>
|
||
/// 输入框验证组件
|
||
/// 可以挂载到任何 TMP_InputField 上,实现输入正确后自动跳转引导
|
||
/// </summary>
|
||
public class NPUTFieldValidator : MonoBehaviour
|
||
{
|
||
[Header("验证设置")]
|
||
[Tooltip("是否启用自动跳转功能")]
|
||
[SerializeField] private bool enableAutoJump = true;
|
||
|
||
[Tooltip("是否启用调试日志")]
|
||
[SerializeField] private bool enableDebugLog = true;
|
||
|
||
[Tooltip("验证延迟时间(秒),避免频繁验证")]
|
||
[SerializeField] private float validationDelay = 0.5f;
|
||
|
||
private InputField inputField;
|
||
private string lastValidatedValue = "";
|
||
|
||
/// <summary>
|
||
/// 初始化组件
|
||
/// </summary>
|
||
private void Awake()
|
||
{
|
||
inputField = GetComponent<InputField>();
|
||
if (inputField == null)
|
||
{
|
||
Debug.LogError($"InputFieldValidator: 在 {gameObject.name} 中未找到 InputField 组件!");
|
||
return;
|
||
}
|
||
|
||
// 监听输入变化事件
|
||
inputField.onValueChanged.AddListener(OnInputValueChanged);
|
||
|
||
if (enableDebugLog)
|
||
{
|
||
// Debug.Log($"InputFieldValidator: 初始化完成 - {gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 输入值变化回调
|
||
/// </summary>
|
||
/// <param name="value">输入值</param>
|
||
private void OnInputValueChanged(string value)
|
||
{
|
||
// 检查是否与上次验证的值相同
|
||
if (value == lastValidatedValue)
|
||
{
|
||
return;
|
||
}
|
||
|
||
// 立即执行验证(移除防抖延迟)
|
||
ValidateInput(value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证输入并自动跳转
|
||
/// </summary>
|
||
/// <param name="value">输入值</param>
|
||
private void ValidateInput(string value)
|
||
{
|
||
lastValidatedValue = value;
|
||
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"InputFieldValidator: 开始验证输入 - 输入框: {gameObject.name}, 值: '{value}'");
|
||
}
|
||
|
||
// 检查是否为空值
|
||
if (string.IsNullOrWhiteSpace(value))
|
||
{
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"InputFieldValidator: 输入为空,不验证 - 输入框: {gameObject.name}");
|
||
}
|
||
return;
|
||
}
|
||
|
||
// 使用新的严谨验证方法:验证输入是否匹配当前动作的正确答案,传递UI的Instance ID
|
||
string uiInstanceId = gameObject.GetInstanceID().ToString();
|
||
bool isInputCorrect = MotionFramework.MotionEngine.GetModule<ProcessManager>().ValidateInputAgainstCurrentAction(value, uiInstanceId);
|
||
|
||
if (isInputCorrect)
|
||
{
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"InputFieldValidator: 输入正确自动跳转 - 输入框: {gameObject.name}, 值: '{value}'");
|
||
}
|
||
|
||
if (enableAutoJump)
|
||
{
|
||
// 输入正确,直接跳转到下一个引导步骤(不调用 HandleClick)
|
||
InterfaceManager.LoadTriggerNextGuide();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"InputFieldValidator: 输入错误,不跳转 - 输入框: {gameObject.name}, 值: '{value}'");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 手动触发验证
|
||
/// </summary>
|
||
public void TriggerValidation()
|
||
{
|
||
if (inputField != null && !string.IsNullOrWhiteSpace(inputField.text))
|
||
{
|
||
ValidateInput(inputField.text);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置是否启用自动跳转
|
||
/// </summary>
|
||
/// <param name="enabled">是否启用</param>
|
||
public void SetAutoJumpEnabled(bool enabled)
|
||
{
|
||
enableAutoJump = enabled;
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"InputFieldValidator: 自动跳转功能 {(enabled ? "启用" : "禁用")} - {gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置验证延迟时间
|
||
/// </summary>
|
||
/// <param name="delay">延迟时间(秒)</param>
|
||
public void SetValidationDelay(float delay)
|
||
{
|
||
validationDelay = Mathf.Max(0f, delay);
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"InputFieldValidator: 验证延迟设置为 {validationDelay} 秒 - {gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清除输入框内容
|
||
/// </summary>
|
||
public void ClearInput()
|
||
{
|
||
if (inputField != null)
|
||
{
|
||
inputField.text = "";
|
||
lastValidatedValue = "";
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前输入值
|
||
/// </summary>
|
||
/// <returns>当前输入值</returns>
|
||
public string GetCurrentValue()
|
||
{
|
||
return inputField?.text ?? "";
|
||
}
|
||
}
|