164 lines
4.4 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using QFramework;
using QFramework.Example;
/// <summary>
/// 管理输入验证、尝试次数和解析显示的核心组件
/// </summary>
public class InputAnswerChecker : MonoBehaviour
{
// 配置字段 - 可在Inspector中设置
[SerializeField] private List<InputField> inputFields = new List<InputField>();
[SerializeField] private GameObject explanationObject;
[SerializeField] private List<string> correctAnswers = new List<string>();
[SerializeField] private Text codeExplanationText;
[SerializeField] private string[] codeExplanations;
[SerializeField] private bool caseSensitive = false;
// 状态字段
private const int MAX_ATTEMPTS = 3; // 每个输入框允许的最大尝试次数
private bool hasAnsweredCorrectly = false; // 是否已正确回答所有问题
private List<int> fieldAttempts; // 每个输入框的尝试次数跟踪
private List<bool> fieldCorrect; // 每个输入框的正确状态跟踪
private void Start()
{
if (explanationObject != null)
explanationObject.SetActive(false);
InitializeTrackingLists();
RegisterInputEvents();
HideAllExplanations();
}
private void InitializeTrackingLists()
{
fieldAttempts = new List<int>(new int[inputFields.Count]);
fieldCorrect = new List<bool>(new bool[inputFields.Count]);
}
private void RegisterInputEvents()
{
for (int i = 0; i < inputFields.Count; i++)
{
int fieldIndex = i;
inputFields[i].onEndEdit.AddListener(delegate { CheckField(fieldIndex); });
}
}
private void CheckField(int fieldIndex)
{
// 如果已完成答题或该字段已正确,则跳过检查
if (hasAnsweredCorrectly || fieldCorrect[fieldIndex])
return;
// 检查单个输入框尝试次数是否超限
if (fieldAttempts[fieldIndex] >= MAX_ATTEMPTS)
{
ShowAllAnswersAndExplanation();
return;
}
string currentValue = ProcessInput(inputFields[fieldIndex].text);
string correctValue = ProcessInput(correctAnswers[fieldIndex]);
fieldAttempts[fieldIndex]++;
if (currentValue == correctValue)
{
fieldCorrect[fieldIndex] = true;
inputFields[fieldIndex].interactable = false;
ShowFieldExplanation(fieldIndex);
CheckAllFieldsCompleted();
}
else
{
inputFields[fieldIndex].text = "";
// 显示错误提示
ShowErrorTip(fieldIndex);
}
}
private string ProcessInput(string input)
{
return caseSensitive ? input : input.ToLower();
}
private void ShowErrorTip(int fieldIndex)
{
var data = new UIResultTipData();
data.label = $"输入错误,请重新输入";
data.isRight = false;
data.autoHideTime = 1.5f;
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().Start(this);
}
private void CheckAllFieldsCompleted()
{
foreach (bool correct in fieldCorrect)
{
if (!correct) return;
}
hasAnsweredCorrectly = true;
ShowExplanation();
}
private void ShowAllAnswersAndExplanation()
{
hasAnsweredCorrectly = true;
for (int i = 0; i < inputFields.Count; i++)
{
if (!fieldCorrect[i])
{
inputFields[i].text = correctAnswers[i];
inputFields[i].interactable = false;
ShowFieldExplanation(i);
}
}
ShowExplanation();
}
private void ShowExplanation()
{
if (explanationObject != null)
explanationObject.SetActive(true);
}
private void HideAllExplanations()
{
if (codeExplanationText != null)
codeExplanationText.text = "";
}
private void ShowFieldExplanation(int fieldIndex)
{
if (codeExplanationText != null && codeExplanations.Length > fieldIndex)
{
codeExplanationText.text = codeExplanations[fieldIndex];
}
}
public void ResetAttempts()
{
hasAnsweredCorrectly = false;
InitializeTrackingLists();
if (explanationObject != null)
explanationObject.SetActive(false);
for (int i = 0; i < inputFields.Count; i++)
{
inputFields[i].text = "";
inputFields[i].interactable = true;
}
HideAllExplanations();
}
}