首先用一个类来保存Json的数据
public class Item{
public string Name {get;set;}
public int Age {get;set;}
public string Hobby {get;set;}
//构造函数
public Item (int name,int age,string hobby)
{
this.Name = name;
this.Age = age;
this.Hobby = hobby;
}
}
获取Json数据的方法
//获取Json里的数据
ConfigJson GetJsonInfo()
{
//Application.streamingAssetsPath(相当于 Asset -> StreamingAssets目录)
return JsonMapper.ToObject<ConfigJson>(File.ReadAllText(Application.streamingAssetsPath + "/config.ini"));
}
调用
void Awake()
{
//调用获取Json里的数据 存入config变量中
var config = GetJsonInfo();
}
使用通过
config.name
config.age
config.hobby
Json数据格式
{
"id": 1,
"name": "Knife",
"age": 100,
"hobby":"游泳"
}