fix():修复重启时花屏效果未关闭的BUG
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
namespace Core
|
||||
{
|
||||
@@ -13,6 +14,11 @@ namespace Core
|
||||
UIManager.Instance.RegisterLayer(layer, transform);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
UIManager.Instance.UnRegisterLayer(layer);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,30 +21,42 @@ namespace Core
|
||||
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]); }
|
||||
{
|
||||
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]); }
|
||||
{
|
||||
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]); }
|
||||
{
|
||||
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)
|
||||
@@ -72,6 +84,22 @@ namespace Core
|
||||
}
|
||||
}
|
||||
|
||||
public void UnRegisterLayer(UILayer layer)
|
||||
{
|
||||
// 先移除该层级下的UI注册
|
||||
var uiToRemove = openedUIs.Values.Where(ui => ui.transform.parent == layerRoots[layer]).ToList();
|
||||
foreach (var ui in uiToRemove)
|
||||
{
|
||||
openedUIs.Remove(ui.gameObject.name);
|
||||
}
|
||||
|
||||
// 然后移除层级根节点
|
||||
if (layerRoots.ContainsKey(layer))
|
||||
{
|
||||
layerRoots.Remove(layer);
|
||||
}
|
||||
}
|
||||
|
||||
public T OpenUI<T>(UILayer layer = UILayer.Normal) where T : Component
|
||||
{
|
||||
string uiName = typeof(T).Name;
|
||||
@@ -81,6 +109,7 @@ namespace Core
|
||||
UpdateCursorState();
|
||||
return openedUIs[uiName] as T;
|
||||
}
|
||||
|
||||
// 加载UI预制体
|
||||
GameObject prefab = Resources.Load<GameObject>("UI/" + uiName); // 从 Resources/UI 文件夹加载UI预制体
|
||||
if (prefab == null)
|
||||
@@ -106,24 +135,28 @@ namespace Core
|
||||
UpdateCursorState();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void ClosePopupUI()
|
||||
{
|
||||
var popupUIs = openedUIs.Values.Where(ui => ui.gameObject.activeSelf && ui.transform.parent == layerRoots[UILayer.Popup]);
|
||||
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]);
|
||||
var topUIs = openedUIs.Values.Where(ui =>
|
||||
ui.gameObject.activeSelf && ui.transform.parent == layerRoots[UILayer.Top]);
|
||||
foreach (var ui in topUIs)
|
||||
{
|
||||
ui.Hide();
|
||||
}
|
||||
|
||||
UpdateCursorState();
|
||||
}
|
||||
|
||||
@@ -155,17 +188,19 @@ namespace Core
|
||||
if (uiBase != null)
|
||||
uiList.Add(uiBase.gameObject);
|
||||
}
|
||||
|
||||
result[layer] = uiList;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private void UpdateCursorState()
|
||||
{
|
||||
if(inputManager == null) return;
|
||||
|
||||
bool shouldLockCursor = !(IsHasPopupUIActive || IsHasTopUIActive);
|
||||
if (inputManager == null) return;
|
||||
|
||||
bool shouldLockCursor = !(IsHasPopupUIActive || IsHasTopUIActive);
|
||||
if (shouldLockCursor)
|
||||
{
|
||||
inputManager.SetCursorState(false, CursorLockMode.Locked);
|
||||
@@ -173,8 +208,8 @@ namespace Core
|
||||
else
|
||||
{
|
||||
inputManager.SetCursorState(true, CursorLockMode.None);
|
||||
|
||||
}
|
||||
|
||||
inputManager.SetInputForLook(shouldLockCursor);
|
||||
inputManager.SetInputForMove(shouldLockCursor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user