【U3D/简单框架】6.UI模块

自我介绍

广东双非一本的大三小白,计科专业,想在制作毕设前夯实基础,毕设做出一款属于自己的游戏!

UI模块

UI基类 BasePanel.cs,以后子类只需要继承这个基类,使用如图

一定一定一定要注意泛型的约束 where T: [class]

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

/// <summary>
/// 面板基类
/// 找到所有自己面板下的控件对象
/// 方便我们再子类中处理逻辑
/// 节约找控件的工作量
/// </summary>
public class BasePanel : MonoBehaviour
{
    // 里氏转换原则,所有UI都会继承UIBehaviour
    private Dictionary<string, List<UIBehaviour>> uiDic = new Dictionary<string, List<UIBehaviour>>();

    void Awake()
    {
        FindChildrenUIComponent<Button>();
        FindChildrenUIComponent<Image>();
        FindChildrenUIComponent<Text>();
        FindChildrenUIComponent<Toggle>();
        FindChildrenUIComponent<Slider>();
        FindChildrenUIComponent<ScrollRect>();
    }

    // 显示自己
    public virtual void ShowMe()
    {

    }

    // 隐藏自己
    public virtual void HideMe()
    {

    }

    protected T GetControl<T>(string name) where T : UIBehaviour
    {
        if (uiDic.ContainsKey(name))
        {
            for (int i = 0; i < uiDic[name].Count; i++)
            {
                if (uiDic[name][i] is T) return uiDic[name][i] as T;
            }
        }
        return null;
    }

    private void FindChildrenUIComponent<T>() where T : UIBehaviour
    {
        T[] array = this.GetComponentsInChildren<T>();
        string objName;
        for (int i = 0; i < array.Length; i++)
        {
            objName = array[i].gameObject.name;
            if (uiDic.ContainsKey(objName))
                uiDic[objName].Add(array[i]);
            else
                uiDic.Add(objName, new List<UIBehaviour>() { array[i] });
        }
    }
}

UI管理器!精华:showpanel里的回调函数!在showpanel异步加载完成后调用的函数

UIManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

// UI层级
public enum E_UI_Layer
{
    Bot,
    Mid,
    Top,
    System
}

/// <summary>
/// UI管理器
/// 1. 管理所有显示的面板
/// 2. 提供给外部 显示和隐藏等等接口
/// </summary>
public class UIManager : BaseManager<UIManager>
{
    public Dictionary<string, BasePanel> panelDic = new Dictionary<string, BasePanel>();

    private Transform bot;
    private Transform mid;
    private Transform top;
    private Transform system;

    public UIManager()
    {
        GameObject obj = ResMgr.GetInstance().Load<GameObject>("canvas");
        Transform canvas = obj.transform;
        GameObject.DontDestroyOnLoad(obj);

        // 找到各个层
        bot = canvas.Find("Bot");
        mid = canvas.Find("Mid");
        top = canvas.Find("Top");
        system = canvas.Find("System");

        obj = ResMgr.GetInstance().Load<GameObject>("EventSystem");
        GameObject.DontDestroyOnLoad(obj);
    }

    // 显示面板
    public void ShowPanel<T>(string panelName, E_UI_Layer layer, UnityAction<T> callback = null) where T : BasePanel
    {
        if (panelDic.ContainsKey(panelName))
        {
            panelDic[panelName].ShowMe();
            callback?.Invoke(panelDic[panelName] as T);
        }

        ResMgr.GetInstance().LoadAsync<GameObject>("path" + panelName, (obj) =>
        {
            // 因为是UI所以需要拖到Canvas下的父对象
            Transform father = bot;
            switch (layer)
            {
                case E_UI_Layer.Mid:
                    father = mid;
                    break;
                case E_UI_Layer.Top:
                    father = top;
                    break;
                case E_UI_Layer.System:
                    father = system;
                    break;
                default:
                    break;
            }

            // 设置父对象,相对位置和大小,因为涉及到屏幕座标,as两行是为了消除偏差
            obj.transform.SetParent(father);
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localScale = Vector3.one;

            (obj.transform as RectTransform).offsetMax = Vector2.zero;
            (obj.transform as RectTransform).offsetMin = Vector2.zero;

            // 得到与蛇体身上的面板脚本
            T panel = obj.GetComponent<T>();
            // 处理面板创建完成后的逻辑
            callback?.Invoke(panel);

            panel.ShowMe();

            // 把面板存起来
            panelDic.Add(panelName, panel);

        });
    }

    // 隐藏面板
    public void HidePanel(string panelName)
    {
        if (panelDic.ContainsKey(panelName))
        {
            panelDic[panelName].HideMe();
            GameObject.Destroy(panelDic[panelName].gameObject);
            panelDic.Remove(panelName);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章