惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
S
SegmentFault 最新的问题
Jina AI
Jina AI
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
B
Blog
博客园 - 【当耐特】
博客园 - Franky
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta

博客园 - 十二号的国王

【iOS开发】在ARC项目中使用非ARC文件 实现ZUNE上软件商城的软件星级推荐效果 [Windows Phone 7]开发分享图片的插件(2) [Windows Phone 7]如何判断手机是否有网络连接 [Windows Phone 7]开发分享图片的插件(1) [Windows Phone 7]如何导航页面和页面间传值 [Windows Phone 7]UI对屏幕的自适应的处理 [Windwos Phone 7] Accelerometer [Windwos Phone 7] 获取设备相关信息 [Windows Phone 7] 常用资源(转) [Windows Phone 7] Storage XNA项目运行错误:No suitable graphics card found. POOM(Pocket Outlook Object Model)开发介绍及应用(转) 剪贴板剪切/复制与粘贴文件+1个待解决的问题 c#枚举-Enum C#读写INI配置文件(转) C#3.0新增特性 - 十二号的国王 - 博客园 关于DBNull vs 快捷键
XNA:保存数据到文件和从文件读取数据
十二号的国王 · 2010-10-12 · via 博客园 - 十二号的国王

基于最新的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.

代码

// Open a storage container.
IAsyncResult result = device.BeginOpenContainer("StorageDemo"nullnull);
// Wait for the WaitHandle to become signaled.
result.AsyncWaitHandle.WaitOne();
StorageContainer container 
= device.EndOpenContainer(result);
// Close the wait handle.
result.AsyncWaitHandle.Close();

 2.Call FileExists to determine if an earlier save game file exists, and if it does, call DeleteFile to delete it.

string filename = "savegame.sav";
// Check to see whether the save exists.
if (container.FileExists(filename))
// Delete it so that we can create one fresh.
container.DeleteFile(filename);

 3.Create a Stream object on the file by using the CreateFile method.

// Create the file.
Stream stream = container.CreateFile(filename);

4.Create an XmlSerializer object, passing the type of the structure that defines your save game data.

// Convert the object to XML data and put it in the stream.
XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));

 5.Call Serialize, and then pass the Stream and the data to serialize.

The XmlSerializer converts data in the structure to XML, and uses the Stream to write the data into the file.

serializer.Serialize(stream, data);

 6.Close the Stream.

// Close the file.
stream.Close();

 7.Dispose the StorageContainer to commit the changes to the device.

// Dispose the container, to commit changes.
container.Dispose();

 二.To read serialized data from a save game file

1.Create a StorageContainer to access the specified device.

代码

// Open a storage container.
IAsyncResult result = device.BeginOpenContainer("StorageDemo"nullnull);
// Wait for the WaitHandle to become signaled.
result.AsyncWaitHandle.WaitOne();
StorageContainer container 
= device.EndOpenContainer(result);
// Close the wait handle.
result.AsyncWaitHandle.Close();

 2.Call FileExists to determine if the save game exists.

代码

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();