我将我的对象序列化,并将其统一放在参考资料文件夹中。我的游戏应该复制这个文件到Application.persistentDataPath路径和反序列化。我收到了错误:Failed to serialize. Reason: No map for object '201326592'.
我的“副本”代码:
public void CopyAndDeserialize()
{
saveData.itemsOfClothing = new List<ItemsOfClothingData.ItemOfClothing>();
TextAsset resourcesTextData = (TextAsset)Resources.Load("items");
path = Path.Combine(StorageManager.Instance.GlobalPath + _saveFileName);
if(!File.Exists(path))
{
File.Create(path);
}
File.WriteAllText(path, resourcesTextData.text);
DeserializeData();
}DeserializeData:
public void DeserializeData()
{
path = Path.Combine(StorageManager.Instance.GlobalPath + _saveFileName);
if (File.Exists(path))
{
fileStream = new FileStream(path, FileMode.Open);
BinaryFormatter binaryFormatter = new BinaryFormatter();
try
{
saveData = (ItemsOfClothingData)binaryFormatter.Deserialize(fileStream);
}
catch (SerializationException e)
{
MonoBehaviour.print("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fileStream.Close();
}
fileStream.Close();
}
}谢谢你的帮忙!
发布于 2022-08-18 06:20:49
问题解决了。使用Resource.Load<TextAsset>(path).bytes获取字节数组,并像往常一样进行反序列化。
发布于 2022-08-17 08:11:42
该文件不是二进制格式,请使用System.IO.File.ReadAllLines(string filepath)或System.IO.File.ReadAllText(string filepath)正确读取它。
有关更多信息,请参见Microsoft文档:https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalllines?view=net-6.0
https://stackoverflow.com/questions/73384588
复制相似问题