refactor(UI):完善对于各个界面的管理逻辑,esc可以直接关闭所有的顶层UI

This commit is contained in:
2025-10-27 21:41:25 +08:00
parent 1462f4689e
commit 4951f25187
21 changed files with 18049 additions and 6374 deletions

View File

@@ -13,9 +13,12 @@ namespace Core
protected virtual void Awake()
{
if (!IsOpenOnFirstLoad) Hide();
}
protected virtual void Start()
{
if (!IsOpenOnFirstLoad) Hide();
}
/// <summary>
/// Called when the UI is shown.
/// </summary>

View File

@@ -1,3 +1,4 @@
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
@@ -17,17 +18,35 @@ namespace Core
/// </summary>
public class UIManager : MonoSingleton<UIManager>
{
public bool IsHasNonBackgroundUIActive
public bool IsHasBackgroundUIActive
{
get
{ return openedUIs.Values.Any(ui => ui.gameObject.activeSelf && ui.transform.parent != layerRoots[UILayer.Background]); }
{ return openedUIs.Values.Any(ui => ui.gameObject.activeSelf && ui.transform.parent == layerRoots[UILayer.Background]); }
}
public bool IsHasNormalUIActive
{
get
{ return openedUIs.Values.Any(ui => ui.gameObject.activeSelf && ui.transform.parent == layerRoots[UILayer.Normal]); }
}
public bool IsHasPopupUIActive
{
get
{ return openedUIs.Values.Any(ui => ui.gameObject.activeSelf && ui.transform.parent == layerRoots[UILayer.Popup]); }
}
public bool IsHasTopUIActive
{
get
{ return openedUIs.Values.Any(ui => ui.gameObject.activeSelf && ui.transform.parent == layerRoots[UILayer.Top]); }
}
private Dictionary<UILayer, Transform> layerRoots = new Dictionary<UILayer, Transform>();
private Dictionary<string, UIBase> openedUIs = new Dictionary<string, UIBase>();
private IInputManager inputManager;
public void RegisterInputManager(IInputManager inputMgr)
{
inputManager = inputMgr;
@@ -87,6 +106,26 @@ namespace Core
UpdateCursorState();
}
}
public void ClosePopupUI()
{
var popupUIs = openedUIs.Values.Where(ui => ui.gameObject.activeSelf && ui.transform.parent == layerRoots[UILayer.Popup]);
foreach (var ui in popupUIs)
{
ui.Hide();
}
UpdateCursorState();
}
public void CloseTopUI()
{
var topUIs = openedUIs.Values.Where(ui => ui.gameObject.activeSelf && ui.transform.parent == layerRoots[UILayer.Top]);
foreach (var ui in topUIs)
{
ui.Hide();
}
UpdateCursorState();
}
// 来回切换UI状态按同一个键实现UI的开关
public void SwitchUI<T>(UILayer layer = UILayer.Normal) where T : Component
@@ -125,7 +164,8 @@ namespace Core
private void UpdateCursorState()
{
if(inputManager == null) return;
bool shouldLockCursor = !IsHasNonBackgroundUIActive; //&& !isInMainMenu; // 仅在没有非Background UI且不在主菜单时锁定鼠标
bool shouldLockCursor = !(IsHasPopupUIActive || IsHasTopUIActive);
if (shouldLockCursor)
{
inputManager.SetCursorState(false, CursorLockMode.Locked);