82 lines
3.0 KiB
C#
82 lines
3.0 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.EventSystems;
|
||
using BaseGames.Core;
|
||
using BaseGames.Core.Assets;
|
||
using BaseGames.Core.Events;
|
||
|
||
namespace BaseGames.UI
|
||
{
|
||
/// <summary>
|
||
/// 暂停菜单控制器(架构 10_UIModule §5)。
|
||
/// 挂载在 Canvas_Menu → PauseMenuPanel GameObject 上。
|
||
/// 按钮绑定在 Awake 中完成;由 UIManager 负责面板开关。
|
||
/// </summary>
|
||
public class PauseMenuController : MonoBehaviour, IFocusable
|
||
{
|
||
// UIManager 通过 ServiceLocator 解析,开启时自动获取,无需 Inspector 直接绑定具体类型
|
||
private IUIManager _uiManager;
|
||
|
||
[Header("按钮引用")]
|
||
[SerializeField] private Button _btnResume;
|
||
[SerializeField] private Button _btnSettings;
|
||
[SerializeField] private Button _btnMainMenu;
|
||
[SerializeField] private Button _btnQuit;
|
||
|
||
[Header("Event Channels")]
|
||
[SerializeField] private VoidEventChannelSO _onResumeRequested;
|
||
[SerializeField] private SceneLoadRequestEventChannelSO _onSceneLoadRequest;
|
||
|
||
private void Awake()
|
||
{
|
||
_btnResume?.onClick.AddListener(Resume);
|
||
_btnSettings?.onClick.AddListener(OpenSettings);
|
||
_btnMainMenu?.onClick.AddListener(GoToMainMenu);
|
||
_btnQuit?.onClick.AddListener(Application.Quit);
|
||
}
|
||
|
||
private void OnEnable()
|
||
{
|
||
// 暂停面板由 UIManager 开启,此时 ServiceLocator 已就绪
|
||
_uiManager = ServiceLocator.GetOrDefault<IUIManager>();
|
||
// 手柄导航:打开时将焦点置于第一个按钮
|
||
EventSystem.current?.SetSelectedGameObject(_btnResume?.gameObject);
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
_uiManager = null;
|
||
}
|
||
|
||
// ── 按钮回调 ──────────────────────────────────────────────────────────
|
||
|
||
private void Resume()
|
||
{
|
||
_onResumeRequested?.Raise();
|
||
_uiManager?.CloseTopPanel();
|
||
}
|
||
|
||
private void OpenSettings()
|
||
{
|
||
_uiManager?.OpenPanel(PanelId.Settings);
|
||
}
|
||
|
||
private void GoToMainMenu()
|
||
{
|
||
_uiManager?.CloseTopPanel();
|
||
_onSceneLoadRequest?.Raise(new SceneLoadRequest
|
||
{
|
||
SceneName = AddressKeys.SceneMainMenu,
|
||
TransitionType = TransitionType.Scene,
|
||
ShowLoadingScreen = false,
|
||
});
|
||
}
|
||
|
||
// ── IFocusable ────────────────────────────────────────────────────────
|
||
|
||
/// <summary>面板恢复为栈顶时(关闭子面板后)自动移回第一个按鈕。</summary>
|
||
public void OnFocusRestored()
|
||
=> EventSystem.current?.SetSelectedGameObject(_btnResume?.gameObject);
|
||
}
|
||
}
|