unity json

2020-12-15  本文已影响0人  MakerSir

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<MyClass>(json);

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

        List<MyClass> ml = new List<MyClass>();

        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);

    }

上一篇下一篇

猜你喜欢

热点阅读