我想在启动游戏后将游戏的数据保存到文件中,然后在关闭/启动游戏后再次从该文件中加载数据。我尝试使用此解决方案,但是当我开始然后停止仿真时,我看不到file 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()
。但是我有两个问题:
在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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句