做成Prefab的UI面板
[
{"panelTypeString":"ItemMessage",
"path":"UIPanel/ItemMessagePanel"},
{"panelTypeString":"Knapsack",
"path":"UIPanel/KnapsackPanel"},
{"panelTypeString":"MainMenu",
"path":"UIPanel/MainMenuPanel"},
{"panelTypeString":"Shop",
"path":"UIPanel/ShopPanel"},
{"panelTypeString":"Skill",
"path":"UIPanel/SkillPanel"},
{"panelTypeString":"System",
"path":"UIPanel/SystemPanel"},
{"panelTypeString":"Task",
"path":"UIPanel/TaskPanel"}
]
public class UIPanelInfo
{
public string PanelTypeString { get; set; }
public string Path { get; set; }
public UIPanelInfo(string panelTypeString,string path)
{
this.PanelTypeString = panelTypeString;
this.Path=path;
}
}
public class UIManager
{
#region 单例模式
private static UIManager _instance;
public static UIManager Instance{
get {
if (_instance==null)
{
_instance = new UIManager();
}
return _instance;
}
}
#endregion
private Transform canvasTransform ;
private Transform CanvasTransform {
get
{
if (canvasTransform == null)
{
canvasTransform = GameObject.Find("Canvas").transform;
}
return canvasTransform;
}
}
private Dictionary panelPathDict;
private Dictionary PanelDict;//保存所有实例化面板的游戏物体身上的BasePanel组件
private Stack panelStack;
private UIManager() {
ParseUipanelTypeJson();
}
///
/// 根据面板类型,得到实例化的面板
///
///
private BasePanel GetPanel (string panelType){
if (PanelDict==null) {
PanelDict = new Dictionary();
}
//BasePanel panel;
//PanelDict.TryGetValue(panelType, out panel);
BasePanel panel = PanelDict.TryGet(panelType);
if (panel == null)
{
//如果找不到,就表示这个面板没有实例化过,那么就去找到这个面板的Prefab的路径,然后去根据Prefab去实例化面板
//string path;
//panelPathDict.TryGetValue(panelType, out path);
string path = panelPathDict.TryGet(panelType);
GameObject instPane = GameObject.Instantiate(Resources.Load(path)) as GameObject;
instPane.transform.SetParent(CanvasTransform,false);
PanelDict.Add(panelType, instPane.GetComponent());
return instPane.GetComponent();
}
else {
return panel;
}
}
///
/// 入栈 把某个页面入栈
///
public void PushPanel(string paneltype) {
if (panelStack==null) {
panelStack = new Stack();
}
//判断一下栈里面是否有页面
if (panelStack.Count>0) {
BasePanel topPanel = panelStack.Peek();//取出栈顶元素
topPanel.Onpause();
}
BasePanel panel = GetPanel(paneltype);
panel.OnEnter();
panelStack.Push(panel);
}
///
/// 出栈 把某个页面出栈
///
public void PopPanel() {
if (panelStack==null) {
panelStack = new Stack();
}
if (panelStack.Count <= 0) return;
//关闭栈顶页面的显示
BasePanel toppanel = panelStack.Pop();
toppanel.OnExit();
if (panelStack.Count <= 0) return;
BasePanel topPane2 = panelStack.Peek();
topPane2.OnResume();
}
///
/// 解析Json数据
///
private void ParseUipanelTypeJson() {
panelPathDict = new Dictionary< string,string>();
JsonData data = JsonMapper.ToObject(File.ReadAllText("Assets/Script/UIFramework/Resources/UIPanelType.txt"));
UIPanelInfo info;
foreach (JsonData temp in data)
{
string panelTypeString = temp["panelTypeString"].ToString();
string Path = temp["path"].ToString();
info = new UIPanelInfo(panelTypeString, Path);
panelPathDict.Add(info.PanelTypeString, info.Path);
}
}
}
///
/// 对Dictory的扩展
///
public static class DictionaryExtension {
///
/// 尝试根据key得到value,得到了的话直接返回value,没有得到直接返回null
/// this Dictionary dict 这个字典表示我们要获取值的字典
///
public static Tvalue TryGet(this Dictionary dict, Tkey key)
{
Tvalue value;
dict.TryGetValue(key, out value);
return value;
}
public class BasePanel : MonoBehaviour {
///
/// 界面被显示出来
///
public virtual void OnEnter()
{
}
///
/// 界面暂停
///
public virtual void OnPause()
{
}
///
/// 界面继续
///
public virtual void OnResume()
{
}
///
/// 界面不显示,退出这个界面,界面被关系
///
public virtual void OnExit()
{
}
}
public class GameRoot : MonoBehaviour {
private void Start()
{
UIManager.Instance.PushPanel("MainMenu");
}
}
using DG.Tweening;
public class TaskPanel : BasePanel {
private CanvasGroup canvasGrop;
private void Start()
{
if (canvasGrop == null)
{ canvasGrop = GetComponent(); }
}
public override void OnEnter()
{
if (canvasGrop == null) { canvasGrop = GetComponent(); }
canvasGrop.alpha = 0;
canvasGrop.blocksRaycasts = true;
canvasGrop.DOFade(1,.5f);
}
//处理页面的关闭
public override void OnExit()
{
//canvasGrop.alpha = 0;
canvasGrop.blocksRaycasts = false;
canvasGrop.DOFade(0,.5f);
}
public void OnClosePanel() {
UIManager.Instance.PopPanel();
}
}