Json是现在非常常用的数据格式,因为.Net的版本问题,所有没有很方便的方法可以直接在Unity里面使用C#官方的Json库,于是Unity3D自己提供了自己的一套Json工具——JsonUtility。
参考下面的示例:
using UnityEngine;
using System.Collections;
public class TestSer
{
public int i;
public string str;
}
public class TestDeser
{
public string str;
public float i;
}
public class TestDeser2
{
public string st;
public float f;
}
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
TestSer ts = new TestSer();
ts.i = 10;
ts.str = "aaaa";
string json = JsonUtility.ToJson (ts);
Debug.Log (json);
TestDeser td = JsonUtility.FromJson<TestDeser> (json);
Debug.Log (td.str);
Debug.Log (td.i);
TestDeser2 td2 = JsonUtility.FromJson<TestDeser2> (jso