自我介紹
廣東雙非一本的大三小白,計科專業,想在製作畢設前夯實基礎,畢設做出一款屬於自己的遊戲!
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);
}
}
}