1118OPSSNew/Assets/Zion/Scripts/manage/QuizSystem.cs

281 lines
8.2 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 UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections.Generic;
using System.Linq;
using SK.Framework;
using Microsoft.MixedReality.Toolkit.OpenVR.Headers;
public class QuizSystem : MonoBehaviour
{
[Header("UI References")]
public TextMeshProUGUI questionText;
public ToggleGroup toggleGroup;
public Toggle[] optionToggles;
public TextMeshProUGUI[] optionTexts;
public Button confirmButton;
public Button nextButton;
public TextMeshProUGUI explanationText;
public GameObject explanationPanel;
[Header("Question Data")]
public QuestionsProfile judgeProfile; // 判断题资源
public QuestionsProfile singleProfile; // 单选题资源
private List<QuestionBase> allQuestions = new List<QuestionBase>();
public int currentQuestionIndex = 0;
private bool isJudgeQuestion = false;
void Awake()
{
SetupUI();
}
void OnEnable()
{
currentQuestionIndex = 0;
allQuestions.Clear();
isJudgeQuestion = false;
InitializeQuiz();
LoadNextQuestion();
confirmButton.gameObject.SetActive(true);
nextButton.gameObject.SetActive(false);
explanationPanel.SetActive(false);
toggleGroup.SetAllTogglesOff();
overBtn.gameObject.SetActive(false);
}
void InitializeQuiz()
{
// 从判断题资源中随机选择5个
if (judgeProfile != null && judgeProfile.Judges != null && judgeProfile.Judges.Count > 0)
{
var randomJudgeQuestions = judgeProfile.Judges
.OrderBy(x => Random.value)
.Take(5)
.Cast<QuestionBase>()
.ToList();
allQuestions.AddRange(randomJudgeQuestions);
}
// 从单选题资源中随机选择10个
if (singleProfile != null && singleProfile.SingleChoices != null && singleProfile.SingleChoices.Count > 0)
{
var randomSingleQuestions = singleProfile.SingleChoices
.OrderBy(x => Random.value)
.Take(10)
.Cast<QuestionBase>()
.ToList();
allQuestions.AddRange(randomSingleQuestions);
}
Debug.Log($"总共加载了 {allQuestions.Count} 道题目");
}
void SetupUI()
{
confirmButton.onClick.AddListener(OnConfirmButtonClick);
nextButton.onClick.AddListener(OnNextButtonClick);
// 初始状态
confirmButton.gameObject.SetActive(true);
nextButton.gameObject.SetActive(false);
explanationPanel.SetActive(false);
// 设置Toggle Group
foreach (var toggle in optionToggles)
{
toggle.group = toggleGroup;
toggle.onValueChanged.AddListener(OnToggleValueChanged);
}
}
void LoadNextQuestion()
{
if (currentQuestionIndex >= allQuestions.Count)
{
// 所有题目完成
ShowQuizComplete();
return;
}
// 重置UI状态
toggleGroup.SetAllTogglesOff();
explanationPanel.SetActive(false);
confirmButton.gameObject.SetActive(true);
nextButton.gameObject.SetActive(false);
confirmButton.interactable = false;
var currentQuestion = allQuestions[currentQuestionIndex];
// 判断题目类型
isJudgeQuestion = currentQuestion is JudgeQuestion;
// 显示题目
questionText.text = $"{currentQuestionIndex + 1}/15. {currentQuestion.Question}";
// 设置选项
SetupOptions(currentQuestion);
// 隐藏未使用的选项
UpdateOptionVisibility();
}
void SetupOptions(QuestionBase question)
{
if (question is SingleChoiceQuestion singleQuestion)
{
// 单选题:显示所有选项
for (int i = 0; i < optionTexts.Length; i++)
{
if (i < singleQuestion.Choices.Count)
{
optionTexts[i].text = $"{(char)('A' + i)}. {singleQuestion.Choices[i].text}";
optionToggles[i].gameObject.SetActive(true);
}
else
{
optionToggles[i].gameObject.SetActive(false);
}
}
}
else if (question is JudgeQuestion judgeQuestion)
{
// 判断题:只显示正确和错误两个选项
optionTexts[0].text = $"A. {judgeQuestion.Positive}";
optionTexts[1].text = $"B. {judgeQuestion.Negative}";
// 前两个选项显示,后两个隐藏
for (int i = 0; i < optionToggles.Length; i++)
{
optionToggles[i].gameObject.SetActive(i < 2);
}
}
}
void UpdateOptionVisibility()
{
// 根据题目类型更新选项显示
if (isJudgeQuestion)
{
// 判断题只显示前两个选项
for (int i = 0; i < optionToggles.Length; i++)
{
optionToggles[i].gameObject.SetActive(i < 2);
}
}
else
{
// 单选题显示所有有内容的选项
var singleQuestion = allQuestions[currentQuestionIndex] as SingleChoiceQuestion;
if (singleQuestion != null)
{
for (int i = 0; i < optionToggles.Length; i++)
{
optionToggles[i].gameObject.SetActive(i < singleQuestion.Choices.Count);
}
}
}
}
void OnToggleValueChanged(bool isOn)
{
// 只要有选项被选中,确定按钮就可交互
confirmButton.interactable = toggleGroup.AnyTogglesOn();
}
void OnConfirmButtonClick()
{
if (!toggleGroup.AnyTogglesOn()) return;
// 获取选中的Toggle
Toggle selectedToggle = toggleGroup.ActiveToggles().FirstOrDefault();
if (selectedToggle == null) return;
// 获取选中的索引
int selectedIndex = System.Array.IndexOf(optionToggles, selectedToggle);
// 显示解析
ShowExplanation(selectedIndex);
// 切换按钮状态
confirmButton.gameObject.SetActive(false);
nextButton.gameObject.SetActive(true);
}
void ShowExplanation(int selectedIndex)
{
var currentQuestion = allQuestions[currentQuestionIndex];
string explanation = currentQuestion.Analysis;
// 检查答案是否正确
bool isCorrect = false;
if (currentQuestion is SingleChoiceQuestion singleQuestion)
{
isCorrect = selectedIndex == singleQuestion.Answer;
}
else if (currentQuestion is JudgeQuestion judgeQuestion)
{
// 判断题索引0对应正确(true)索引1对应错误(false)
bool userAnswer = selectedIndex == 0;
isCorrect = userAnswer == judgeQuestion.Answer;
}
// 显示解析
explanationText.text = $"{(isCorrect ? "" : "")}\n\n解析{explanation}";
explanationPanel.SetActive(true);
}
void OnNextButtonClick()
{
currentQuestionIndex++;
LoadNextQuestion();
}
public Button overBtn;
void ShowQuizComplete()
{
questionText.text = "答题完成!";
// 隐藏所有选项
foreach (var toggle in optionToggles)
{
toggle.gameObject.SetActive(false);
}
confirmButton.gameObject.SetActive(false);
nextButton.gameObject.SetActive(false);
explanationPanel.SetActive(false);
overBtn.gameObject.SetActive(true);
}
void OnDestroy()
{
// 清理事件监听
confirmButton.onClick.RemoveListener(OnConfirmButtonClick);
nextButton.onClick.RemoveListener(OnNextButtonClick);
foreach (var toggle in optionToggles)
{
toggle.onValueChanged.RemoveListener(OnToggleValueChanged);
}
}
// 重置答题系统
void ResetQuiz()
{
currentQuestionIndex = 0;
allQuestions.Clear();
InitializeQuiz();
LoadNextQuestion();
}
void OnDisable()
{
currentQuestionIndex = 0;
}
}