1
我已经创建了一个加载文件来跟踪游戏中创建的所有字符。有15个字符的限制,所以无论何时玩家创建一个新角色,我需要检查是否达到了此限制。但是,当我尝试修改加载文件中的对象容器并重新序列化它时,我最终将我的字符索引恢复为0.是否有人能看到我的错误?BinaryFormatter似乎是追加而不是覆盖
BinaryFormatter binForm = new BinaryFormatter();
//check load data file and add new player to the list if char limit is not reached
using (FileStream fs = File.Open("./saves/loadData.vinload", FileMode.Open))
{
PlayerLoadData pLoad = (PlayerLoadData)binForm.Deserialize(fs);
Debug.Log(pLoad.charIndex);
if (pLoad.charIndex < 15)
{
pLoad.names[pLoad.charIndex] = finalName;
pLoad.charIndex += 1;
/*
Debug.Log(pLoad.charIndex);
foreach(string n in pLoad.names)
{
Debug.Log(n);
}*/
binForm.Serialize(fs, pLoad);
}
else
{
manifestScenePopups.TooManyChars();
return;
}
}
试过但我得到一个异常:SerializationException:serializationStream支持查找,但其长度为0 [编辑]此外,我事先检查文件,以确保它存在并包含基础对象容器。如果不存在,则创建它。 – Jaja 2015-04-04 22:34:32
@Jaja你应该分两步完成。使用'FileMode.Open'来反序列化。然后使用'FileMode.Create'来覆盖现有的数据。 – EZI 2015-04-04 22:36:57
谢谢你,我用另一个使用语句进行序列化,并且工作。 – Jaja 2015-04-04 22:43:21