unity读取resources下面的json配置文件 以MiniJson为例

示例json串

{
   "dataList":[
  
        {
            "type": 1,
            "name": "额头",
            "icon": "forehead_icon@3x",
            "smallDataList": [
                {
                    "type": 0,
                    "name": "额头宽度",
                    "resList": [
                        "forehead_narrow",
                        "forehead_wide"
                    ],
                    "icon": "forehead_width_icon@3x"
                },
                {
                    "type": 1,
                    "name": "额头前后",
                    "resList": [
                        "forehead_deep",
                        "forehead_shallow"
                    ],
                    "icon": "forehead_depth_icon@3x"
                }
            ]
        },
        {
            "type": 1,
            "name": "耳朵",
            "icon": "ear_icon@3x",
            "smallDataList": [
                {
                    "type": 0,
                    "name": "耳朵高度",
                    "resList": [
                        "earlobe_short",
                        "earlobe_long"
                    ],
                    "icon": "ear_height_icon@3x"
                },
                {
                    "type": 1,
                    "name": "耳朵宽度",
                    "resList": [
                        "ears_narrow",
                        "ears_wide"
                    ],
                    "icon": "ear_width_icon@3x"
                }
            ]
        }
        
    ]
}

C#解析

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.IO;
using System;


public class AdjustSmallData
{
    public int type;
    public string name;
    public string icon;
    public List<string> targetPath;
}
public class AdjustData
{
    public int type;
    public string name;
    public string icon;
    public List<AdjustSmallData> smallDataList;
}

public class ReadJson : MonoBehaviour
{
    private List<AdjustData> dataList = new List<AdjustData>();
    private void Start()
    {

        Debug.Log("Start");
        for (var i = 0; i < hideBodyTogList.Count; ++i)
        {
            var tempI = i;
            hideBodyTogList[i].onValueChanged.AddListener((selected) =>
            {
                _body_example.ShowBodyPart(tempI, selected);
            });
        }

        //初始化数据
        Debug.Log("初始化脸部细节数据****************");
        var textString = Resources.Load<TextAsset>("faceConfig");
        var tempDic = MiniJSON.Json.Deserialize(textString.ToString()) as Dictionary<string, object>;
        var list = tempDic["dataList"] as List<object>;


        for (var i = 0; i < list.Count; ++i)
        {
            var temp = list[i] as Dictionary<string, object>;
            var data = new AdjustData()
            {
                type = i,
                name = temp["name"].ToString(),
                icon = temp["icon"].ToString(),
                smallDataList = new List<AdjustSmallData>()
                {

                }
            };

            var smallList = temp["smallDataList"] as List<object>;
            for (var j = 0; j < smallList.Count; ++j)
            {
                var tempSmall = smallList[j] as Dictionary<string, object>;
                var resList = tempSmall["resList"] as List<object>;
                data.smallDataList.Add(new AdjustSmallData
                {
                    type = j,
                    name = tempSmall["name"].ToString(),
                    icon = tempSmall["icon"].ToString(),
                    targetPath = new List<string> {
                        resList[0] as string,
                        resList[1] as string,
                    }
                });
            }

            dataList.Add(data);
        }
        Debug.Log("初始化脸部细节数据****************完成");
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • JSON 指 JavaScript 对象表示法(JavaScript Object Notation),是一种轻量...
    神齐阅读 1,495评论 0 11
  • 相信不少搞开发的小伙伴都遇到过读取Json文件的问题。Unity 5.3版本之后,提供了一个叫 JsonUt...
    WithStream阅读 9,644评论 4 8
  • 吃鱼的时候, 在重庆吃豌豆荚,在昆明吃薄荷 在重庆吃山药,在昆明吃香芋 在重庆吃蓬蒿,在昆明吃茴香
    安车安茶阅读 247评论 0 0
  • 秋了吗 所以中秋 国庆 快到了吧 其实还有两个月 好好学习 好好工作 好好生活 日记无不劝告 自己要这样 所以日复...
    三趾树懒记阅读 56评论 0 1
  • 一年一度的跨年演讲,那个男人又来了。四万多字的无删节文字版在罗辑思维的公号已经发出。我对他其实了解不多,简单作下框...
    第一公文馆阅读 1,151评论 0 1