UIPanelType.json、UIPanelInfo、和UIManage脚本代码

   UIPanelType.json


{
    "infoList":
    [
    
    {"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"}

]
}

UIPanelInfo

using UnityEngine;
using System.Collections;
using System;

[Serializable]
public class UIPanelInfo :ISerializationCallbackReceiver {
    [NonSerialized]
    public UIPanelType panelType;
    public string panelTypeString;
    //{
    //    get
    //    {
    //        return panelType.ToString();
    //    }
    //    set
    //    {
    //        UIPanelType type =(UIPanelType)System.Enum.Parse(typeof(UIPanelType), value);
    //        panelType = type;
    //    }
    //}
    public string path;

    // 反序列化   从文本信息 到对象
    public void OnAfterDeserialize()
    {
        Debug.Log("开始调用");
        UIPanelType type = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), panelTypeString);
        panelType = type;
    }

    public void OnBeforeSerialize()
    {
        
    }
}

UIManage脚本

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.UI;
public class UIManager
{

    /// 
    /// 单例模式的核心
    /// 1,定义一个静态的对象 在外界访问 在内部构造
    /// 2,构造方法私有化

    private static UIManager _instance;

    public static UIManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new UIManager();
            }
            return _instance;
        }
    }
    private Transform canvasTransform;

    public Transform CanvasTransform
    {
        get {
            if (canvasTransform == null)
            {
                canvasTransform = GameObject.Find("Canvas").transform;
            }
            return canvasTransform;
        }
        set {; }
    }

      
    private Dictionary panelPathDict;//存储所有面板Prefab的路径

    private Dictionary panelDict;//保存所有实例化面板的游戏物体身上的BasePanel组件

    private Stack panelStack;


    private UIManager()
    {
        ParseUIPanelTypeJson();
    }
    /// 
    ///入栈
    /// 
    public void PushPanel(UIPanelType 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();
        }
        Debug.Log(panelStack.Count);
        if (panelStack.Count <= 0)
        {
            Debug.Log("asdfasdfasd");
            return;
        }

        BasePanel topPanel = panelStack.Peek();
        topPanel.OnExit();
        panelStack.Pop();
        Debug.Log(panelStack.Count);
        if (panelStack.Count <= 0)
        {
            return;
        }
        BasePanel topPanel2 = panelStack.Peek();
        topPanel2.OnResume();
    }

    /// 
    /// 
    /// 
    /// 
    private BasePanel GetPanel(UIPanelType panelType)
    {
        if (panelDict == null)
        {
            panelDict = new Dictionary();
        }

        //BasePanel panel;
        //panelDict.TryGetValue(panelType, out panel);//TODO

        BasePanel panel = panelDict.TryGet(panelType);

        if (panel == null)
        {
            //如果找不到,那么就找这个面板的Prefab的路径,然后去根据prefab 去实例化面板

            //string path;
            //panelPathDict.TryGetValue(panelType, out path);
            string path =  panelPathDict.TryGet(panelType);

            GameObject instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;

            instPanel.transform.SetParent(CanvasTransform,false);//TODO

            panelDict.Add(panelType, instPanel.GetComponent());

            return instPanel.GetComponent();
        }
        else
        {
            return panel;
        }
       
    }

    [Serializable]
    class UIPanelTypeJson
    {
        public List infoList;
    }


    private void ParseUIPanelTypeJson()
    {


        panelPathDict = new Dictionary();

        TextAsset ta = Resources.Load("UIPanelType");
        //因为这种方法反序列化不了带枚举类型的对象  所以
        UIPanelTypeJson jsonObject = JsonUtility.FromJson(ta.text);

        foreach (UIPanelInfo info in jsonObject.infoList)
        {
            panelPathDict.Add(info.panelType, info.path);
        }
    }

    /// 
    /// just for test
    /// 
    public void Test()
    {
        string path ;
        panelPathDict.TryGetValue(UIPanelType.Knapsack,out path);

    }
}

你可能感兴趣的:(UIPanelType.json、UIPanelInfo、和UIManage脚本代码)