简单的UI框架 | 三、开发UIManager解析面板信息Json

简单的UI框架

开发UIManager解析面板信息Json

UIManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager
{
    
    
    private Dictionary<UIType, string> panelPathDict;//存储所有的面板Prefab的路径

    private UIManager()
    {
    
    
        ParseUITypeJson();
    }

    private void ParseUITypeJson()
    {
    
    
        panelPathDict = new Dictionary<UIType, string>();

        TextAsset ta = Resources.Load<TextAsset>("UIType");

        List<UIPanelInfo> panelInfoList = JsonUtility.FromJson<List<UIPanelInfo>>(ta.text);

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

}

首先我们建立一个字典用来存放所有面板的路径信息,注意我们在使用字典的时候需要引入一个解析空间using System.Collections.Generic
因为我们的UIManager需要做成单例模式,他的构造方法需要私有化,把构造方法私有化,在外界就不能调用,就没有办法在外界实例化,实例化这个对象我们只需要在内部实例化。实例化这个对象的时候只会实例化一次,在实例化的时候做一个初始化,在实例化的时候调用下面的方法解析Json文件。
接下来是我们解析Json的方法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager
{
    
    


    private void ParseUITypeJson()
    {
    
    
        panelPathDict = new Dictionary<UIType, string>();

        TextAsset ta = Resources.Load<TextAsset>("UIType");

        List<UIPanelInfo> panelInfoList = JsonUtility.FromJson<List<UIPanelInfo>>(ta.text);

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

}

这里我们为了方便调用Json文件,我们新建一个Resources文件夹,将Json文件放入这样我们可以直接找到这个Json文件,文件类型为TextAsset。
我们需要将Json信息转化为对象,我们就需要使用JsonUtility自带的FromJson方法,当然把对象转化为Json信息我们可以使用ToJson方法。
我们使用FromJson解析时需要定义一个类,这个类就是跟Json信息对应的一个类,类里面的属性需要对应于Json中的属性,然后直接调用FromJson就可以解析出来。
所以我们新建一个脚本,专门放这个类。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class UIPanelInfo 
{
    
    
    public UIType panelType;
    public string path;
}

属性名字要一一对应。
这里我们要加入一个注解
[Serializable]
表示他是序列化了,当然需要加入一个命名空间
using System;
序列化是将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入临时或持久性存储区。以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。
这样定义之后就相当于Json文件相当于UIPanelInfo的一个List。所以我们之后就可以当作一个UIPanelInfo类型的List进行读取就可以了。

返回UIManager脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager
{
    
    


    private void ParseUITypeJson()
    {
    
    
        panelPathDict = new Dictionary<UIType, string>();

        TextAsset ta = Resources.Load<TextAsset>("UIType");

        List<UIPanelInfo> panelInfoList = JsonUtility.FromJson<List<UIPanelInfo>>(ta.text);

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

}

接下来进行读取操作


        List<UIPanelInfo> panelInfoList = JsonUtility.FromJson<List<UIPanelInfo>>(ta.text);


这里我们用到了泛函的知识来定义了List。
接下来我们把得到的信息转换到字典里面即可,首先我们在调用方法之前先置空这个字典。


panelPathDict = new Dictionary<UIType, string>();

接下来遍历所有的信息


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

我们把panelType当作Key,path当作Value存到字典里面,为了之后方便通过Type来查找路径。

总结

UIManager为UI框架的核心管理类。
首先实现了解析保存所有的面板信息,将Json文件通过键值对的形式进行查找,同时学会了JsonUtility自带的方法如何使用,也用到了C#中重要的泛函知识,

猜你喜欢

转载自blog.csdn.net/m0_64058685/article/details/124526254