XNA:保存数据到文件和从文件读取数据
2010-10-12 21:50
330 查看
基于最新的XNA Game Studio 4.0的环境下,因为XNA Game Studio 4.0以前的版本有些方法有变动。
前提:已经有一个正确的StorageDevice和定义好要保存的数据。
[Serializable]
public struct SaveGameData
{
public string PlayerName;
public Vector2 AvatarPosition;
public int Level;
public int Score;
}
一.To serialize data to a save game file
1.Create a StorageContainer to access the specified device.
代码
string filename = "savegame.sav";
// Check to see whether the save exists.
if (!container.FileExists(filename))
{
// If not, dispose of the container and return.
container.Dispose();
return;
}
3.Open a Stream object on the file by using the OpenFile method.
// Open the file.
Stream stream = container.OpenFile(filename, FileMode.Open);
4.Create an XmlSerializer object, and then pass the type of the structure that defines your save game data.
CopyXmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
5.Call Deserialize, and then pass the Stream object.
Deserialize returns a copy of the save game structure populated with the data from the save game file. (You will have to cast the return value from Object to your type.)
SaveGameData data = (SaveGameData)serializer.Deserialize(stream);
6.Close the Stream.
// Close the file.
stream.Close();
7.Dispose the StorageContainer.
// Dispose the container.
container.Dispose();
前提:已经有一个正确的StorageDevice和定义好要保存的数据。
[Serializable]
public struct SaveGameData
{
public string PlayerName;
public Vector2 AvatarPosition;
public int Level;
public int Score;
}
一.To serialize data to a save game file
1.Create a StorageContainer to access the specified device.
代码
string filename = "savegame.sav";
// Check to see whether the save exists.
if (!container.FileExists(filename))
{
// If not, dispose of the container and return.
container.Dispose();
return;
}
3.Open a Stream object on the file by using the OpenFile method.
// Open the file.
Stream stream = container.OpenFile(filename, FileMode.Open);
4.Create an XmlSerializer object, and then pass the type of the structure that defines your save game data.
CopyXmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
5.Call Deserialize, and then pass the Stream object.
Deserialize returns a copy of the save game structure populated with the data from the save game file. (You will have to cast the return value from Object to your type.)
SaveGameData data = (SaveGameData)serializer.Deserialize(stream);
6.Close the Stream.
// Close the file.
stream.Close();
7.Dispose the StorageContainer.
// Dispose the container.
container.Dispose();
相关文章推荐
- XNA:保存数据到文件和从文件读取数据
- XNA 3.0——将数据保存到文件,从文件读取数据
- 从零学python系列之从文件读取和保存数据
- QT和VS读取.txt文件中的数据并保存为数组
- Matlab读取和保存TXT文件数据
- Pull解析器读取XML文件,及保存数据到xml文件中
- 读取含有json格式数据的txt文件 并且把数据保存到数据库
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- 流和文件:保存和读取字节级数据
- PHP Excel 读取xls,xlsx文件并出入数据然后保存
- java读取Excel数据,然后写入到txt文件,并批量保存到oracle数据库中
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 使用NPOI插件读取excel模版修改数据后保存到新目录新文件中
- JAVA读取Oracle数据库BLOB字段数据文件并保存到本地文件
- android往文件中保存和读取数据
- Ubuntu10.04中利用V4L2读取摄像头数据并保存成文件
- Ubuntu10.04中利用V4L2读取摄像头数据并保存成文件【转】
- Matlab批量读取元胞数组中数据保存到txt文件中(WIDER FACE数据集中,读取人脸标注矩形框信息)
- 流和文件:保存和读取字符级数据以及随机访问
- 个人学习代码保存:例11.读取Excel文件中的数据