在Manager类中
将文件存放在StreamingFile文件中
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[System.Serializable]
public struct Save
{
}
public void SaveGame()
{
saveByBin();
}
private void SaveByBin()
{
//序列化过程(将save转化为字节流)
//创建Save对象并保存当前游戏状态
Save save = CreateSaveGO();
//创建一个二进制格式化程序
BinaryFormatter bf = new BinaryFormatter();
//创建一个文件流
FileStream fileStream = File.Create(Application.dataPath + "/StreamingFile" + "/byBin.txt");
//用二进制格式化程序的序列化方法来序列化Save对象,参数:创建的文件流和需要序列化的对象
bf.Serialize(fileStream,save);
//关闭流
fileStream.Close;
if(File.Exists(Application.dataPath + "/StreamingFile" + "/byBin.txt")){
//若保存文件存在则执行
}
}
public void LoadGame()
{
LoadByBin();
}
private void LoadByBin()
{
if(!File.Exists(Application.dataPath + "/StreamingFile" + "/byBin.txt")){
return;
}
//反序列化过程
//创建一个二进制格式化程序
BinaryFormatter bf = new BinaryFormatter();
//打开一个文件流
FileStream fileStream = File.Open(Application.da