Tz2/Assets/Scripts/DropdownValidator.cs

205 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
using DefaultNamespace.ProcessMode;
using Framework.Manager;
using UnityEngine.UI;
/// <summary>
/// 下拉框验证组件
/// 可以挂载到任何 Dropdown 上,实现选择正确后自动跳转引导
/// </summary>
public class DropdownValidator : MonoBehaviour
{
[Header("验证设置")]
[Tooltip("是否启用自动跳转功能")]
[SerializeField] private bool enableAutoJump = true;
[Tooltip("是否启用调试日志")]
[SerializeField] private bool enableDebugLog = true;
[Tooltip("验证延迟时间(秒),避免频繁验证")]
[SerializeField] private float validationDelay = 0.5f;
private Dropdown dropdown;
private int lastValidatedIndex = -1;
/// <summary>
/// 初始化组件
/// </summary>
private void Awake()
{
dropdown = GetComponent<Dropdown>();
if (dropdown == null)
{
Debug.LogError($"DropdownValidator: 在 {gameObject.name} 中未找到 Dropdown 组件!");
return;
}
// 监听选择变化事件
dropdown.onValueChanged.AddListener(OnDropdownValueChanged);
if (enableDebugLog)
{
Debug.Log($"DropdownValidator: 初始化完成 - {gameObject.name}");
}
}
/// <summary>
/// 下拉框选择变化回调
/// </summary>
/// <param name="value">选择的索引</param>
private void OnDropdownValueChanged(int value)
{
// 检查是否与上次验证的索引相同
if (value == lastValidatedIndex)
{
return;
}
// 立即执行验证(移除防抖延迟)
ValidateSelection(value);
}
/// <summary>
/// 验证选择并自动跳转
/// </summary>
/// <param name="selectedIndex">选择的索引</param>
private void ValidateSelection(int selectedIndex)
{
lastValidatedIndex = selectedIndex;
// 检查是否有有效选择
if (selectedIndex < 0 || selectedIndex >= dropdown.options.Count)
{
if (enableDebugLog)
{
Debug.Log($"DropdownValidator: 选择无效,不验证 - 下拉框: {gameObject.name}, 索引: {selectedIndex}");
}
return;
}
// 获取选择的文本
string selectedText = dropdown.options[selectedIndex].text;
if (enableDebugLog)
{
Debug.Log($"DropdownValidator: 开始验证选择 - 下拉框: {gameObject.name}, 索引: {selectedIndex}, 文本: '{selectedText}'");
}
// 使用新的严谨验证方法验证选择是否匹配当前动作的正确答案传递UI的Instance ID
string uiInstanceId = gameObject.GetInstanceID().ToString();
bool isSelectionCorrect = MotionFramework.MotionEngine.GetModule<ProcessManager>().ValidateInputAgainstCurrentAction(selectedText, uiInstanceId);
if (isSelectionCorrect)
{
if (enableDebugLog)
{
Debug.Log($"DropdownValidator: 选择正确自动跳转 - 下拉框: {gameObject.name}, 索引: {selectedIndex}, 文本: '{selectedText}'");
}
if (enableAutoJump)
{
// 选择正确,直接跳转到下一个引导步骤(不调用 HandleClick
InterfaceManager.LoadTriggerNextGuide();
}
}
else
{
if (enableDebugLog)
{
Debug.Log($"DropdownValidator: 选择错误,不跳转 - 下拉框: {gameObject.name}, 索引: {selectedIndex}, 文本: '{selectedText}'");
}
}
}
/// <summary>
/// 手动触发验证
/// </summary>
public void TriggerValidation()
{
if (dropdown != null && dropdown.value >= 0 && dropdown.value < dropdown.options.Count)
{
ValidateSelection(dropdown.value);
}
}
/// <summary>
/// 设置是否启用自动跳转
/// </summary>
/// <param name="enabled">是否启用</param>
public void SetAutoJumpEnabled(bool enabled)
{
enableAutoJump = enabled;
if (enableDebugLog)
{
Debug.Log($"DropdownValidator: 自动跳转功能 {(enabled ? "" : "")} - {gameObject.name}");
}
}
/// <summary>
/// 设置验证延迟时间
/// </summary>
/// <param name="delay">延迟时间(秒)</param>
public void SetValidationDelay(float delay)
{
validationDelay = Mathf.Max(0f, delay);
if (enableDebugLog)
{
Debug.Log($"DropdownValidator: 验证延迟设置为 {validationDelay} 秒 - {gameObject.name}");
}
}
/// <summary>
/// 清除下拉框选择
/// </summary>
public void ClearSelection()
{
if (dropdown != null)
{
dropdown.value = 0; // 重置为第一个选项
lastValidatedIndex = -1;
}
}
/// <summary>
/// 获取当前选择值
/// </summary>
/// <returns>当前选择的文本</returns>
public string GetCurrentValue()
{
if (dropdown != null && dropdown.value >= 0 && dropdown.value < dropdown.options.Count)
{
return dropdown.options[dropdown.value].text;
}
return "";
}
/// <summary>
/// 获取当前选择索引
/// </summary>
/// <returns>当前选择的索引</returns>
public int GetCurrentIndex()
{
return dropdown?.value ?? -1;
}
/// <summary>
/// 设置下拉框选项
/// </summary>
/// <param name="options">选项列表</param>
public void SetOptions(List<string> options)
{
if (dropdown != null)
{
dropdown.ClearOptions();
dropdown.AddOptions(options);
lastValidatedIndex = -1;
if (enableDebugLog)
{
Debug.Log($"DropdownValidator: 设置选项完成 - 下拉框: {gameObject.name}, 选项数量: {options.Count}");
}
}
}
}