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

时间:2016-03-19 15:18:40

标签: c# file unity3d fileinputstream fileoutputstream

我想在开始游戏后将我的游戏数据保存到文件中,然后在关闭/开始游戏之后我想要从该文件加载数据。我尝试使用this解决方案,但是当我开始然后停止模拟时,我看不到文件indicatorsInfo.dat,但是Debug.Log()说它存在。无论如何它不会加载数据,出了什么问题?

enter image description here

    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个变量的值),但我无法加载它。
  3. enter image description here

1 个答案:

答案 0 :(得分:3)

在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