Get the path of the storage container
summary
Gets the storage container path for the selected storage device.
Operating environment
Prerequisites
Supported XNA Versions |
|
Supported Platforms |
|
Windows Required Vertex Shader Version | 1.1 |
Windows Required Pixel Shader Version | 1.1 |
Operating environment
platform |
How to work with the sample
Works keyboardXbox | 360 controllermouse | ||
---|---|---|---|
Choosing a storage device | A | A | - |
substance
About the Storage Device Selection UI
Xbox 360 has multiple storage devices, including a hard drive and a memory unit. When saving data for games, etc., Xbox 360 comes standard with a UI (user interface) that allows you to choose which device to save data to. (See figure above)
In this sample, we use this UI to get the destination path.
Adding a Game Services Component
The save device selection UI works asynchronously with the actual game. To accomplish this, you need to add a game service and have the game service drive the save device selection UI.
// 非同期処理を行うためのゲームサービスコンポーネントを追加
this.Components.Add(new GamerServicesComponent(this));
GamerServicesComponent
constructor
Create an instance of GamerServicesComponent to handle the gamer service.
game | Game | This component and its associated game class. |
Start displaying the storage device selection UI
To display the save device selection UI, call the "Guide.BeginShowStorageDeviceSelector" method in the Update method. However, the save device selection UI will not be displayed at the time of calling this method, but will be called by the game service you just added.
The first argument of the Guide.BeginShowStorageDeviceSelector method specifies the method that will be called after the device is selected in the Save Device Selector UI. You must define this method yourself (see the next section).
The second argument can be set to any data. If you want to use some data after selecting a storage device, set this parameter to your own data.
// ストレージデバイス選択UIを表示するための設定を行います
Guide.BeginShowStorageDeviceSelector(this.GetStorageDevice, null);
Guide.BeginShowStorageDeviceSelector
method
Initiates the process for displaying the storage device selection UI. This operation is done asynchronously with the game process.
callback | AsyncCallback | Sets the method that is called after the save device is selected. |
state | Object | Set your own data to use for asynchronous processing. |
By the way, this save device selection UI is not displayed because it is automatically selected if only one device is connected. It is also not displayed on Windows.
Processing method after selecting a storage device
The method specified in the Guide.BeginShowStorageDeviceSelector method is defined to accept an IAsyncResult.
<summary>
ストレージデバイスを取得するために呼ばれる
</summary>
<param name="result">非同期処理の結果</param>
private void GetStorageDevice(IAsyncResult result)
{
// 結果をもとにストレージデバイスの選択UIを終了してストレージデバイスを取得します
StorageDevice storageDevice = Guide.EndShowStorageDeviceSelector(result);
if (storageDevice != null && storageDevice.IsConnected)
{
// ストレージデバイスの取得に成功し、接続されている場合 /////
// ストレージコンテナを開きます
using (StorageContainer container = storageDevice.OpenContainer("XNASample"))
{
// コンテナの名前を取得
this.storageContainerName = container.TitleName;
// コンテナで指定されているパスを取得
this.storageContainerPath = container.Path;
}
}
}
This method is called when you have finished the device selection process in the save device selection UI.
You can receive a save device by passing the result passed by the argument (IAsyncResult) to the argument of "Guide.EndShowStorageDeviceSelector". Note that if you cancel the selection, null will be returned.
Guide.EndShowStorageDeviceSelector
method
Displays the save device selection UI and receives the save device.
asyncResult | IAsyncResult | Pass the save device selection result. |
Return Values | StorageDevice | The selected storage device. If the selection is canceled, null is returned. |
Check if the device is actually connected with the "StorageDevice.IsConnected" property and open the container with the "StorageDevice.OpenContainer" method. The argument to the StorageDevice.OpenContainer method is the name of the container. This name identifies the container in which the data is stored.
StorageDevice.IsConnected
property
Returns true if the device is successfully connected; otherwise, false.
StorageDevice.OpenContainer
method
Opens the storage container with the specified name.
titleName | string | The name of the container. |
Return Values | StorageContainer | Retrieves the storage container with the specified name. |
Once you have the container, you can use the StorageContainer.Path property to get the path where the file is stored.
StorageContainer.Path
property
Gets the file path of the user's game save file.
Consider that the storage device selection UI is an asynchronous process
In the sample, while the save device selection UI is displayed, it counts every frame to ensure that it is running asynchronously and displays the count. If you actually run it, you can see that the value of the counter increases in the background even while the device selection UI is displayed.
If you are actually making a game, you have to take this into account and stop the progress of the game while you are selecting a device. Alternatively, you may want to stop the progress, but continue the effects and other screen effects. This area needs to be adjusted to match the actual game.
All Codes
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace GetStorageContainerPath
{
<summary>
ゲームメインクラス
</summary>
public class GameMain : Microsoft.Xna.Framework.Game
{
<summary>
グラフィックデバイス管理クラス
</summary>
private GraphicsDeviceManager graphics = null;
<summary>
スプライトのバッチ化クラス
</summary>
private SpriteBatch spriteBatch = null;
<summary>
スプライトでテキストを描画するためのフォント
</summary>
private SpriteFont font = null;
<summary>
ストレージコンテナの名前
</summary>
private string storageContainerName = "";
<summary>
ストレージコンテナのパス
</summary>
private string storageContainerPath = "";
<summary>
フレームカウント数
</summary>
private int frameCount = 0;
<summary>
直線のキーボード入力の状態
</summary>
private KeyboardState oldKeyboardState = new KeyboardState();
<summary>
直線のゲームパッド入力の状態
</summary>
private GamePadState oldGamePadState = new GamePadState();
<summary>
GameMain コンストラクタ
</summary>
public GameMain()
{
// グラフィックデバイス管理クラスの作成
this.graphics = new GraphicsDeviceManager(this);
// ゲームコンテンツのルートディレクトリを設定
this.Content.RootDirectory = "Content";
// 画面サイズを変更
this.graphics.PreferredBackBufferWidth = 1024;
this.graphics.PreferredBackBufferHeight = 768;
// 非同期処理を行うためのゲームサービスコンポーネントを追加
this.Components.Add(new GamerServicesComponent(this));
}
<summary>
ゲームが始まる前の初期化処理を行うメソッド
グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
</summary>
protected override void Initialize()
{
// コンポーネントの初期化などを行います
base.Initialize();
}
<summary>
ゲームが始まるときに一回だけ呼ばれ
すべてのゲームコンテンツを読み込みます
</summary>
protected override void LoadContent()
{
// テクスチャーを描画するためのスプライトバッチクラスを作成します
this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
// フォントをコンテンツパイプラインから読み込む
this.font = this.Content.Load<SpriteFont>("Font");
}
<summary>
ゲームが終了するときに一回だけ呼ばれ
すべてのゲームコンテンツをアンロードします
</summary>
protected override void UnloadContent()
{
// TODO: ContentManager で管理されていないコンテンツを
// ここでアンロードしてください
}
<summary>
描画以外のデータ更新等の処理を行うメソッド
主に入力処理、衝突判定などの物理計算、オーディオの再生など
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Update(GameTime gameTime)
{
// キーボードの情報取得
KeyboardState keyboardState = Keyboard.GetState();
// ゲームパッドの情報取得
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
// Xbox360 コントローラの BACK ボタンを押したときにゲームを終了させます
if (gamePadState.Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
if ((keyboardState.IsKeyDown(Keys.A) && this.oldKeyboardState.IsKeyUp(Keys.A)) ||
(gamePadState.Buttons.A == ButtonState.Pressed &&
this.oldGamePadState.Buttons.A == ButtonState.Released))
{
// A ボタンが押されたとき /////
// ストレージデバイス選択UIを表示するための設定を行います
Guide.BeginShowStorageDeviceSelector(this.GetStorageDevice, null);
}
// 入力情報を記憶
this.oldKeyboardState = keyboardState;
this.oldGamePadState = gamePadState;
// フレームのカウント
this.frameCount++;
// 登録された GameComponent を更新する
base.Update(gameTime);
}
<summary>
ストレージデバイスを取得するために呼ばれる
</summary>
<param name="result">非同期処理の結果</param>
private void GetStorageDevice(IAsyncResult result)
{
// 結果をもとにストレージデバイスの選択UIを終了してストレージデバイスを取得します
StorageDevice storageDevice = Guide.EndShowStorageDeviceSelector(result);
if (storageDevice != null && storageDevice.IsConnected)
{
// ストレージデバイスの取得に成功し、接続されている場合 /////
// ストレージコンテナを開きます
using (StorageContainer container = storageDevice.OpenContainer("XNASample"))
{
// コンテナの名前を取得
this.storageContainerName = container.TitleName;
// コンテナで指定されているパスを取得
this.storageContainerPath = container.Path;
}
}
}
<summary>
描画処理を行うメソッド
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Draw(GameTime gameTime)
{
// 画面を指定した色でクリアします
this.GraphicsDevice.Clear(Color.CornflowerBlue);
// スプライトの描画準備
this.spriteBatch.Begin();
// テキスト描画
this.spriteBatch.DrawString(this.font,
"A : Data is saved in a file.",
new Vector2(50.0f, 50.0f), Color.White);
// ストレージコンテナの名前
this.spriteBatch.DrawString(this.font,
"StorageContainerTitleName : " + this.storageContainerName,
new Vector2(50.0f, 70.0f), Color.White);
// ストレージコンテナのパス
this.spriteBatch.DrawString(this.font,
"[ Path ]\r\n" + this.storageContainerPath,
new Vector2(50.0f, 90.0f), Color.White);
// フレームカウント
this.spriteBatch.DrawString(this.font,
"FrameCount : " + this.frameCount,
new Vector2(this.graphics.PreferredBackBufferWidth - 200.0f, 50.0f), Color.White);
// スプライトの一括描画
this.spriteBatch.End();
// 登録された DrawableGameComponent を描画する
base.Draw(gameTime);
}
}
}