unity json

public class MyClass

    {

        public int level;

        public float timeElapsed;

        public string playerName;

    }

    // Start is called before the first frame update

    void Start()

    {

        MyClass myObject = new MyClass();

        myObject.level = 1;

        myObject.timeElapsed = 47.5f;

        myObject.playerName = "zyx";

        string json = JsonUtility.ToJson(myObject);

        print("json: " + json);

        MyClass myObject1 = new MyClass();

        myObject1 = JsonUtility.FromJson(json);

        print("playerName: " + myObject1.playerName);

        List ml = new List();

        myObject.level = 1;

        myObject.timeElapsed = 47.5f;

        myObject.playerName = "zyx";

        ml.Add(myObject);

        myObject.level = 2;

        myObject.timeElapsed = 47.5f;

        myObject.playerName = "wb";

        ml.Add(myObject);

        string jsonToSave = JsonUtility.ToJson(ml);

        print("jsonToSave: " + jsonToSave);


    }

你可能感兴趣的:(unity json)