前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >序列化和Stack应用于UGUI(Unity 扩展)

序列化和Stack应用于UGUI(Unity 扩展)

作者头像
bering
发布2019-12-02 14:00:49
4820
发布2019-12-02 14:00:49
举报
文章被收录于专栏:游戏开发之旅

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/CJB_King/article/details/78652777

1.扩展:

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public static class DictionaryExtension  {
    public static TValue TryGet<TKey,TValue>(this Dictionary<TKey,TValue> dict,TKey key )
    {
        TValue value;
        dict.TryGetValue(key, out value);
        return value;
    }
}

2:框架

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class UIManager
{

    private static UIManager _instance = null;
    public static UIManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new UIManager();
            }
            return _instance;
        }
    }
    private Transform canasTrans;
    public Transform canvasTransform
    {
        get
        {
            if (canasTrans == null)
            {
                canasTrans = GameObject.Find("Canvas").transform;
            }
            return canasTrans;
        }

    }
    private Dictionary<UIPanelType, string> panelDic = new Dictionary<UIPanelType, string>();
    private Dictionary<UIPanelType, BasePanel> basePanelDic = new Dictionary<UIPanelType, BasePanel>();
    private Stack<BasePanel> panelStack;
    private UIManager()
    {
        InitialUIPanel();
    }
    public void InitialUIPanel()
    {
        TextAsset textAsset = Resources.Load<TextAsset>("UIPanelType");
        UIPanelInfo UiPanelInfo = JsonUtility.FromJson<UIPanelInfo>(textAsset.text);
        foreach (UIPanel uipanel in UiPanelInfo.infoList)
        {
            if (!panelDic.ContainsKey(uipanel.uiPanelType))
            {
                panelDic.Add(uipanel.uiPanelType, uipanel.path);
            }
        }

    }
    public void PushPanel(UIPanelType uiPanelType)
    {
        if (panelStack == null)
            panelStack = new Stack<BasePanel>();
        if (panelStack.Count>0)
        {
            BasePanel firstPanel = panelStack.Peek();
            firstPanel.OnPause();
        }
        BasePanel secondPanel = TryGetBasePanel(uiPanelType);
        secondPanel.OnEnter();
        if (!panelStack.Contains(secondPanel))
            panelStack.Push(secondPanel);
    }
    public void PopPanel()
    {
        if (panelStack == null || panelStack.Count <= 0)
            return;
        if (panelStack.Count > 0)
        {
            BasePanel basePanel = panelStack.Pop();
            basePanel.OnExit();
        }

        if (panelStack.Count <= 0) return;

        BasePanel basePanel2 = panelStack.Peek();
        basePanel2.OnResume();

    }
    BasePanel TryGetBasePanel(UIPanelType uiPanelType)
    {
        BasePanel getBasePanel = basePanelDic.TryGet<UIPanelType, BasePanel>(uiPanelType);
        if (!getBasePanel)
        {
            string panelPath = panelDic.TryGet<UIPanelType, string>(uiPanelType);
            GameObject PanelGo = Resources.Load(panelPath) as GameObject;
            GameObject uiPanelGo = GameObject.Instantiate(PanelGo);
            uiPanelGo.transform.SetParent(canvasTransform, false);
            BasePanel basePanel = uiPanelGo.GetComponent<BasePanel>();
            basePanelDic.Add(uiPanelType, basePanel);
            return basePanel;
        }
        else
            return getBasePanel;
    }

}
[Serializable]
public class UIPanelInfo
{
    public List<UIPanel> infoList;     //infoList要与序列化文本中的json属性名称保持一致
}
[Serializable]
public class UIPanel : ISerializationCallbackReceiver
{

    public string path;
    public string panelTypeString;
    [NonSerialized]//uiPanelType不会被序列化
    public UIPanelType uiPanelType;
    public void OnBeforeSerialize()
    {

    }
    // 反序列化   从文本信息 到对象
    public void OnAfterDeserialize()
    {
        uiPanelType = (UIPanelType)Enum.Parse(typeof(UIPanelType), panelTypeString);
    }
}

BasePanel:

代码语言:javascript
复制
using UnityEngine;
using System.Collections;

public class BasePanel : MonoBehaviour {

	public virtual void OnEnter()
    {
        Debug.Log(this.name);
    }
    public virtual void OnExit()
    {
        Debug.Log(this.name + "OnExit");
    }
    public virtual void OnPause()
    {
        Debug.Log(this.name+"OnPause");
    }
    public virtual void OnResume()
    {
        Debug.Log(this.name + "OnResume");

    }
}

Json文件对应:

代码语言:javascript
复制
{
	"infoList":
	[
		{ 
			"panelTypeString":"Message",
			"path":"UIPanel/MessagePanel"
		},
		{
			"panelTypeString":"Start",
			"path":"UIPanel/StartPanel"
		},
		{
			"panelTypeString":"Login",
			"path":"UIPanel/LoginPanel"
		},
		{
			"panelTypeString":"Register",
			"path":"UIPanel/RegisterPanel"
		}
	]
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/11/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档