LitJson数据持久化


数据持久化存数据
第一步:判断路径下有无你需要的文本文件

有则读取json中数据,拿到你需要的数据,接收下来

点击查看代码
        //LitJson读取数据
        public int LitJsonRead()
        {
            string str = Application.streamingAssetsPath + "/UserRecord";
            if (File.Exists(str))
            {
                JsonData jsdata = JsonMapper.ToObject(File.ReadAllText("Assets/StreamingAssets/UserRecord"));
                m_BestScore = (int)jsdata["best_score"];
                return m_BestScore;

            }
            else
            {
                //File.CreateText(str);
                LitJsonWrite(0);
                return 0;
            }
        }
然后就可以在需要的地方调用写入json数据的方法了,这里我只保存了一个数据,多的话也可以用List,字典之类
点击查看代码
        public void LitJsonWrite(int saveData)
        {
            JsonData m_bestScore = new JsonData();
            m_bestScore["best_score"] = saveData;
            //Debug.Log(saveData);
            var saveJsonData = JsonMapper.ToJson(m_bestScore);
            var path = Application.streamingAssetsPath + "/UserRecord";
            StreamWriter sw = new StreamWriter(path);
            sw.Write(saveJsonData);
            sw.Close();
        }
注:只做学习用,如有不足之处希望多多赐教
C