C# 保存字节xna,保存某些数字时出现问题
我有点困惑,以下是我到目前为止所做的:C# 保存字节xna,保存某些数字时出现问题,c#,xna,isolatedstorage,C#,Xna,Isolatedstorage,我有点困惑,以下是我到目前为止所做的: public class SaveLoadSystem { String SAVEFILENAME = "gameSave2.sav"; Stats stats = new Stats(); GameProgressInformation gameInfo = new GameProgressInformation(); public SaveLoadSystem() { IsolatedSto
public class SaveLoadSystem
{
String SAVEFILENAME = "gameSave2.sav";
Stats stats = new Stats();
GameProgressInformation gameInfo = new GameProgressInformation();
public SaveLoadSystem()
{
IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
// open isolated storage, and write the savefile.
if (savegameStorage.FileExists(SAVEFILENAME))
{
IsolatedStorageFileStream fs = null;
try
{
fs = savegameStorage.OpenFile(SAVEFILENAME, System.IO.FileMode.Open);
}
catch (IsolatedStorageException e)
{
}
if (fs != null)
{
byte[] saveBytes = new byte[256];
int count = fs.Read(saveBytes, 0, 256);
if (count > 0)
{
for (int i = 0; i < stats.GetLevels; i++)
{
GameProgressInformation.LevelUnlockInfo levelInfo = new GameProgressInformation.LevelUnlockInfo();
// Think line below is wrong.
levelInfo.score = saveBytes[i * 4];
GameProgressInformation.levels.Add(levelInfo);
}
fs.Close();
}
}
}
else
{
for (int i = 0; i < stats.GetLevels; i++)
{
GameProgressInformation.LevelUnlockInfo levelInfo = new GameProgressInformation.LevelUnlockInfo();
GameProgressInformation.levels.Add(levelInfo);
}
}
}
public void Save()
{
// Save the game.
IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
// open isolated storage, and write the savefile.
IsolatedStorageFileStream fs = null;
fs = savegameStorage.OpenFile(SAVEFILENAME, System.IO.FileMode.Create);
if (fs != null)
{
for (int i = 0; i < GameProgressInformation.levels.Count; i++)
{
byte[] scoreBytes = System.BitConverter.GetBytes(GameProgressInformation.levels[i].score);
fs.Write(scoreBytes, 0, scoreBytes.Length);
}
fs.Close();
}
}
}
我的困惑是如何在加载时获取所有必要的二进制数据,同时确保将正确的数据分配到正确的级别。我想这应该是一套4张的。感谢您的帮助 想出了怎么做。对流读取器的工作原理有错误的理解。只需按照与编写数据完全相同的顺序读取数据即可。为什么要在保存二进制文件的方法中使用IsolatedStorageFileStream.Read?构造函数加载该文件,因此读取应该在那里。我遇到的问题是fs.read一次读取整个文件,而实际上我要么一次只想读取一个级别,要么我想要一种能够始终确保一次读取正确数量的数据的方式。对于未来的读者来说,详细说明错误可能会有所帮助。