Unity加载配置文件【解析Json】

Json 文件

Json文件的存储:

存储在StreamingAssets目录下的:
//这里用了游戏配置表常用的Json存储格式-对象数组

{
   
   "data":[
     {
   
      "id": 1001,
      "name": "ScreenFront_1",
    },
    {
   
      "id": 1002,
      "name": "ScreenLeft_1",
    },{
   
      "id": 1003,
      "name": "ScreenRight_1",
    },{
   
      "id": 1004,
      "name": "ScreenBottom_1",
    },{
   
      "id": 1005,
      "name": "Logo_1",
    },]
}

Json文件的解析:

定义本地存储结构

定义本地数据结构,添加序列化标签[System.Serializable]

using System.Collections.Generic;

[System.Serializable]
public class ConfigData {
   
    public bool debug {
    get; set; }
    public bool debugConsole {
    get; set; }
    public int logDeleteTime {
    get; set; }
    public int debugLevel {
    get; set; }
    public bool connectWs {
    get; set; }
    public bool debugMultiPlayer {
    get; set; }
    public bool allowSetCave {
    get; set; }
    public int playerCount {
    get; set; }
    public int noBodyTime {
    get; set; }
    public float video360Volume {
    get; set; }
    public float roiRadius {
    get; set; }
    public bool allowClose {
    get; set; }
    public int uiPlan {
    get; set; }

}

[System.Serializable]
public class ArrObject {
   
    public string child {
    get; set; }
    public int age {
    get; set; }
}

[System.Serializable]
public class ObjObject {
   
    public string parent {
    get; set; }
    public int age {
    get; set; }
}

public class AppData
{
   
    public AppItem[] data;
}
public class AppItem
{
   
    public string name;
    public string description;
    public string picture;
    public string appName;
    public string appUrl;
}

public class UIData
{
   
    public UIElement[] data;
}

public class UIElement
{
   
    public int id {
    get; set; }
    public string name {
    get; set; }   
}

反序列化Json到本地数据结构

    void MergeJson()
    {
   
        // Merge all JSON files in the json folder
        string jsonFolderPath = Application.streamingAssetsPath + "/json";
        List<string> jsonFiles = new List<string>(Directory.GetFiles(jsonFolderPath, "*.json"));
        foreach (string filePath in jsonFiles)
        {
   
            string fileName = Path.GetFileName(filePath).Split('.')[0];
       
            switch (fileName)
            {
   
                case "config":
                    data = ReadJsonFile<ConfigData>(filePath);
                    break;
                case "display":
                    display = ReadJsonFile<DisplayConfig.DisplayConfigData>(filePath);
                    break;
                case "apps":
                    appsData = ReadJsonFile<AppData>(filePath);
                    break;
                case "UIElements":
                    uiData = ReadJsonFile<UIData>(filePath);
                    break;
                default:
                    break;
            }
        }
    }

ReadJson 方法

 public static T ReadJsonFile<T>(string filePath)
 {
   
     string readData = File.ReadAllText(filePath);
     // 支持数组嵌套的情况
     return JsonConvert.DeserializeObject<T>(readData);
 }

JsonConvert.DeserializeObject(readData);
来自于Newtonsoft.Json 库
用于将参数中的字符串文本反序列化到T泛型的实例化数据结构。(可自定义序列化过程)
返回类型可空

运行时调用

可以将Merge()方法做成单例。在程序一开始时,显示的调用。
也可以在配置文件更新数据【热更新时】动态地去更新本地数据结构。
在本地数据结构不为空时,在其他模块拿到里面的数据。

例. 加载UI图片

1.实例化本地存储结构

private IList<UIElement> uiDataList;
uiDataList = new List<UIElement>(ConfigManager.Instance.uiData.data);

2.读取本地(已Merge过的数据)

 public void InitializeImageByName(string targetName,int width,int height,GameObject targetObject)
 {
   
     foreach (var item in uiDataList)
     {
   
         if(item.name == targetName)
         {
   
             ResourcesManager.Instance.InitializeImageFromStreamingAssets(ResPath.StreamingUIDir, item.name, width, height,targetObject);
         }
     }
 }

item.name 即访问到了
image.png
的name 属性。
后续从资源管理器加载对应UI资源即可。

相关推荐

  1. 动态json文件

    2023-12-06 01:30:07       39 阅读
  2. springBoot配置文件

    2023-12-06 01:30:07       127 阅读
  3. 【spring】外部的配置文件

    2023-12-06 01:30:07       26 阅读
  4. Logback 配置文件步骤

    2023-12-06 01:30:07       21 阅读
  5. springboot配置文件(三)外部配置文件

    2023-12-06 01:30:07       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-06 01:30:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 01:30:07       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 01:30:07       82 阅读
  4. Python语言-面向对象

    2023-12-06 01:30:07       91 阅读

热门阅读

  1. 【Element-ui】Element-ui是什么?如何安装

    2023-12-06 01:30:07       63 阅读
  2. 【Docker仓库】部署Docker Registry web-ui管理镜像仓库

    2023-12-06 01:30:07       57 阅读
  3. Uniapp Vue3 基础到实战 教学视频

    2023-12-06 01:30:07       51 阅读
  4. Android MTK平台配置应用可卸载

    2023-12-06 01:30:07       55 阅读
  5. ubuntu1804安装jupyter中的js环境

    2023-12-06 01:30:07       65 阅读
  6. 1076 Forwards on Weibo (链接表层序遍历)

    2023-12-06 01:30:07       55 阅读
  7. React实现登录授权功能

    2023-12-06 01:30:07       66 阅读
  8. 制作openeuler的livecd

    2023-12-06 01:30:07       68 阅读
  9. docker快捷控制

    2023-12-06 01:30:07       47 阅读
  10. QT之QNetworkAccessManager

    2023-12-06 01:30:07       56 阅读
  11. C#实现批量生成二维码

    2023-12-06 01:30:07       51 阅读
  12. 基于 EmotiVoice 的批量 TXT 文本转语音工具

    2023-12-06 01:30:07       50 阅读