今天看到一个unity 自带的解析json的IPA,感觉比litjson好用很多,废话不多,上代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.IO; [Serializable] public class MyClass { public int level; public float timeElapsed; public string playerName; } public class Srealizable : MonoBehaviour { string json; string path; private void Awake() { path = Application.streamingAssetsPath + "/myclas.txt"; } // Use this for initialization void Start () { Test1();} public void Test1() { MyClass myObject = new MyClass(); myObject.level = 1; myObject.timeElapsed = 47.5f; myObject.playerName = "Dr Charles Francis"; json = JsonUtility.ToJson(myObject); Debug.LogError(json); // DesTets(); WritOBJ(); } public void DesTets() { MyClass Obj = JsonUtility.FromJson<MyClass>(json); Debug.LogError(Obj.level); } void WritOBJ() { StreamWriter streamWriter = new StreamWriter(path,true); // byte[] data = System.Text.Encoding.Unicode.GetBytes(json); streamWriter.Write(json); streamWriter.Close(); ReadOBJ(); } void ReadOBJ() { StreamReader streamReader = new StreamReader(path); string str= streamReader.ReadToEnd(); streamReader.Close(); MyClass obj = JsonUtility.FromJson<MyClass>(str); Debug.LogError(obj.level + " " + obj.playerName + " " + obj.timeElapsed); } }