chore: initial commit
This commit is contained in:
81
Assets/Scripts/UI/UIManager.cs
Normal file
81
Assets/Scripts/UI/UIManager.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BaseGames.Core;
|
||||
using BaseGames.Core.Events;
|
||||
|
||||
namespace BaseGames.UI
|
||||
{
|
||||
[DefaultExecutionOrder(+50)]
|
||||
public class UIManager : MonoBehaviour
|
||||
{
|
||||
[Header("Canvas Roots")]
|
||||
[SerializeField] private GameObject _hudRoot;
|
||||
[SerializeField] private GameObject _pauseMenuRoot;
|
||||
[SerializeField] private GameObject _deathScreenRoot;
|
||||
[SerializeField] private GameObject _settingsRoot;
|
||||
[SerializeField] private GameObject _mapRoot;
|
||||
[SerializeField] private GameObject _shopRoot;
|
||||
|
||||
[Header("Event Channels")]
|
||||
[SerializeField] private GameStateEventChannelSO _onGameStateChanged;
|
||||
[SerializeField] private VoidEventChannelSO _onPauseRequested;
|
||||
[SerializeField] private VoidEventChannelSO _onFastTravelOpen;
|
||||
[SerializeField] private StringEventChannelSO _onShopOpen;
|
||||
[SerializeField] private VoidEventChannelSO _onMapOpen;
|
||||
|
||||
private readonly Stack<GameObject> _panelStack = new();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (_onGameStateChanged != null) _onGameStateChanged.OnEventRaised += HandleGameStateChanged;
|
||||
if (_onPauseRequested != null) _onPauseRequested.OnEventRaised += TogglePause;
|
||||
if (_onFastTravelOpen != null) _onFastTravelOpen.OnEventRaised += OpenMap;
|
||||
if (_onShopOpen != null) _onShopOpen.OnEventRaised += OpenShop;
|
||||
if (_onMapOpen != null) _onMapOpen.OnEventRaised += OpenMap;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (_onGameStateChanged != null) _onGameStateChanged.OnEventRaised -= HandleGameStateChanged;
|
||||
if (_onPauseRequested != null) _onPauseRequested.OnEventRaised -= TogglePause;
|
||||
if (_onFastTravelOpen != null) _onFastTravelOpen.OnEventRaised -= OpenMap;
|
||||
if (_onShopOpen != null) _onShopOpen.OnEventRaised -= OpenShop;
|
||||
if (_onMapOpen != null) _onMapOpen.OnEventRaised -= OpenMap;
|
||||
}
|
||||
|
||||
private void HandleGameStateChanged(GameStateId state)
|
||||
{
|
||||
// GameStateId 是 struct,用 if/else 而非 switch
|
||||
bool showHud = state == GameStates.Gameplay || state == GameStates.BossFight;
|
||||
if (_hudRoot != null) _hudRoot.SetActive(showHud);
|
||||
|
||||
if (state == GameStates.Dead)
|
||||
{
|
||||
if (_deathScreenRoot != null) _deathScreenRoot.SetActive(true);
|
||||
}
|
||||
else if (state == GameStates.Cutscene)
|
||||
{
|
||||
if (_hudRoot != null) _hudRoot.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void OpenPanel(GameObject panel)
|
||||
{
|
||||
if (panel == null) return;
|
||||
if (_panelStack.Count > 0) _panelStack.Peek().SetActive(false);
|
||||
panel.SetActive(true);
|
||||
_panelStack.Push(panel);
|
||||
}
|
||||
|
||||
public void CloseTopPanel()
|
||||
{
|
||||
if (_panelStack.Count == 0) return;
|
||||
_panelStack.Pop().SetActive(false);
|
||||
if (_panelStack.Count > 0) _panelStack.Peek().SetActive(true);
|
||||
}
|
||||
|
||||
private void TogglePause() => OpenPanel(_pauseMenuRoot);
|
||||
private void OpenShop(string _) => OpenPanel(_shopRoot);
|
||||
private void OpenMap() => OpenPanel(_mapRoot);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user