2024-10-11 10:12:15 +08:00
|
|
|
|
/********************************************************************
|
|
|
|
|
文件: 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;
|
2024-10-27 04:03:15 +08:00
|
|
|
|
frame?.OnUpdate(deltaTime);
|
2024-10-11 10:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打开界面
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="framName"></param>
|
2024-10-27 04:03:15 +08:00
|
|
|
|
public void OpenFrame(string framName, OpenFrameData openFrameData = null, Action callback = null)
|
2024-10-11 10:12:15 +08:00
|
|
|
|
{
|
|
|
|
|
Type frameType = CommonTools.GetType(framName);
|
2024-10-27 04:03:15 +08:00
|
|
|
|
OpenFrame(frameType, openFrameData, callback);
|
2024-10-11 10:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打开界面
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2024-10-27 04:03:15 +08:00
|
|
|
|
public void OpenFrame<T>(OpenFrameData openFrameData = null, Action callback = null) where T : FrameBase
|
2024-10-11 10:12:15 +08:00
|
|
|
|
{
|
2024-10-27 04:03:15 +08:00
|
|
|
|
OpenFrame(typeof(T), openFrameData, callback);
|
2024-10-11 10:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打开界面
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
/// <param name="type"></param>
|
2024-10-27 04:03:15 +08:00
|
|
|
|
private void OpenFrame(Type type, OpenFrameData openFrameData = null, Action callback = null)
|
2024-10-11 10:12:15 +08:00
|
|
|
|
{
|
2024-10-27 04:03:15 +08:00
|
|
|
|
string name = type.Name;
|
|
|
|
|
|
2024-10-11 10:12:15 +08:00
|
|
|
|
FrameBase frame;
|
|
|
|
|
if (allOpenFrames.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
frame = allOpenFrames[name];
|
2024-10-27 04:03:15 +08:00
|
|
|
|
frame.CloseFrame();
|
2024-10-11 10:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
frame = (FrameBase)Activator.CreateInstance(type);
|
|
|
|
|
allOpenFrames.Add(name, frame);
|
|
|
|
|
}
|
2024-10-27 04:03:15 +08:00
|
|
|
|
|
|
|
|
|
frame.ShowFrame(openFrameData, callback);
|
2024-10-11 10:12:15 +08:00
|
|
|
|
|
2024-10-27 04:03:15 +08:00
|
|
|
|
switch (frame.FrameData.FrameType)
|
2024-10-11 10:12:15 +08:00
|
|
|
|
{
|
|
|
|
|
case FrameType.Frame:
|
|
|
|
|
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];
|
2024-10-27 04:03:15 +08:00
|
|
|
|
frame.CloseFrame(callback);
|
2024-10-11 10:12:15 +08:00
|
|
|
|
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);
|
2024-10-27 04:03:15 +08:00
|
|
|
|
if (frame.FrameData != null && !string.IsNullOrEmpty(frame.FrameData.PrefabPath))
|
2024-10-11 10:12:15 +08:00
|
|
|
|
{
|
2024-10-27 04:03:15 +08:00
|
|
|
|
Transform parent;
|
|
|
|
|
if (frame.FrameData.OpenFrameData != null && frame.FrameData.OpenFrameData.ParentFrameData != null
|
|
|
|
|
&& frame.FrameData.OpenFrameData.ParentFrameData.parent != null)
|
|
|
|
|
{
|
|
|
|
|
parent = frame.FrameData.OpenFrameData.ParentFrameData.parent;
|
|
|
|
|
}else{
|
|
|
|
|
parent = GetTier(frame.FrameData.Tier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GameObject obj = LoaderTools.LoadGameObjectAndInstantiateAsync(frame.FrameData.PrefabPath, parent);
|
2024-10-11 10:12:15 +08:00
|
|
|
|
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
2024-10-27 04:03:15 +08:00
|
|
|
|
Debug.LogErrorFormat("加载Frame Prefab失败:{0}!", frame.FrameData.PrefabPath);
|
2024-10-11 10:12:15 +08:00
|
|
|
|
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;
|
2024-10-27 04:03:15 +08:00
|
|
|
|
|
|
|
|
|
FrameBase frameBase = allOpenFrames[name];
|
|
|
|
|
return frameBase as T;
|
2024-10-11 10:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-27 04:03:15 +08:00
|
|
|
|
/// 获取界面
|
2024-10-11 10:12:15 +08:00
|
|
|
|
/// </summary>
|
2024-10-27 04:03:15 +08:00
|
|
|
|
public FrameBase GetFrame(string frameName)
|
2024-10-11 10:12:15 +08:00
|
|
|
|
{
|
2024-10-27 04:03:15 +08:00
|
|
|
|
return allOpenFrames.GetValueOrDefault(frameName);
|
2024-10-11 10:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-27 04:03:15 +08:00
|
|
|
|
/// 是否开启
|
2024-10-11 10:12:15 +08:00
|
|
|
|
/// </summary>
|
2024-10-27 04:03:15 +08:00
|
|
|
|
public bool IsOpen<T>() where T : FrameBase
|
2024-10-11 10:12:15 +08:00
|
|
|
|
{
|
2024-10-27 04:03:15 +08:00
|
|
|
|
return GetFrame<T>() != null;
|
2024-10-11 10:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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:
|
2024-10-27 04:03:15 +08:00
|
|
|
|
frameOpenStack.Remove(name);
|
2024-10-11 10:12:15 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 清理
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void Clear()
|
|
|
|
|
{
|
|
|
|
|
tierList.Clear();
|
|
|
|
|
frameOpenStack.Clear();
|
|
|
|
|
foreach (var frameNodePair in allOpenFrames)
|
|
|
|
|
{
|
|
|
|
|
frameNodePair.Value.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|