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