예제 #1
0
    private void SaveJson()
    {
        ChooseSave save = new ChooseSave();

        foreach (TurretType tt in Enum.GetValues(typeof(TurretType)))
        {
            if (tt.ToString() == mWhichToggle1)
            {
                save.turretType1 = tt;
            }
            if (tt.ToString() == mWhichToggle2)
            {
                save.turretType2 = tt;
            }
        }
        string filePath = Application.dataPath + "/StreamingFile" + "/Json.json";
        //利用JsonMapper将save对象转换为Json格式的字符串
        string saveJsonStr = JsonMapper.ToJson(save);
        //将这个字符串写入到文件中
        //创建一个StreamWriter,并将字符串写入文件中
        StreamWriter sw = new StreamWriter(filePath);

        sw.Write(saveJsonStr);
        //关闭StreamWriter
        sw.Close();
        Debug.Log("保存成功");
    }
예제 #2
0
    private void LoadSave()
    {
        string filePath = Application.dataPath + "/StreamingFile" + "/Json.json";

        if (File.Exists(filePath))
        {
            StreamReader sr      = new StreamReader(filePath);           //创建一个StreamReader
            string       jsonStr = sr.ReadToEnd();                       //将读取到的流赋值给jsonStr
            sr.Close();                                                  //关闭流
            ChooseSave save = JsonMapper.ToObject <ChooseSave>(jsonStr); //将字符串jsonStr转换为Save对象
            Debug.Log("Load OJ8K");
            mType1 = save.turretType1;
            mType2 = save.turretType2;
        }
        else
        {
            Debug.Log("存档文件不存在");
        }
    }