using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
///
/// UI管理器 - 负责管理所有UI界面的显示、隐藏和层级
///
public class UIManager : ManagerBase
{
#region 常量和枚举
// 基础排序顺序
private const int BASE_SORTING_ORDER = 10;
#endregion
#region 字段和属性
// UI根节点
private Transform uiRoot;
// 各层级的Transform
private Transform staticTrans;
private Transform bottomTrans;
private Transform midTrans;
private Transform topTrans;
private Transform systemTrans;
// UI字典,存储所有已加载的UI,键为UI名称,值为UI实例
private Dictionary> uiDict = new Dictionary>();
// UI栈,用于管理UI的显示顺序
private Stack uiStack = new Stack();
// 当前最高的排序顺序
private int currentHighestSortingOrder = 0;
// 当前回合数,用于记录UI的使用情况
private int currentRound = 0;
// 缓存层级对应的排序顺序
private Dictionary layerSortingOrderCache = new Dictionary();
// 缓存层级对应的Transform
private Dictionary layerTransformCache = new Dictionary();
// UI实例计数器,用于为同类型UI生成唯一标识
private Dictionary uiInstanceCounter = new Dictionary();
// 上次检查时间
private float lastCheckTime = 0f;
// 检查间隔(秒)
private const float CHECK_INTERVAL = 5f;
public Action OnOpenWindow;
public Action OnCloseWindow;
#endregion
#region 初始化
///
/// 初始化UI管理器
///
public override void Init()
{
base.Init();
// 初始化UI根节点
InitUIRoot();
// 初始化缓存
layerSortingOrderCache.Clear();
layerTransformCache.Clear();
uiInstanceCounter.Clear();
Debug.Log("UI管理器初始化完成");
}
///
/// 初始化UI根节点
///
private void InitUIRoot()
{
// 查找UI根节点
GameObject root = GameObject.Find("UIRoot");
// 如果场景中没有UI根节点,则创建一个
if (root == null)
{
root = new GameObject("UIRoot");
// 添加DontDestroyOnLoad组件,确保UI根节点在场景切换时不被销毁
GameObject.DontDestroyOnLoad(root);
// 创建各层级节点
GameObject staticNode = new GameObject("Static");
GameObject bottomNode = new GameObject("Bottom");
GameObject midNode = new GameObject("Mid");
GameObject topNode = new GameObject("Top");
GameObject systemNode = new GameObject("System");
// 设置父节点
staticNode.transform.SetParent(root.transform, false);
bottomNode.transform.SetParent(root.transform, false);
midNode.transform.SetParent(root.transform, false);
topNode.transform.SetParent(root.transform, false);
systemNode.transform.SetParent(root.transform, false);
if (root == null)
{
Debug.LogError("无法找到UI根节点");
return;
}
}
uiRoot = root.transform;
uiRoot.position = Vector3.zero;
// 初始化各层级的Transform
staticTrans = uiRoot.Find("Static");
bottomTrans = uiRoot.Find("Bottom");
midTrans = uiRoot.Find("Mid");
topTrans = uiRoot.Find("Top");
systemTrans = uiRoot.Find("System");
// // 添加基础Canvas
// Canvas rootCanvas = root.AddComponent