Unity数据读取二 读取json数据
程序员文章站
2024-01-20 12:57:52
...
导入Json的引用文件:LitJson
链接:https://pan.baidu.com/s/1L60zADN6PTrue4hwTvCwCA
提取码:thhf
库放到Plugins文件夹下
创建json文件 json文件一起放到百度云盘里了
{
"Listdata":[
{
"ID": 0,
"Open": true,
"Icon": "UIProp/0",
"Name": "加血",
"Isprop": true,
"Desc": "解释",
"Time":0,
"Wait":3
},
{
"ID": 1,
"Open": true,
"Icon": "UIProp/0",
"Name": "加蓝",
"Isprop": true,
"Desc": "解释",
"Time":0,
"Wait":3
},
{
"ID": 2,
"Open": false,
"Icon": "UIProp/0",
"Name": "加血药水",
"Isprop": true,
"Desc": "解释",
"Time":0,
"Wait":3
},
{
"ID": 3,
"Open": false,
"Icon": "UIProp/0",
"Name": "加血药水",
"Isprop": true,
"Desc": "解释",
"Time":0,
"Wait":3
},
{
"ID": 4,
"Open": false,
"Icon": "UIProp/0",
"Name": "加血药水",
"Isprop": true,
"Desc": "解释",
"Time":0,
"Wait":3
}
]
}
//自己创建的json 验证是否通过 https://www.bejson.com/ json全文赋值粘贴点击格式化校验
//json放到StreamingAssets文件夹下
然后读取
using UnityEngine;
using LitJson;
using System.IO;
public class TaskManager : MonoBehaviour
{
void Start()
{
StreamReader streamreader = new StreamReader(Application.dataPath + "/StreamingAssets/daojuData.json");//读取数据,转换成数据流
string str = streamreader.ReadToEnd();
tuJIanData.DaoJuData = JsonMapper.ToObject<TuJianList>(str);
}
}
//详细文件夹
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TuJIanData
{
private TuJianList daojuData = new TuJianList();
public TuJianList DaoJuData
{
get
{
return daojuData;
}
set
{
daojuData = value;
}
}
}
public class TuJianList
{
//public List<TuJianItem> Listdata = new List<TuJianItem>();
public List<TuJianItem> Listdata { get; set; }
}
[Serializable]
public class TuJianItem
{
public int ID
{
get;
set;
}
public bool Open
{
get;
set;
}
public string Icon
{
get;
set;
}
public string Name
{
get;
set;
}
public bool Isprop
{
get;
set;
}
public string Desc
{
get;
set;
}
public int Time
{
get;
set;
}
public int Wait
{
get;
set;
}
}