Unity簡單的UI框架---------UGUI
阿新 • • 發佈:2019-02-03
/* 那天看了SIKI的一個UI框架,他是用到了json解析,感覺有點繁瑣,對於新增需求或者需求變動,所更改的內容比較多: 1、需要更改json文字 2、需要更改對應的列舉型別。所以自己又總結了一個簡單的,只需要增加或者更改UIPanelPath即可, 裡面都是存放著對應的Resources資料夾下的UI面板路徑 */ using UnityEngine; using System.Collections; using System.Collections.Generic; /// <summary> /// UI管理器,用於管理所有面板的顯示和隱藏 /// </summary> public class UIManager{ private static UIManager instance; public static UIManager Instance { get { if (instance==null) { instance = new UIManager(); } return instance; } } private UIManager() { Init(); } private Transform canvasTransform; private Transform CanvasTransform { get { if (canvasTransform==null) { canvasTransform = GameObject.Find("Canvas").transform; } return canvasTransform; } } private Dictionary<string, BasePanel> panelDict; //儲存例項化面板的字典 private Stack<BasePanel> panelStack; //儲存面板的棧 /// <summary> /// 獲取面板 /// </summary> private BasePanel GetPanel(string panelPath) { BasePanel panel; panelDict.TryGetValue(panelPath, out panel); if (panel==null) { GameObject insObj = GameObject.Instantiate(Resources.Load(panelPath)) as GameObject; insObj.transform.SetParent(CanvasTransform, false); BasePanel basePanel = insObj.GetComponent<BasePanel>(); panelDict.Add(panelPath, basePanel); return basePanel; } else { return panel; } } /// <summary> /// 初始化對應的字典和棧 /// </summary> private void Init() { if (panelDict == null) panelDict = new Dictionary<string, BasePanel>(); if (panelStack == null) panelStack = new Stack<BasePanel>(); } /// <summary> /// 入棧,顯示面板 /// </summary> /// <param name="panelPath"> 面板的路徑 </param> public void PushPanel(string panelPath) { if (panelStack.Count > 0) { BasePanel topPanel = panelStack.Peek(); topPanel.OnPause(); } BasePanel panel = GetPanel(panelPath); panel.OnEnter(); panelStack.Push(panel); } /// <summary> /// 出棧,移除面板 /// </summary> public void PopPanel() { if (panelStack.Count <= 0) return; BasePanel topPanel = panelStack.Pop(); topPanel.OnExit(); if (panelStack.Count <= 0) return; BasePanel nextPanel = panelStack.Peek(); nextPanel.OnResume(); } }
using UnityEngine; using System.Collections; /// <summary> /// Resources資料夾下UI面板的路徑,新增和變動只需更改這裡即可 /// </summary> public class UIPanelPath { public const string MAINUIPANEL = "UIPanel/MainUIPanel"; public const string MISSIONCHANGEPANEL = "UIPanel/MissionChangePanel"; }
using UnityEngine; using System.Collections; /// <summary> /// 面板基類,每個面板繼承此類,重寫裡面的方法即可 /// </summary> public class BasePanel : MonoBehaviour { //每個子類面板都掛CanvasGroup元件,用於控制顯示和隱藏 protected CanvasGroup canvasGroup; public virtual void Awake() { canvasGroup = GetComponent<CanvasGroup>(); } /// <summary> /// 面板顯示 /// </summary> public virtual void OnEnter() { } /// <summary> /// 面板暫停 /// </summary> public virtual void OnPause() { } /// <summary> /// 面板恢復 /// </summary> public virtual void OnResume() { } /// <summary> /// 面板關閉 /// </summary> public virtual void OnExit() { } }
using UnityEngine;
using System.Collections;
/// <summary>
///啟動框架
/// </summary>
public class FrameworkStart : MonoBehaviour {
void Start ()
{
UIManager.Instance.PushPanel(UIPanelPath.MAINUIPANEL);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MainUIPanel : BasePanel {
[SerializeField]
private Button missileButton;
[SerializeField]
private Button satelliteButton;
[SerializeField]
private Button spaceportButton;
private void Start()
{
missileButton.onClick.AddListener(OnClickMissileButton);
satelliteButton.onClick.AddListener(OnClickSatelliteButton);
spaceportButton.onClick.AddListener(OnClickSpaceportButton);
}
/// <summary>
/// 點選導彈按鈕
/// </summary>
private void OnClickMissileButton()
{
Debug.Log("導彈");
}
/// <summary>
/// 點選衛星按鈕
/// </summary>
private void OnClickSatelliteButton()
{
Debug.Log("衛星");
}
/// <summary>
/// 點選空間站按鈕
/// </summary>
private void OnClickSpaceportButton()
{
UIManager.Instance.PushPanel(UIPanelPath.MISSIONCHANGEPANEL);
}
public override void OnPause()
{
canvasGroup.alpha = 0;
}
public override void OnResume()
{
canvasGroup.alpha = 1;
}
}