C# 序列化异常:尝试反序列化空流
我正在制作一个游戏,并将其用作我的保存/加载系统,然后将数据存储在单独的“GameData”脚本中。当我第一次尝试开始游戏时,出现以下错误: 序列化异常:尝试反序列化空流 我尝试检查长度是否大于0,并尝试了try/catch版本。它们都会得到相同的错误,并陷入循环。也许有一种方法可以检查你是否是第一次打开游戏并先保存?我宁愿在加载之前找到一种方法来确保文件中有数据。或者我可以创建一个数据,而不是使用C# 序列化异常:尝试反序列化空流,c#,unity3d,exception,serialization,binaryformatter,C#,Unity3d,Exception,Serialization,Binaryformatter,我正在制作一个游戏,并将其用作我的保存/加载系统,然后将数据存储在单独的“GameData”脚本中。当我第一次尝试开始游戏时,出现以下错误: 序列化异常:尝试反序列化空流 我尝试检查长度是否大于0,并尝试了try/catch版本。它们都会得到相同的错误,并陷入循环。也许有一种方法可以检查你是否是第一次打开游戏并先保存?我宁愿在加载之前找到一种方法来确保文件中有数据。或者我可以创建一个数据,而不是使用null?任何帮助都将不胜感激 这是脚本的长度版本 using UnityEngine; usin
null
?任何帮助都将不胜感激
这是脚本的长度版本
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public static class SaveSystem
{
public static void SaveData(PlayerCollision playerCol)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Create);
GameData data = new GameData(playerCol);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameData LoadData()
{
string path = Application.persistentDataPath + "/player.fun";
if (File.Exists(path) && path.Length > 0)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
GameData data = formatter.Deserialize(stream) as GameData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file was not found in " + path);
return null;
}
}
}
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public static class SaveSystem
{
public static void SaveData(PlayerCollision playerCol)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Create);
GameData data = new GameData(playerCol);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameData LoadData()
{
try
{
string path = Application.persistentDataPath + "/player.fun";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
GameData data = formatter.Deserialize(stream) as GameData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file was not found in " + path);
return null;
}
}
catch (System.Exception e)
{
Debug.LogError("Error Loading Save " + e);
return null;
}
}
}
这是脚本的try/catch版本
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public static class SaveSystem
{
public static void SaveData(PlayerCollision playerCol)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Create);
GameData data = new GameData(playerCol);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameData LoadData()
{
string path = Application.persistentDataPath + "/player.fun";
if (File.Exists(path) && path.Length > 0)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
GameData data = formatter.Deserialize(stream) as GameData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file was not found in " + path);
return null;
}
}
}
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public static class SaveSystem
{
public static void SaveData(PlayerCollision playerCol)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Create);
GameData data = new GameData(playerCol);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameData LoadData()
{
try
{
string path = Application.persistentDataPath + "/player.fun";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
GameData data = formatter.Deserialize(stream) as GameData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file was not found in " + path);
return null;
}
}
catch (System.Exception e)
{
Debug.LogError("Error Loading Save " + e);
return null;
}
}
}
我只是发布最终的解决方案,因为有太多人修复了错误,而不会在将来分享和发布帮助任何人。 正如上面的“Dymaniod”指出的,我想使用“stream.length”而不是“path.length”,因为“WarmedxMints”也指出了统一答案,路径的长度总是大于0。 我仍然有一个加载错误循环。最后一个有效的脚本如下。 感谢你们两位的帮助
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public static class SaveSystem
{
public static void SaveData(PlayerCollision playerCol)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Create);
GameData data = new GameData(playerCol);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameData LoadData(PlayerCollision playerCol)
{
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Open);
if (File.Exists(path) && stream.Length > 0)
{
BinaryFormatter formatter = new BinaryFormatter();
GameData data = formatter.Deserialize(stream) as GameData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file was not found in " + path);
BinaryFormatter formatter = new BinaryFormatter();
GameData data = new GameData(playerCol);
formatter.Serialize(stream, data);
stream.Close();
return data;
}
}
}
path.Length
不是您想要的。改为选中流的长度。您的文件似乎有0个字节。谢谢您的帮助!它消除了错误。然后,它开始运行debug.log错误消息的循环,但我通过在“else{}”下运行save函数,而不是仅仅使用“returnnull”,成功地解决了这个问题。非常感谢。既然FileStream
是一个IDisposable
你应该使用使用(FileStream=newfilestream(path,FileMode.Create){…}
那么你就不必隐式调用流。关闭
我也不会使用新的FileStream
,但实际上更像是使用(var stream=File.Open)(路径,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite))