2025-06-26 14:07:29 +08:00

144 lines
4.4 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
namespace QFramework.Example
{
public class DaTiUIData : UIPanelData
{
}
public partial class DaTiUI : UIPanel
{
public List<QuestionData> questions = new List<QuestionData>();
public Button TiJiaoBtn;
public Button XiaYiBuBtn;
private int totalScore = 0; // 新增变量用于记录总得分
private List<bool> answerResults = new List<bool>(); // 用于记录每道题的答题结果
/// <summary>
/// 初始化数据
/// </summary>
public void ReSetData()
{
totalScore = 0; // 重置总得分
answerResults.Clear(); // 清空答题结果记录
for (int i = 0; i < questions.Count; i++)
{
int P = i;
questions[P].JieXi.gameObject.SetActive(false);
for (int j = 0; j < questions[P].options.Count; j++)
{
questions[P].options[j].interactable = true;
questions[P].options[j].isOn = false;
}
}
TiJiaoBtn.gameObject.SetActive(true);
XiaYiBuBtn.gameObject.SetActive(false);
}
/// <summary>
/// 按钮,提交答案
/// </summary>
public void TiJiao()
{
totalScore = 0; // 每次提交时重置总得分
answerResults.Clear(); // 清空答题结果记录
for (int i = 0; i < questions.Count; i++)
{
int P = i;
bool isCorrect = false; // 标记本题是否答对
bool hasSelected = false; // 标记本题是否有选择
for (int j = 0; j < questions[P].options.Count; j++)
{
Toggle Curtog;
if (questions[P].options[j].isOn)
{
hasSelected = true;
Curtog = questions[P].options[j];
if (Curtog == questions[P].rightToggle)
{
isCorrect = true;
totalScore += questions[P].scoreValue; // 答对加两分
Debug.Log($"第 {i + 1} 题:答对了,当前总得分: {totalScore}");
questions[P].JieXi.gameObject.SetActive(false); // 答对隐藏解析
}
else
{
Debug.Log($"第 {i + 1} 题:答错了,当前总得分: {totalScore}");
questions[P].JieXi.gameObject.SetActive(true); // 答错显示解析
}
break; // 找到所选的 Toggle 后跳出内层循环
}
questions[P].options[j].interactable = false;
}
if (!hasSelected)
{
isCorrect = false;
Debug.Log($"第 {i + 1} 题:未选择答案,答错了,当前总得分: {totalScore}");
questions[P].JieXi.gameObject.SetActive(true); // 未选择答案显示解析
}
answerResults.Add(isCorrect); // 记录本题的答题结果
}
Debug.Log($"最终总得分: {totalScore}");
ScoreController.Instance.Add("单选题", totalScore);
TiJiaoBtn.gameObject.SetActive(false);
XiaYiBuBtn.gameObject.SetActive(true);
}
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as DaTiUIData ?? new DaTiUIData();
// please add init code here
TypeEventSystem.Global.Register<OnModuleQuit>(OnModuleQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
}
protected override void OnOpen(IUIData uiData = null)
{
ReSetData();
TiJiaoBtn.onClick.RemoveAllListeners();
TiJiaoBtn.onClick.AddListener(() => { TiJiao(); });
XiaYiBuBtn.onClick.RemoveAllListeners();
XiaYiBuBtn.onClick.AddListener(() => {
StringEventSystem.Global.Send("理论考核通过");
Hide();
});
}
protected override void OnShow()
{
}
protected override void OnHide()
{
// StringEventSystem.Global.Send("理论考核通过");
}
protected override void OnClose()
{
}
public void OnModuleQuit(OnModuleQuit arg)
{
Hide();
}
}
}