killapp/Assets/Scripts/UI/Pages/Safetylearning/SafetylearningQuestion.cs
“虞渠成” 7b5272b468 0608
2026-06-08 08:55:10 +08:00

120 lines
3.6 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;
using System.Collections;
using System.Collections.Generic;
using Kill.Managers;
using UnityEngine;
using UnityEngine.UI;
public class SafetylearningQuestion : MonoBehaviour
{
public Text questionIndexText;
public Text questionText;
public GameObject answerContainer;
public GameObject answerButtonPrefab;
QuestionInfo questionInfo;
public bool isChinese = false;
List<GameObject> answerButtons = new List<GameObject>();
// 选项选择事件
public event Action OnOptionSelected;
public void Init(int questionIndex, QuestionInfo question)
{
answerButtons.Clear();
questionIndexText.text = questionIndexText.text.Replace("{0}", questionIndex.ToString());
answerButtonPrefab.SetActive(false);
questionInfo = question;
isChinese = LanguageManager.Instance.languageType == LanguageManager.LanguageType.Chinese;
questionText.text = isChinese ? question.questionContent : question.questionContentEn;
for (int i = 0; i < question.options.Count; i++)
{
int index = i;
var option = question.options[i];
var answerButton = Instantiate(answerButtonPrefab, answerContainer.transform);
answerButton.SetActive(true);
answerButton.GetComponent<Button>().onClick.AddListener(() => ClickAnswerButton(index));
answerButton.GetComponent<SafetylearningOption>().Init(option, isChinese);
answerButtons.Add(answerButton);
}
}
public void ClickAnswerButton(int index)
{
for(int i = 0; i <answerButtons.Count; i++) {
answerButtons[i].GetComponent<SafetylearningOption>().SetSelected(i==index);
}
// 触发选择事件
OnOptionSelected?.Invoke();
}
/// <summary>
/// 获取用户选择的答案(如 "AB"、"C"
/// </summary>
public string GetUserAnswer()
{
string answer = "";
for (int i = 0; i < answerButtons.Count; i++)
{
if (answerButtons[i].GetComponent<SafetylearningOption>().isSelected)
{
answer += questionInfo.options[i].optionLabel;
}
}
// 排序确保 AB 而不是 BA
char[] chars = answer.ToCharArray();
Array.Sort(chars);
return new string(chars);
}
/// <summary>
/// 是否已选择答案
/// </summary>
public bool HasSelectedAnswer()
{
foreach (var btn in answerButtons)
{
if (btn.GetComponent<SafetylearningOption>().isSelected)
return true;
}
return false;
}
/// <summary>
/// 禁用所有选项交互
/// </summary>
public void DisableAllOptions()
{
foreach (var btn in answerButtons)
{
btn.GetComponent<Button>().interactable = false;
}
}
public void CheckAnswer()
{
for (int i = 0; i < questionInfo.options.Count; i++)
{
var option = questionInfo.options[i];
var optionComponent = answerButtons[i].GetComponent<SafetylearningOption>();
bool isSelected = optionComponent.isSelected;
// 标注正确答案
if (option.isCorrect)
{
optionComponent.SetCorrect(true);
}
// 标注选错的
else if (isSelected)
{
optionComponent.SetCorrect(false);
}
// 其他只禁用交互
else
{
optionComponent.GetComponent<Button>().interactable = false;
}
}
}
}