1.导入管理器代码
2.制作面板预制物
3.UIPanelType注册一个名字
4.配置路径
5.UIManager.Instance.PushPanel(UIPanelType.xxxPanel)
6.面板预制物添加BasePanel(让管理器管理面板预制物)
7.面板预制物添加CanvasGroup(让面板透明)
UIManager的管理类
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIManager
{
private static UIManager _instance;
private Transform canvasTransform;
private UIPanelTypeJson json;
//打开的顺序
Stack<BasePanel> PanelStack = new Stack<BasePanel>();
private Dictionary<UIPanelType, GameObject> PanelCach = new Dictionary<UIPanelType, GameObject>();
public static UIManager GetInstance()
{
if (_instance == null)
{
_instance = new UIManager();
}
return _instance;
}
UIManager() {
ParseUIPanelTypeJson();
//canvasTransform = FindObjectOfType<Canvas>().transform;
canvasTransform = GameObject.Find("Canvas").transform;
}
//显示出一个面板panel
public void PushPanel(UIPanelType panelType)
{
if (PanelStack.Count != 0)
{
PanelStack.Peek().OnPause();
}
BasePanel panel = GetPanel(panelType);
//Debug.Log("**************");
PanelStack.Push(panel);
panel.OnEnter();
}
public void PopPanel()
{
if (PanelStack.Count == 0)
{
return;
}
BasePanel panel = PanelStack.Pop();
panel.OnExit();
if (PanelStack.Count !=0 )
{
PanelStack.Peek().OnResume();
}
}
//解析 UIPanelTypeJson
private void ParseUIPanelTypeJson()
{
//加载json文件
TextAsset t = Resources.Load<TextAsset>("UIPanelType");
//Debug.Log(t.text);
json = JsonUtility.FromJson<UIPanelTypeJson>(t.text);
}
private BasePanel GetPanel(UIPanelType panelType)
{
GameObject instPanel = PanelCach.TryGetValueBy(panelType);
if (instPanel == null)
{
//通过名字找path
string path = "";
//Debug.Log(JsonUtility.ToJson(json));
foreach (var item in json.PanelList)
{
//判断panel名字是否相同
if (item.PanelName==panelType.ToString())
{
path = item.PanelPath;
}
}
instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;
if (canvasTransform == null)
{
canvasTransform = GameObject.Find("Canvas").transform;
PanelCach.Clear();
}
instPanel.transform.SetParent(canvasTransform,false);
PanelCach.Add(panelType,instPanel);
//Debug.Log("新创建panel");
}
else
{
//Debug.Log("用的缓存panel");
}
return instPanel.GetComponent<BasePanel>();
}
}
//序列化
[Serializable]
public class UIPanelTypeJson
{
public UIPanelInfo[] PanelList;
}
//序列化
[Serializable]
public class UIPanelInfo
{
public string PanelName;
public string PanelPath;
}
UIPanelType枚举类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum UIPanelType {
MainPanel, DlgRoot,
GameOverPanel, StartPanel,
GamePanel, SignPanel,
AboutPanel, AudioPanel,
EquipPanel, HelpPanel,
SettingPanel, WelcomPanel,
MapPanel, SettlementPanel,
RecoveryPanel, ShopPanel,
TaskPanel, AddScoreImagePanel,
HeroPanel, RankingPanel,
DailyRewardPanel,
}
BasePanel 所有的UIPanel都继承此类
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasePanel : MonoBehaviour {
protected CanvasGroup canvas;
private UIAnim anim;
private void Start()
{
//anim = GetComponent<UIAnim>();
//Debug.Log("GetComponent<UIAnim>().OnEnter();"+ anim.name);
}
public virtual void OnEnter()
{
//Debug.Log("OnEnter:open");
if (canvas == null)
{
canvas = GetComponent<CanvasGroup>();
}
if (GetComponent<UIAnim>() != null)
{
GetComponent<UIAnim>().OnEnter();
}
canvas.alpha = 1;
canvas.blocksRaycasts = true;
}
public virtual void OnExit()
{
//Debug.Log("OnExit:close");
canvas.alpha = 0;
canvas.blocksRaycasts = false;
}
public virtual void OnPause()
{
//Debug.Log("OnPause");
}
public virtual void OnResume()
{
//Debug.Log("OnResume");
}
}
json文件放在Resources文件夹下如图
Resources下也放入面板预制物方便动态加载