无法将数据保存到文件,然后将其加载到 Unity3d 中

Can't save data to file and then load it in Unity3d

我想在开始游戏后将我的游戏数据保存到文件中,然后在 close/start 再次游戏后我想从该文件中加载数据。我尝试使用 this 解决方案,但是当我开始然后停止模拟时,我没有看到文件 indicatorsInfo.dat,但是 Debug.Log() 说它存在。反正它不加载数据,怎么了?

    using UnityEngine;
    using System;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;

    public class GAMEMANAGERFileIO : MonoBehaviour
    {
        void OnEnable()
        {        
            LoadFromFile();
            SaveToFile();
        }

        void OnDisable()
        {        
            SaveToFile();
        }

        public void SaveToFile()
        {
            GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
            GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();            

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            FileStream fileStream = File.Create(Application.persistentDataPath + "/indicatorsInfo.dat"); // open file
            IndicatorsInfo indicatorsInfo = new IndicatorsInfo();

            // Initialise data of the class IndicatorsInfo
            //...

            // save data to the file. Serialize([to where we want to save], [what we want to save])
            binaryFormatter.Serialize(fileStream, indicatorsInfo);

========== EDIT 1 ======================================================
        File.WriteAllText("C:/Users/dima/Desktop/exampleSaveData.txt",
            indicatorsInfo.walkSpeedTemfFile.ToString() + ", " +
            indicatorsInfo.runSpeedTemfFile + ", " +
            indicatorsInfo.jumpForceTemfFile + ", " +
            indicatorsInfo.enduranceTemfFile);
========================================================================

            fileStream.Close();
            Debug.Log("saved"); // this works
        }

        public void LoadFromFile()
        {
            // check if file exisits before we will try to open it
            if(File.Exists(Application.persistentDataPath + "/indicatorsInfo.dat"))
            {
                GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
                GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();

                BinaryFormatter binaryFormatter = new BinaryFormatter();
                FileStream fileStream = File.Open(Application.persistentDataPath + "/indicatorsInfo.dat", FileMode.Open); // open file
                IndicatorsInfo indicatorsInfo = (IndicatorsInfo)binaryFormatter.Deserialize(fileStream); // read from file
                fileStream.Close(); // close file

                // Initialise local data with values from the class IndicatorsInfo
                //...

========== EDIT 1 ======================================================
            File.ReadAllText("C:/Users/dima/Desktop/exampleSaveData.txt");
========================================================================
            }
        }
    }

    // clear class with data, which we will store to file
    [Serializable]
    class IndicatorsInfo
    {
        //...
    }

编辑 1

我添加了 File.WriteAllText()File.ReadAllText()。但是我有两个问题:

  1. 需要自己创建txt文件才能保存到里面;
  2. 我可以将数据保存到文件(4 个变量的值),但我无法加载它。

在 Unity 中写入和读取文件非常容易。

// IO crib sheet..
// filePath = Application.persistentDataPath+"/"+fileName;
// check if file exists System.IO.File.Exists(f)
// write to file File.WriteAllText(f,t)
// delete the file if needed File.Delete(f)
// read from a file File.ReadAllText(f)

仅此而已。

string currentText = File.ReadAllText(filePath);

注意事项......

// filePath = Application.persistentDataPath+"/"+fileName;
// YOU MUST USE "Application.persistentDataPath"
// YOU CANNOT USE ANYTHING ELSE
// NOTHING OTHER THAN "Application.persistentDataPath" WORKS
// ALL OTHER OPTIONS FAIL ON ALL PLATFORMS
// YOU CAN >ONLY< USE Application.persistentDataPath IN UNITY