Unity5.3 的一项新功能就是Json的序列化,支持嵌套使用,可以把json字符串转成对象,把对象转成json字符串。
using UnityEngine; using UnityEditor; using System.Collections; using System; public class NewBehaviourScript : MonoBehaviour { void Start () { MyObject myObject = new MyObject(); myObject.name = "雨松MOMO"; myObject.newOjbect = new MyNewObject(){level =100}; string json = JsonUtility.ToJson(myObject); Debug.Log(json); myObject = JsonUtility.FromJson<MyObject>(json); Debug.Log(myObject.name +" " + myObject.newOjbect.level); JsonUtility.FromJsonOverwrite(json,myObject); } } [Serializable] public class MyObject { public string name; public MyNewObject newOjbect; } [Serializable] public class MyNewObject { public int level; }
目前我用的是测试版。 遗憾的是还不支持字典 还有 Hashtable
或者试试这个人的方法, http://kou-yeung.hatenablog.com/entry/2015/12/31/014611
时间: 2024-11-10 13:19:46