1、在Unity"Assets"目录下新建“Resources”特殊文件夹
2、“Resources”特殊文件夹下新建一个Txt文本
3、更改Txt文本的文件名和后缀(确保文件拓展名是没有被隐藏的,并且后缀改为"json")
4、在上面的json文件中使用json语法写入数据(若Unity无法识别中文,将Json文件的编码格式改为“UTF-8”)
附:编码格式修改方法,使用电脑自带软件“记事本“打开该Json文件,点击”另存为“,选择编码格式为“UTF-8”,点击保存并替换掉该json文件。
[
{
"ID": "0",
"Date_Type": "字符串"
},
{
"ID": "1",
"Date_Type": "整数"
}
]
5、新建脚本”ItemDate“用来对应json文件中数据
using System;
/// <summary>
/// 物品信息类
/// </summary>
[Serializable]
public class ItemDate
{
public string ID;
public string Date_Type;
}
6、将LitJson.dll文件导入Unity,放在”Plugins“路径下
7、新建脚本”BagManager“用来读取json文件中数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
public class BagManager : MonoBehaviour
{
public List<ItemDate> Bag_ItemList = new List<ItemDate>();
// Start is called before the first frame update
void Start()
{
ReadInformation();
}
/// <summary>
/// 读取json文档信息
/// </summary>
private void ReadInformation()
{
//读取csv二进制文件
TextAsset binAsset = Resources.Load("Grid_Information", typeof(TextAsset)) as TextAsset;
string Content = binAsset.text;
//解析数据
Bag_ItemList = JsonMapper.ToObject<List<ItemDate>>(Content);
}
}