UImanager查询软件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// UI管理器 - 负责管理多个UI面板的显示和切换
/// 使用单例模式,提供面板间的层级导航功能
/// </summary>
public class UIManager : MonoBehaviour
{
    // 单例实例,确保整个游戏中只有一个UIManager
    public static UIManager Instance { get; private set; }

    [Header("面板")]
    public GameObject Main;        // 主面板
    public GameObject One_Panel;   // 面板一
    public GameObject Two_Panel;   // 面板二
    public GameObject Three_Panel; // 面板三
    public GameObject Four_Panel;  // 面板四

    [Header("打开按钮")]
    public Button open_one;   // 打开面板一的按钮
    public Button open_two;   // 打开面板二的按钮
    public Button open_three; // 打开面板三的按钮
    public Button open_four;  // 打开面板四的按钮

    [Header("返回按钮")]
    public Button back_one;   // 面板一的返回按钮
    public Button back_two;   // 面板二的返回按钮
    public Button back_three; // 面板三的返回按钮
    public Button back_four;  // 面板四的返回按钮

    // 面板历史列表,用于记录面板的打开顺序,实现返回功能
    private List<GameObject> panelList = new List<GameObject>();

    /// <summary>
    /// 初始化单例模式
    /// 如果已存在实例则销毁当前对象,确保只有一个UIManager
    /// </summary>
    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject); // 保持对象不被销毁
        }
        else
        {
            Destroy(gameObject);
        }
    }

    /// <summary>
    /// 初始化UI事件监听
    /// 为所有按钮添加点击事件
    /// </summary>
    void Start()
    {
        // 为打开按钮添加点击事件监听
        if (open_one != null) open_one.onClick.AddListener(() => OpenPanel(One_Panel));
        if (open_two != null) open_two.onClick.AddListener(() => OpenPanel(Two_Panel));
        if (open_three != null) open_three.onClick.AddListener(() => OpenPanel(Three_Panel));
        if (open_four != null) open_four.onClick.AddListener(() => OpenPanel(Four_Panel));
        
        // 为返回按钮添加点击事件监听
        if (back_one != null) back_one.onClick.AddListener(Back);
        if (back_two != null) back_two.onClick.AddListener(Back);
        if (back_three != null) back_three.onClick.AddListener(Back);
        if (back_four != null) back_four.onClick.AddListener(Back);

        // 初始化显示主面板
        ShowOnly(Main);
        panelList.Clear();
        panelList.Add(Main);
    }

    /// <summary>
    /// 打开新面板
    /// </summary>
    /// <param name="panel">要打开的面板</param>
    public void OpenPanel(GameObject panel)
    {
        if (panel == null) 
        {
            Debug.LogWarning("面板对象不能为空");
            return;
        }
        
        ShowOnly(panel);           // 显示指定面板
        panelList.Add(panel);      // 将面板添加到历史列表
    }

    /// <summary>
    /// 返回上一级面板
    /// 类似浏览器的后退功能
    /// </summary>
    public void Back()
    {
        if (panelList.Count > 1) // 确保至少有一个面板可以返回
        {
            panelList.RemoveAt(panelList.Count - 1); // 从历史列表中移除当前面板
            ShowOnly(panelList[panelList.Count - 1]); // 显示上一个面板
        }
        else
        {
            Debug.Log("已经是第一个面板,无法返回");
        }
    }

    /// <summary>
    /// 直接返回主面板
    /// </summary>
    public void BackToMain()
    {
        if (Main != null)
        {
            ShowOnly(Main);
            panelList.Clear();
            panelList.Add(Main);
        }
    }

    /// <summary>
    /// 只显示指定的面板,隐藏其他所有面板
    /// 确保任何时候只有一个面板处于激活状态
    /// </summary>
    /// <param name="panel">要显示的面板</param>
    void ShowOnly(GameObject panel)
    {
        if (panel == null) return;

        // 遍历所有面板,只激活指定的面板
        if (Main != null) Main.SetActive(panel == Main);
        if (One_Panel != null) One_Panel.SetActive(panel == One_Panel);
        if (Two_Panel != null) Two_Panel.SetActive(panel == Two_Panel);
        if (Three_Panel != null) Three_Panel.SetActive(panel == Three_Panel);
        if (Four_Panel != null) Four_Panel.SetActive(panel == Four_Panel);
    }

    /// <summary>
    /// 获取当前面板历史深度
    /// </summary>
    /// <returns>历史深度</returns>
    public int GetHistoryDepth()
    {
        return panelList.Count;
    }
}

 

posted @ 2025-07-29 14:55  Ww7i  阅读(6)  评论(0)    收藏  举报