我想在开始游戏后将我的游戏数据保存到文件中,然后在关闭/开始游戏之后我想要从该文件加载数据。我尝试使用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()
。但我有两个问题:
答案 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