Files
VFSUnity/Assets/Scripts/UI/UIManager.cs
T
yangbear bf926165c0 feat: 场景优化 - 清理重复对象、设置Tags/Colliders、重建Canvas UI、连线Inspector引用
- 删除顶层重复的 GameManager/ExperimentController/InteractionSystem/AnimationController
- 移除Canvas上的旧 HUDController(保留 UIManager)
- 为14个交互定位点设置Tag并添加SphereCollider
- 创建12个自定义Tag(InjuryVisual/Swelling/Pain/VitalSigns/Neuro/BandagePos1-3/Ankle/PaddingPos/SplintLeft/SplintRight)
- 完全重建Canvas UI子对象结构以匹配 UIManager 脚本
- 连线 ExperimentFlowCoordinator 和 UIManager 的所有 serialized fields
- 编译零错误,游戏画面正常显示
2026-06-24 02:44:58 +08:00

222 lines
7.0 KiB
C#

using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// UI 中央调度器:引用所有子面板,绑定 StepManager 事件。
/// 挂在 Canvas GameObject 上。使用原生 Text(非 TMP)。
/// </summary>
public class UIManager : MonoBehaviour
{
[Header("管理器引用")]
[SerializeField] private StepManager _stepManager;
[SerializeField] private InteractionSystem _interactionSystem;
[Header("=== 顶部提示栏 ===")]
[SerializeField] private GameObject _tipPanel;
[SerializeField] private Text _tipText;
[Header("=== 详细操作说明面板 ===")]
[SerializeField] private GameObject _detailPanel;
[SerializeField] private Text _detailText;
[Header("=== 对话气泡 ===")]
[SerializeField] private GameObject _dialogueBubble;
[SerializeField] private Text _dialogueText;
[Header("=== 画中画视频 ===")]
[SerializeField] private VideoController _videoController;
[Header("=== 右侧功能按钮 ===")]
[SerializeField] private Button _btnFullScreen;
[SerializeField] private Button _btnPrevious;
[SerializeField] private Button _btnNext;
[SerializeField] private Button _btnSwitchMode;
[SerializeField] private Button _btnReturn;
[SerializeField] private Text _btnSwitchModeLabel;
[Header("=== 底部进度条 ===")]
[SerializeField] private Slider _progressBar;
[SerializeField] private Text _progressText;
[Header("=== 弹窗 ===")]
[SerializeField] private ExamPopup _examPopup;
[SerializeField] private ScoreDisplay _scoreDisplay;
[Header("=== 底部图标 ===")]
[SerializeField] private Button _btnCPR;
[SerializeField] private Button _btnFracture;
[SerializeField] private Button _btnSprain;
[SerializeField] private Button _btnBleeding;
[SerializeField] private Button _btnTransport;
[Header("=== 固定方式选择(仅骨折固定) ===")]
[SerializeField] private GameObject _fixationPanel;
[SerializeField] private Button _btnLimbFixation;
[SerializeField] private Button _btnSplintFixation;
private void Start()
{
if (_stepManager == null)
_stepManager = FindObjectOfType<StepManager>();
if (_interactionSystem == null)
_interactionSystem = FindObjectOfType<InteractionSystem>();
BindEvents();
BindButtons();
}
#region 事件绑定
private void BindEvents()
{
if (_stepManager == null) return;
_stepManager.OnStepChanged.AddListener(OnStepChanged);
_stepManager.OnTipChanged.AddListener(UpdateTip);
_stepManager.OnDialogueChanged.AddListener(UpdateDialogue);
_stepManager.OnDetailedContent.AddListener(UpdateDetailContent);
_stepManager.OnExamPopup.AddListener(ShowExamPopup);
_stepManager.OnExperimentComplete.AddListener(OnExperimentComplete);
_stepManager.OnVideoToggle.AddListener(ToggleVideo);
}
private void BindButtons()
{
if (_btnFullScreen) _btnFullScreen.onClick.AddListener(ToggleFullScreen);
if (_btnPrevious) _btnPrevious.onClick.AddListener(OnPreviousClicked);
if (_btnNext) _btnNext.onClick.AddListener(OnNextClicked);
if (_btnSwitchMode) _btnSwitchMode.onClick.AddListener(OnSwitchModeClicked);
if (_btnReturn) _btnReturn.onClick.AddListener(OnReturnClicked);
if (_btnLimbFixation) _btnLimbFixation.onClick.AddListener(() => _stepManager?.SwitchFixation(FixationMethod.LimbFixation));
if (_btnSplintFixation) _btnSplintFixation.onClick.AddListener(() => _stepManager?.SwitchFixation(FixationMethod.SplintFixation));
}
#endregion
#region UI 更新回调
private void OnStepChanged(int index)
{
UpdateProgressBar();
UpdateNavigationButtons();
}
private void UpdateTip(string text)
{
if (_tipPanel != null) _tipPanel.SetActive(!string.IsNullOrEmpty(text));
if (_tipText != null) _tipText.text = text;
}
private void UpdateDialogue(string text)
{
if (_dialogueBubble != null) _dialogueBubble.SetActive(!string.IsNullOrEmpty(text));
if (_dialogueText != null) _dialogueText.text = text;
}
private void UpdateDetailContent(string text)
{
if (_detailPanel != null) _detailPanel.SetActive(!string.IsNullOrEmpty(text));
if (_detailText != null) _detailText.text = text;
}
private void ToggleVideo(string fileName, bool active)
{
if (_videoController == null) return;
if (active && !string.IsNullOrEmpty(fileName))
_videoController.PlayVideo(fileName);
else
_videoController.StopVideo();
}
private void UpdateProgressBar()
{
if (_stepManager == null) return;
int cur = _stepManager.CurrentStepIndex + 1;
int total = _stepManager.TotalSteps;
if (_progressBar != null) _progressBar.value = (float)cur / total;
if (_progressText != null) _progressText.text = cur + "/" + total;
}
private void UpdateNavigationButtons()
{
if (_stepManager == null) return;
if (_btnPrevious) _btnPrevious.interactable = _stepManager.CurrentStepIndex > 0;
if (_btnNext)
{
var label = _btnNext.GetComponentInChildren<Text>();
bool isLast = _stepManager.CurrentStepIndex >= _stepManager.TotalSteps - 1;
if (label != null) label.text = isLast ? "完成" : "下一步";
}
}
#endregion
#region 考核弹窗
private void ShowExamPopup(StepData step)
{
if (_examPopup == null) return;
_examPopup.Show(step, OnExamAnswer);
}
private void OnExamAnswer(float proportion, string feedback)
{
_stepManager?.OnStepInteractionComplete(proportion, feedback);
_stepManager?.NextStep();
}
#endregion
#region 实验完成
private void OnExperimentComplete()
{
var gm = GameManager.Instance;
if (gm == null) return;
if (gm.CurrentMode == ExperimentMode.Exam)
_scoreDisplay?.ShowExamScore();
else
_scoreDisplay?.ShowNotesOnly();
}
#endregion
#region 按钮回调
private void ToggleFullScreen()
{
Screen.fullScreen = !Screen.fullScreen;
}
private void OnPreviousClicked()
{
_stepManager?.PreviousStep();
}
private void OnNextClicked()
{
if (_stepManager != null && _stepManager.Mode == ExperimentMode.Exam)
{
var step = _stepManager.GetCurrentStep();
if (step != null && step.CompleteType != StepCompleteType.MultipleChoice)
_stepManager.OnStepInteractionComplete(1f);
}
_stepManager?.NextStep();
}
private void OnSwitchModeClicked()
{
if (_stepManager == null) return;
var newMode = _stepManager.Mode == ExperimentMode.Learn
? ExperimentMode.Exam : ExperimentMode.Learn;
_stepManager.SwitchMode(newMode);
}
private void OnReturnClicked()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("main");
}
#endregion
}