Xna 在Xbox游戏项目中通过内容管道编写

Xna 在Xbox游戏项目中通过内容管道编写,xna,save,xbox360,content-pipeline,Xna,Save,Xbox360,Content Pipeline,我正在创建一个Xbox应用程序,我的内容管道有这个问题。 加载.xnb文件不是问题,但我似乎找不到任何关于通过内容管道编写的有用教程。每当用户按下定制的“保存”按钮时,我都想编写一个XML。 我在网上搜索过“保存游戏状态”等,但到目前为止,我还没有找到一个解决方案 那么,总结一下:如果调用Save()方法,有没有一种方法可以通过内容管道写入数据(XML格式) Cheerz在XNA游戏中保存和加载包含一系列异步方法调用。您将在Microsoft.Xna.Framework.Storage命名空间中

我正在创建一个Xbox应用程序,我的内容管道有这个问题。 加载.xnb文件不是问题,但我似乎找不到任何关于通过内容管道编写的有用教程。每当用户按下定制的“保存”按钮时,我都想编写一个XML。 我在网上搜索过“保存游戏状态”等,但到目前为止,我还没有找到一个解决方案

那么,总结一下:如果调用Save()方法,有没有一种方法可以通过内容管道写入数据(XML格式)


Cheerz

在XNA游戏中保存和加载包含一系列异步方法调用。您将在Microsoft.Xna.Framework.Storage命名空间中找到所需的对象

具体来说,您需要一个StorageDevice和一个StorageContainer

private static StorageDevice mStorageDevice;
private static StorageContainer mStorageContainer;
要保存:

public static void SaveGame()
{
   // Call this static method to begin the process; SaveGameDevice is another method in your class
   StorageDevice.BeginShowSelector(SaveGameDevice, null);
}

// this will be called by XNA sometime after your call to BeginShowSelector
// SaveGameContainer is another method in your class
private static void SaveGameDevice(IAsyncResult pAsyncResult)
{       
   mStorageDevice = StorageDevice.EndShowSelector(pAsyncResult);
   mStorageDevice.BeginOpenContainer("Save1", SaveGameContainer, null);
}

// this method does the actual saving
private static void SaveGameContainer(IAsyncResult pAsyncResult)
{
   mStorageContainer = mStorageDevice.EndOpenContainer(pAsyncResult);

   if (mStorageContainer.FileExists("save.dat"))
      mStorageContainer.DeleteFile("save.dat");

   // in my case, I have a BinaryWriter wrapper that I use to perform the save
   BinaryWriter writer = new BinaryWriter(new System.IO.BinaryWriter(mStorageContainer.CreateFile("save.dat")));

   // I save the gamestate by passing the BinaryWriter
   GameProgram.GameState.SaveBinary(writer);

   // then I close the writer
   writer.Close();     

   // clean up
   mStorageContainer.Dispose();
   mStorageContainer = null;
}
负载非常相似:

public static void LoadGame()
{
    StorageDevice.BeginShowSelector(LoadGameDevice, null);
}

private static void LoadGameDevice(IAsyncResult pAsyncResult)
{
   mStorageDevice = StorageDevice.EndShowSelector(pAsyncResult);
   mStorageDevice.BeginOpenContainer("Save1", LoadGameContainer, null);
}

private static void LoadGameContainer(IAsyncResult pAsyncResult)
{
   mStorageContainer = mStorageDevice.EndOpenContainer(pAsyncResult))

   // this is my wrapper of BinaryReader which I use to perform the load
   BinaryReader reader = null;

   // the file may not exist
   if (mStorageContainer.FileExists("save.dat"))
   {
      reader = new BinaryReader(new System.IO.BinaryReader(mStorageContainer.OpenFile("save.dat", FileMode.Open)));

      // pass the BinaryReader to read the data
      GameProgram.LoadGameState(reader);
      reader.Close();
   }

   // clean up
   mStorageContainer.Dispose();
   mStorageContainer = null;
}