1

Xbox アプリケーションを作成していますが、コンテンツ パイプラインでこの問題が発生しています。.xnb ファイルのロードは問題ではありませんが、コンテンツ パイプラインを介した書き込みに関する役立つチュートリアルが見つからないようです。ユーザーがカスタムメイドの「保存」ボタンを押すたびに XML を書きたいと思います。「ゲームの状態を保存する」などについてウェブを検索しましたが、これまでのところ、私の場合の解決策は見つかりませんでした。

要約すると、Save() メソッドが呼び出された場合、コンテンツ パイプラインを介して (XML 形式で) データを書き込む方法はありますか?

乾杯

4

1 に答える 1

0

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;
}
于 2012-03-20T01:28:45.603 に答える