184 lines
4.6 KiB
C#
184 lines
4.6 KiB
C#
using QFramework;
|
|
using QFramework.Example;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
public class AnLiKaoHe: MonoBehaviour
|
|
{
|
|
|
|
|
|
public GameObject JieXi;
|
|
// 配置字段 - 可在Inspector中设置
|
|
[SerializeField] private List<TMP_InputField> inputFields = new List<TMP_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; // 每个输入框的正确状态跟踪
|
|
|
|
|
|
public bool isFirist = true;
|
|
private void Start()
|
|
{
|
|
if (JieXi != null)
|
|
JieXi.SetActive(false);
|
|
|
|
InitializeTrackingLists();
|
|
RegisterInputEvents();
|
|
HideAllExplanations();
|
|
isFirist = true;
|
|
}
|
|
|
|
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)
|
|
{
|
|
if (isFirist)
|
|
{
|
|
ScoreController.Instance.Add("案例考核", 10);
|
|
|
|
}
|
|
fieldCorrect[fieldIndex] = true;
|
|
inputFields[fieldIndex].interactable = false;
|
|
ShowFieldExplanation(fieldIndex);
|
|
CheckAllFieldsCompleted();
|
|
}
|
|
else
|
|
{
|
|
isFirist = false;
|
|
|
|
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 (JieXi != null)
|
|
JieXi.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 (JieXi != null)
|
|
JieXi.SetActive(false);
|
|
|
|
for (int i = 0; i < inputFields.Count; i++)
|
|
{
|
|
inputFields[i].text = "";
|
|
inputFields[i].interactable = true;
|
|
}
|
|
|
|
HideAllExplanations();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|