其中BasePanel和Canvas都是挂在面板的预制物上的。
1.导入我们的UI框架(本篇文章中有用的是两个UIPanelType,NUIManager和NBasePanel,会放在文章最后供大家使用)
2.先将我们做好的Panel设置成预制物,注意这边创建一个叫做Resources的文件夹,并且下面创建一个Panel文件夹,然后把预制物都放在里面。
3.在PanelType的脚本文件中注册我们需要管理的面板的信息。(这边注册两个来示范)
4.在PanelType的json配置文件中注册这些面板的名字以及路径信息
5.在Unity中创建一个空物体取名为GameManager并且在其身上挂上一个名叫GameManager的脚本,在脚本的Start方法里写一个弹出面板的功能。
6.在你的预制物身上挂上Canvas Group和NBasePanel脚本
7.点击运行,可以看到我们在GameManager的Start方法里面写的显示LoadingPanel的语句成功调用,并且在Unity里面成果显出除了此面板的UI。
最后是UI框架的代码
UIPanelType.json:
{
"PanelList": [
{
"PanelName": "LodingPanel",
"PanelPath": "Panel/LodingPanel"
},
{
"PanelName": "MainPanel",
"PanelPath": "Panel/MainPanel"
},
{
"PanelName": "PopUp",
"PanelPath": "Panel/PopUp"
}
]
}
DicTool.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class DicTool{
public static TValue TryGetValueByNN(this Dictionary dic, TKey type )
{
TValue v;
dic.TryGetValue(type, out v);
return v;
}
}
NBasePanel.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NBasePanel : MonoBehaviour {
protected CanvasGroup canvas;
public virtual void OnEnter()
{
if (canvas == null)
{
canvas = GetComponent();
}
print("open");
canvas.alpha = 1;
canvas.blocksRaycasts = true;
}
public virtual void OnExit()
{
print("close");
canvas.alpha = 0;
canvas.blocksRaycasts = false;
}
public virtual void OnPause()
{
print("OnPause");
}
public virtual void OnResume()
{
print("OnResume");
}
}
NUIManager.cs:
using System;
using UnityEngine;
using System.Collections.Generic;
public class NUIManager
{
private static NUIManager Instance;
private Transform CanvasTransform;
UIPanelTypeJson json;
//打开的顺序 堆
Stack PanelStack = new Stack();
private Dictionary PanelCach = new Dictionary();
public static NUIManager GetInstance()
{
//惰性实例化
if (Instance == null)
{
Instance = new NUIManager();
}
return Instance;
}
///
/// 现实一个面板
///
///
public void PushPanel(UIPanelType panelType)
{
//调用被挡住的OnPause
if (PanelStack.Count != 0)
{
PanelStack.Peek().OnPause();
}
NBasePanel panel = GetPanel(panelType);
PanelStack.Push(panel);
panel.OnEnter();
}
public void PopPanel()
{
if (PanelStack.Count == 0)
return;
NBasePanel panel = PanelStack.Pop();
panel.OnExit();
if (PanelStack.Count != 0)
PanelStack.Peek().OnResume();
}
private NUIManager()
{
ParseUIPanelTypeJson();
CanvasTransform = GameObject.Find("Canvas").transform;
}
private void ParseUIPanelTypeJson()
{
//加载json文件
TextAsset t = Resources.Load("UIPanelType");
json = JsonUtility.FromJson(t.text);
Debug.LogError(json.PanelList.Length);
Debug.LogError(json.PanelList[0].PanelPath);
}
///
/// 创建一个面板并且显示
///
///
///
private NBasePanel GetPanel(UIPanelType panelType)
{
GameObject instPanel = PanelCach.TryGetValueByNN(panelType);
//判断缓存里面有没有,如果没有,创建新的,如果有拿缓存里的
if (instPanel == null)
{
//通过名字找路径
string path = "";
foreach (var item in json.PanelList)
{
Debug.Log("开始查找" + item.PanelName + ":" + item.PanelPath);
if (item.PanelName == panelType.ToString())
{
path = item.PanelPath;
Debug.Log("找到啦" + item.PanelName);
}
}
Debug.Log("新创建创建啦:" + path);
instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;
instPanel.transform.SetParent(CanvasTransform, false);
PanelCach.Add(panelType, instPanel);
}
else
{
Debug.Log("用的缓存");
}
return instPanel.GetComponent();
}
}
//序列化
[Serializable]
public class UIPanelTypeJson
{
public UIPanelInfo[] PanelList;
}
//序列化
[Serializable]
public class UIPanelInfo
{
public string PanelName;
public string PanelPath;
}
UIPanelType.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum UIPanelType {
LodingPanel,
MainPanel
}