231 lines
6.2 KiB
C#
231 lines
6.2 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using System.Linq;
|
||
using OpenCVForUnity.CoreModule;
|
||
using UnityEngine.SceneManagement;
|
||
using Yoga;
|
||
|
||
public class UIManager : MonoSingleton<UIManager>
|
||
{
|
||
private Dictionary<Type, UIBase> _panelDic = new Dictionary<Type, UIBase>();
|
||
private Stack<UIBase> _panelStack = new Stack<UIBase>();
|
||
|
||
public Camera UiCamera { get; set; }
|
||
|
||
private RectTransform _uiPanelRoot;
|
||
private RectTransform _uiPopRoot;
|
||
private RectTransform _hudRoot;
|
||
|
||
private Transform _uiRootTrans;
|
||
|
||
public override void Init()
|
||
{
|
||
//查找当前scene的摄像机
|
||
UiCamera = Camera.main;
|
||
|
||
var canvases = GameObject.FindGameObjectsWithTag("UI");
|
||
GameObject canvas;
|
||
if (canvases.Length <= 0)
|
||
{
|
||
canvas = new GameObject("UICanvas");
|
||
canvas.AddComponent<Canvas>();
|
||
}
|
||
else
|
||
canvas = canvases.ToList().Find(canvas => canvas.name == "UICanvas");
|
||
|
||
if (canvas == null)
|
||
{
|
||
canvas = new GameObject("UICanvas");
|
||
canvas.AddComponent<Canvas>();
|
||
}
|
||
|
||
_uiRootTrans = canvas.transform;
|
||
|
||
//优先层级 最高 hud 最低 panel
|
||
//panel 挂载节点
|
||
if (transform.Find("PanelRoot") == null)
|
||
{
|
||
_uiPanelRoot = new GameObject("PanelRoot").AddComponent<RectTransform>();
|
||
_uiPanelRoot.SetParent(_uiRootTrans);
|
||
_uiPanelRoot.anchoredPosition = Vector2.zero;
|
||
_uiPanelRoot.anchorMin = Vector2.zero;
|
||
_uiPanelRoot.anchorMax = Vector2.one;
|
||
_uiPanelRoot.offsetMin = Vector2.zero;
|
||
_uiPanelRoot.offsetMax = Vector2.zero;
|
||
}
|
||
else
|
||
_uiPanelRoot = transform.Find("PanelRoot").GetComponent<RectTransform>();
|
||
|
||
_uiPanelRoot.SetSiblingIndex(1);
|
||
|
||
//pop 挂载节点
|
||
if (transform.Find("PopRoot") == null)
|
||
{
|
||
_uiPopRoot = new GameObject("PopRoot").AddComponent<RectTransform>();
|
||
_uiPopRoot.SetParent(_uiRootTrans);
|
||
_uiPopRoot.anchoredPosition = Vector2.zero;
|
||
_uiPopRoot.anchorMin = Vector2.zero;
|
||
_uiPopRoot.anchorMax = Vector2.one;
|
||
_uiPopRoot.offsetMin = Vector2.zero;
|
||
_uiPopRoot.offsetMax = Vector2.zero;
|
||
}
|
||
else
|
||
_uiPopRoot = transform.Find("PopRoot").GetComponent<RectTransform>();
|
||
|
||
_uiPopRoot.SetSiblingIndex(2);
|
||
|
||
//hud 挂载节点
|
||
if (transform.Find("HUDRoot") == null)
|
||
{
|
||
_hudRoot = new GameObject("HUDRoot").AddComponent<RectTransform>();
|
||
_hudRoot.SetParent(_uiRootTrans);
|
||
_hudRoot.anchoredPosition = Vector2.zero;
|
||
_hudRoot.anchorMin = Vector2.zero;
|
||
_hudRoot.anchorMax = Vector2.one;
|
||
_hudRoot.offsetMin = Vector2.zero;
|
||
_hudRoot.offsetMax = Vector2.zero;
|
||
}
|
||
else
|
||
_hudRoot = transform.Find("HUDRoot").GetComponent<RectTransform>();
|
||
_hudRoot.SetSiblingIndex(3);
|
||
}
|
||
|
||
private T GetPanel<T>() where T : UIBase
|
||
{
|
||
Type type = typeof(T);
|
||
//无缓存,加载相对应的Panel文件
|
||
if (_panelDic.TryGetValue(type, out var panel))
|
||
{
|
||
if (panel != null)
|
||
_panelDic[type].Destroy();
|
||
_panelDic.Remove(type);
|
||
}
|
||
|
||
string path = UILoadConfig.GetPath(type.FullName);
|
||
GameObject panelGo = Instantiate(Resources.Load<GameObject>(path));
|
||
panel = panelGo.GetComponent<T>();
|
||
if (panel is UIPanelBase)
|
||
panelGo.transform.SetParent(_uiPanelRoot);
|
||
else if (panel is UIPopupBase)
|
||
panelGo.transform.SetParent(_uiPopRoot);
|
||
else if (panel is UIHudBase)
|
||
panelGo.transform.SetParent(_hudRoot);
|
||
else
|
||
throw new Exception("UIManager.GetPanel() : panel is not a type of UIPanelBase, UIPopupBase or UIHudBase");
|
||
|
||
panelGo.transform.localPosition = Vector3.zero;
|
||
|
||
_panelDic.Add(type, panel);
|
||
return panel as T;
|
||
}
|
||
|
||
public void ShowPanel<T>(params object[] args) where T : UIBase
|
||
{
|
||
T panel = GetPanel<T>();
|
||
panel.Init(args);
|
||
panel.gameObject.SetActive(true);
|
||
panel.OnEnter();
|
||
_panelStack.Push(panel);
|
||
}
|
||
|
||
public void ShowPanel<T>(bool hideCurrentPage, params object[] args) where T : UIBase
|
||
{
|
||
if (hideCurrentPage)
|
||
{
|
||
if (_panelStack.Count > 0)
|
||
{
|
||
var topPanel = _panelStack.Peek();
|
||
topPanel.OnPause();
|
||
}
|
||
}
|
||
|
||
ShowPanel<T>(args);
|
||
}
|
||
|
||
public void ClearPanel()
|
||
{
|
||
foreach (var panel in _panelDic.Values)
|
||
{
|
||
panel.OnExit();
|
||
}
|
||
_panelStack.Clear();
|
||
}
|
||
|
||
public void PushPanel<T>() where T : UIBase
|
||
{
|
||
if (_panelStack.Count > 0)
|
||
{
|
||
var topPanel = _panelStack.Peek();
|
||
topPanel.OnPause();
|
||
}
|
||
|
||
var panel = GetPanel<T>();
|
||
_panelStack.Push(panel);
|
||
panel.OnEnter();
|
||
}
|
||
|
||
private void PopPanel()
|
||
{
|
||
if (_panelStack.Count <= 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
UIBase topPanel = _panelStack.Pop();
|
||
topPanel.OnHide();
|
||
topPanel.OnExit();
|
||
|
||
if (_panelStack.Count > 0)
|
||
{
|
||
UIBase nextPanel = _panelStack.Peek();
|
||
nextPanel.OnResume();
|
||
}
|
||
}
|
||
|
||
public void CloseCurrent()
|
||
{
|
||
PopPanel();
|
||
}
|
||
|
||
public void LoadScene()
|
||
{
|
||
EnableScreenMask();
|
||
var op = SceneManager.LoadSceneAsync("YogaMain");
|
||
op.completed += (operation) =>
|
||
{
|
||
DisableScreenMask();
|
||
};
|
||
}
|
||
|
||
public void EnableScreenMask()
|
||
{
|
||
|
||
}
|
||
|
||
public void DisableScreenMask()
|
||
{
|
||
|
||
}
|
||
|
||
public void CloseUI(UIBase targetUI)
|
||
{
|
||
if (_panelStack.Count <= 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
targetUI.OnHide();
|
||
targetUI.OnExit();
|
||
//从栈中移除
|
||
_panelStack = new Stack<UIBase>(_panelStack.Where(ui => ui != targetUI));
|
||
//打开顶层页面
|
||
if (_panelStack.Count > 0)
|
||
{
|
||
UIBase nextPanel = _panelStack.Peek();
|
||
nextPanel.OnResume();
|
||
}
|
||
}
|
||
}
|