我开始使用 Windows 窗体(不使用 XNA)在 C# 中编写游戏代码,并且我正在尝试弄清楚如何保存/加载。
现在我只有主窗体、一些用户控件和一个单独项目中的一个类。
我一直在寻找一个像样的教程,这是我找到的最好的教程:
http://www.codeproject.com/Articles/1789/Object-Serialization-using-C
这种方法适用于 Windows 窗体游戏吗?还是我可以采用更好的方法?
此外,如果这是最佳途径,我如何将来自多个不同类的数据保存在一个保存文件中,然后将数据读入?
很遗憾,我没有任何相关代码。任何提示将不胜感激。
连续序列化保存类实例计数,然后保存类实例,然后反序列化类计数及其实例。
一种写法和一种读法
使用格式化程序进行序列化和反序列化..
保存所有当前状态,包括生命计数和游戏状态、玩家位置和其他属性。
例如看这个:
阅读:
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(op.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
FCnt = (int)formatter.Deserialize(stream);
for (int i = 0; i < FCnt; i++)
{
facility[i] = (Facility)formatter.Deserialize(stream);
}
stream.Close();
写作:
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(FN, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, FCnt);
for (int i = 0; i < FCnt; i++)
{
formatter.Serialize(stream, facility[i]);
}
stream.Close();