using System;
using System.Collections.Generic;
using UnityEngine;
///
/// UI面板类型,用于确定面板的层级和行为
///
public enum UIPanelType
{
Base, // 基础面板(主界面等)
Normal, // 普通面板(功能面板)
Popup, // 弹出面板(用户管理等)
Dialog, // 对话框(确认弹窗等)
System // 系统面板(警报覆盖等),最高优先级
}
///
/// UI面板信息
///
public class UIPanelInfo
{
public BasePanel Panel { get; set; }
public UIPanelType Type { get; set; }
public int Layer { get; set; }
public bool IsModal { get; set; } // 是否为模态面板
public string PanelName { get; set; }
public UIPanelInfo(BasePanel panel, UIPanelType type, bool isModal = false)
{
Panel = panel;
Type = type;
IsModal = isModal;
PanelName = panel.GetType().Name;
}
}
///
/// 增强版UI管理器
/// 使用栈管理面板导航,支持层级管理和优雅的返回逻辑
///
public class EnhancedUIManager : MonoBehaviour
{
#region 单例
private static EnhancedUIManager _instance;
public static EnhancedUIManager Instance
{
get
{
if (_instance == null)
{
var go = new GameObject("EnhancedUIManager");
_instance = go.AddComponent();
DontDestroyOnLoad(go);
}
return _instance;
}
}
#endregion
#region 字段和属性
[Header("UI容器")]
public Transform CanvasRoot;
public Transform BaseLayer; // 基础层(主界面)
public Transform NormalLayer; // 普通层(功能面板)
public Transform PopupLayer; // 弹窗层(用户管理)
public Transform DialogLayer; // 对话框层(确认弹窗)
public Transform SystemLayer; // 系统层(警报覆盖)
// 面板管理
private Dictionary _allPanels = new Dictionary();
private Stack _navigationStack = new Stack();
private List _activeDialogs = new List();
private UIPanelInfo _currentBasePanel;
// 层级管理
private readonly Dictionary _baseLayerValues = new Dictionary
{
{ UIPanelType.Base, 0 },
{ UIPanelType.Normal, 100 },
{ UIPanelType.Popup, 200 },
{ UIPanelType.Dialog, 300 },
{ UIPanelType.System, 400 }
};
private int _nextLayerIncrement = 0;
#endregion
#region 初始化
void Awake()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(gameObject);
InitializeLayers();
}
else if (_instance != this)
{
Destroy(gameObject);
}
}
private void InitializeLayers()
{
// 查找或创建Canvas
if (CanvasRoot == null)
{
var canvas = FindObjectOfType