Files
VFSUnity/Assets/Scripts/UI/ScoreDisplay.cs
T

110 lines
3.9 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.UI;
using TMPro;
/// <summary>
/// 实验结束后的结果展示面板。
/// 考核模式:显示加权成绩 + 错题分析列表(含每步得分/满分)。
/// 学习模式:显示注意事项。
/// </summary>
public class ScoreDisplay : MonoBehaviour
{
[Header("面板根节点")]
[SerializeField] private GameObject _panelRoot;
[Header("考核模式")]
[SerializeField] private GameObject _examResultRoot;
[SerializeField] private TMP_Text _scoreTitle;
[SerializeField] private TMP_Text _scoreValue;
[SerializeField] private Transform _analysisListParent;
[SerializeField] private GameObject _analysisItemPrefab;
[Header("学习模式 - 注意事项")]
[SerializeField] private GameObject _notesRoot;
[SerializeField] private TMP_Text _notesText;
[Header("底部按钮")]
[SerializeField] private Button _confirmButton;
private static readonly string NotesContent =
"1. 下肢损伤选择仰卧位;\n" +
"2. 骨折固定前后要脱掉鞋袜并检查下肢末梢循环、运动、感觉;\n" +
"3. 穿带子时应从健侧穿向伤侧;\n" +
"4. 骨折固定时先固定伤口的近心端,后固定远心端,再由上而下进行固定;\n" +
"5. 固定后尽可能抬高下肢。";
private void Awake()
{
_confirmButton?.onClick.AddListener(OnConfirm);
if (_panelRoot != null) _panelRoot.SetActive(false);
}
/// <summary>考核模式 - 显示加权成绩</summary>
public void ShowExamScore()
{
if (_panelRoot != null) _panelRoot.SetActive(true);
if (_examResultRoot != null) _examResultRoot.SetActive(true);
if (_notesRoot != null) _notesRoot.SetActive(false);
var gm = GameManager.Instance;
if (gm == null) return;
int score = gm.GetExamScoreRounded();
float pct = gm.GetExamPercentage();
if (_scoreTitle != null) _scoreTitle.text = "成绩显示栏";
if (_scoreValue != null) _scoreValue.text = $"考试成绩:{score} 分({pct:F0}%";
BuildAnalysisList();
}
/// <summary>学习模式 - 显示注意事项</summary>
public void ShowNotesOnly()
{
if (_panelRoot != null) _panelRoot.SetActive(true);
if (_examResultRoot != null) _examResultRoot.SetActive(false);
if (_notesRoot != null) _notesRoot.SetActive(true);
if (_notesText != null) _notesText.text = NotesContent;
}
private void BuildAnalysisList()
{
if (_analysisListParent == null || _analysisItemPrefab == null) return;
foreach (Transform child in _analysisListParent)
Destroy(child.gameObject);
var gm = GameManager.Instance;
if (gm == null || gm.StepScores == null) return;
for (int i = 0; i < gm.StepScores.Length; i++)
{
var item = Instantiate(_analysisItemPrefab, _analysisListParent);
var text = item.GetComponentInChildren<TMP_Text>();
if (text == null) continue;
float earned = gm.StepScores[i];
float max = gm.StepMaxScores[i];
bool fullScore = Mathf.Approximately(earned, max);
bool zeroScore = earned <= 0f;
string status;
if (fullScore) status = "<color=#00FF00>正确</color>";
else if (zeroScore) status = "<color=#FF0000>错误</color>";
else status = $"<color=#FFAA00>部分正确</color>";
string scorePart = $"{earned:F0}/{max:F0} 分)";
string fbPart = (!fullScore && !string.IsNullOrEmpty(gm.StepFeedback[i]))
? $"{gm.StepFeedback[i]}" : "";
text.text = $"第{i + 1}题:{status} {scorePart}{fbPart}";
}
}
private void OnConfirm()
{
if (_panelRoot != null) _panelRoot.SetActive(false);
UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
}
}