zoukankan      html  css  js  c++  java
  • XNA:保存数据到文件和从文件读取数据

    基于最新的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();
  • 相关阅读:
    解决 Windows Server 2008 R2 上 Windows Update 无法失败,提示 8024402F
    【UWP】实现 FindAncestor 绑定
    实现在 .net 中使用 HttpClient 下载文件时显示进度
    【UWP】手动实现 WebAuthenticationBroker
    记录使用 Cake 进行构建并制作 nuget 包
    w筛选系数=(1+错次)/(1+总次)
    WZP身份溯源策略(World Zero Protection),宜分宜合、自主可控的实名认证体系
    WZP报文封装协议(Web Zip Protocol),安全可嵌套的传输协议
    WZP安全配置方案,针对通讯技术的安全措施
    WZP网络结构模型,对OSI参考模型和TCP/IP模型的改进
  • 原文地址:https://www.cnblogs.com/ssqjd/p/1849211.html
Copyright © 2011-2022 走看看