324 lines
9.8 KiB
C#
324 lines
9.8 KiB
C#
/********************************************************************
|
||
文件: UIManager.cs
|
||
作者: 梦语
|
||
邮箱: 1982614048@qq.com
|
||
创建时间: 2024/03/29 17:28:19
|
||
最后修改: 梦语
|
||
最后修改时间: 2024/04/03 15:10:11
|
||
功能: 界面管理类
|
||
*********************************************************************/
|
||
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using UnityEngine.Accessibility;
|
||
using UnityEngine.UI;
|
||
|
||
namespace Ether
|
||
{
|
||
public class UIManager : SingletonAutoMono<UIManager>
|
||
{
|
||
[HideInInspector]
|
||
public GameObject MainCanvas; //主画布
|
||
List<Transform> tierList = new List<Transform>(); //所有的层级
|
||
//所有打开的界面
|
||
private DictionaryEx<string, FrameBase> allOpenFrames = new DictionaryEx<string, FrameBase>();
|
||
|
||
private LinkedSequence<string> frameOpenStack = new LinkedSequence<string>();
|
||
|
||
public override void Init()
|
||
{
|
||
Clear();
|
||
MainCanvas = EntityManager.Inst.GetEntity("MainCanvas").gameObject;
|
||
CheckMainCanvasLayer();
|
||
}
|
||
|
||
#region 层级管理
|
||
|
||
/// <summary>
|
||
/// 检查MainCanvas下的层级
|
||
/// </summary>
|
||
private void CheckMainCanvasLayer()
|
||
{
|
||
tierList.Clear();
|
||
//获取当前Canvas下所有的物体
|
||
int childNum = MainCanvas.transform.childCount;
|
||
Transform[] childTrans = new Transform[childNum];
|
||
|
||
for (int i = 0; i < childNum; i++)
|
||
{
|
||
childTrans[i] = MainCanvas.transform.GetChild(i);
|
||
}
|
||
|
||
//动态创建UI层级
|
||
string[] frameLayers = CommonTools.GetAllEnumName<FrameTier>();
|
||
foreach (string layer in frameLayers)
|
||
{
|
||
if (string.IsNullOrEmpty(layer))
|
||
{
|
||
continue;
|
||
}
|
||
Transform trans = MainCanvas.transform.Find(layer);
|
||
if (trans == null)
|
||
{
|
||
trans = new GameObject(layer, typeof(RectTransform)).transform;
|
||
trans.SetParent(MainCanvas.transform, false);
|
||
RectTransform rectTrans = trans.GetComponent<RectTransform>();
|
||
rectTrans.anchorMin = new Vector2(0, 0);
|
||
rectTrans.anchorMax = new Vector2(1, 1);
|
||
rectTrans.anchoredPosition = Vector2.zero;
|
||
rectTrans.sizeDelta = Vector2.zero;
|
||
}
|
||
tierList.Add(trans);
|
||
trans.SetAsLastSibling();
|
||
}
|
||
|
||
//设置Canvas下之前的物体层级到最上层
|
||
foreach (Transform trans in childTrans)
|
||
{
|
||
trans.SetAsLastSibling();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取层级
|
||
/// </summary>
|
||
public Transform GetTier(int tierIndex)
|
||
{
|
||
if (tierList.Count <= tierIndex)
|
||
{
|
||
Debug.LogError($"没有该层级:{tierIndex},请检查配置");
|
||
return null;
|
||
}
|
||
return tierList[tierIndex];
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取层级
|
||
/// </summary>
|
||
public Transform GetTier(FrameTier tier)
|
||
{
|
||
int tierIndex = (int)tier;
|
||
if (tierList.Count <= tierIndex)
|
||
{
|
||
Debug.LogError($"没有该层级:{tier},请检查配置");
|
||
return null;
|
||
}
|
||
return tierList[tierIndex];
|
||
}
|
||
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 循环
|
||
/// </summary>
|
||
private void Update()
|
||
{
|
||
float deltaTime = Time.deltaTime;
|
||
foreach (var viewPair in allOpenFrames)
|
||
{
|
||
FrameBase frame = viewPair.Value;
|
||
frame?.OnUpdate(deltaTime);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开界面
|
||
/// </summary>
|
||
/// <param name="framName"></param>
|
||
public void OpenFrame(string framName, OpenFrameData openFrameData = null, Action callback = null)
|
||
{
|
||
Type frameType = CommonTools.GetType(framName);
|
||
OpenFrame(frameType, openFrameData, callback);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开界面
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
public void OpenFrame<T>(OpenFrameData openFrameData = null, Action callback = null) where T : FrameBase
|
||
{
|
||
OpenFrame(typeof(T), openFrameData, callback);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开界面
|
||
/// </summary>
|
||
/// <param name="name"></param>
|
||
/// <param name="type"></param>
|
||
private void OpenFrame(Type type, OpenFrameData openFrameData = null, Action callback = null)
|
||
{
|
||
string name = type.Name;
|
||
|
||
FrameBase frame;
|
||
if (allOpenFrames.ContainsKey(name))
|
||
{
|
||
frame = allOpenFrames[name];
|
||
frame.CloseFrame();
|
||
}
|
||
else
|
||
{
|
||
frame = (FrameBase)Activator.CreateInstance(type);
|
||
allOpenFrames.Add(name, frame);
|
||
}
|
||
|
||
frame.ShowFrame(openFrameData, callback);
|
||
|
||
switch (frame.FrameData.FrameType)
|
||
{
|
||
case FrameType.Frame:
|
||
if (openFrameData != null && openFrameData.ParentFrameData != null)
|
||
{
|
||
break;
|
||
}
|
||
frameOpenStack.Add(name);
|
||
string str = "当前打开的界面 :";
|
||
foreach (var node in frameOpenStack)
|
||
{
|
||
str += node.Value + " ";
|
||
}
|
||
Debug.Log(str);
|
||
|
||
break;
|
||
case FrameType.Pop:
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 关闭界面
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
public void CloseFrame<T>(Action callback = null) where T : FrameBase
|
||
{
|
||
string name = typeof(T).Name;
|
||
CloseFrame(name, callback);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 关闭界面
|
||
/// </summary>
|
||
/// <param name="frameName"></param>
|
||
public void CloseFrame(string frameName, Action callback = null)
|
||
{
|
||
if (allOpenFrames.ContainsKey(frameName))
|
||
{
|
||
FrameBase frame = allOpenFrames[frameName];
|
||
frame.CloseFrame(callback);
|
||
allOpenFrames.Remove(frameName);
|
||
}
|
||
|
||
frameOpenStack.Remove(frameName);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取界面资源
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="frame"></param>
|
||
/// <returns></returns>
|
||
public GameObject GetFrameRes<T>(T frame) where T : FrameBase
|
||
{
|
||
Type viewType = typeof(T);
|
||
if (frame.FrameData != null && !string.IsNullOrEmpty(frame.FrameData.PrefabPath))
|
||
{
|
||
Transform parent;
|
||
if (frame.OpenFrameData != null && frame.OpenFrameData.ParentFrameData != null
|
||
&& frame.OpenFrameData.ParentFrameData.parent != null)
|
||
{
|
||
parent = frame.OpenFrameData.ParentFrameData.parent;
|
||
}else{
|
||
parent = GetTier(frame.FrameData.Tier);
|
||
}
|
||
|
||
GameObject obj = LoaderTools.LoadGameObjectAndInstantiateAsync(frame.FrameData.PrefabPath, parent);
|
||
|
||
if (obj == null)
|
||
{
|
||
Debug.LogErrorFormat("加载Frame Prefab失败:{0}!", frame.FrameData.PrefabPath);
|
||
return null;
|
||
}
|
||
|
||
obj.AddComponent<Canvas>();
|
||
obj.AddComponent<CanvasGroup>();
|
||
obj.AddComponent<GraphicRaycaster>();
|
||
|
||
return obj;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogErrorFormat("当前Frame:{0}没有对应的Prefab路径,请添加入配置!", viewType);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取界面
|
||
/// </summary>
|
||
public T GetFrame<T>() where T : FrameBase
|
||
{
|
||
string name = typeof(T).Name;
|
||
|
||
FrameBase frameBase = allOpenFrames[name];
|
||
return frameBase as T;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取界面
|
||
/// </summary>
|
||
public FrameBase GetFrame(string frameName)
|
||
{
|
||
return allOpenFrames.GetValueOrDefault(frameName);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否开启
|
||
/// </summary>
|
||
public bool IsOpen<T>() where T : FrameBase
|
||
{
|
||
return GetFrame<T>() != null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回
|
||
/// </summary>
|
||
public void BackFrame(string name, FrameType frameType)
|
||
{
|
||
switch (frameType)
|
||
{
|
||
case FrameType.Frame:
|
||
frameOpenStack.Remove(name);
|
||
if (frameOpenStack.Count > 0)
|
||
{
|
||
OpenFrame(frameOpenStack.Last().Value);
|
||
}
|
||
break;
|
||
case FrameType.Pop:
|
||
frameOpenStack.Remove(name);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清理
|
||
/// </summary>
|
||
public override void Clear()
|
||
{
|
||
tierList.Clear();
|
||
frameOpenStack.Clear();
|
||
foreach (var frameNodePair in allOpenFrames)
|
||
{
|
||
frameNodePair.Value.Close();
|
||
}
|
||
}
|
||
}
|
||
}
|