Loading Resources (Contents)
summary
Describes how to import resources such as image files and model files.
Operating environment
Prerequisites
Supported XNA Versions |
|
Supported Platforms |
|
Windows Required Vertex Shader Version | 2.0 |
Windows Required Pixel Shader Version | 2.0 |
Operating environment
platform |
|
substance
Image files, model files, sound files, etc. used in the game should be added to the project in advance.
Add a file that will be a resource. Images and models are basically added to the "Content" project. Right-click the Content project and choose Existing Item.
The Add Existing Item dialog opens. Select the files you want to add and click the Add button.
You can see that the file has been added to the project as shown.
If you don't want to add it from the dialog, you can also add it by dragging and dropping from the explorer. However, please note that in Windows Vista and Windows 7, you cannot drag and drop if the permissions of the launched application are different.
Once you've added the file, create a program to read it. When you add a resource to a project, the Content Pipeline uses the . .xnb file, which is perfect for use in games. To read it, use the "ContentManager" class. The ContentManager class is built into the Game class.
To load a resource (content), use the "ContentManager.Load" method. This time, it is read appropriately and is not used for another purpose. Since the class to be created differs depending on the file format to be loaded, a class is created according to the file by specifying "Texture2D" or "SpriteFont" as the type parameter of the Load method. Also, since the parameter passed in the argument is relative to the content project or the folder specified in "ContentManager.RootDirectory", you must specify the file name (without the extension) including the folder name (the asset name to be precise).
Texture2D texture = this.Content.Load<Texture2D>("Texture");
SpriteFont font = this.Content.Load<SpriteFont>("Font");
Model xModel = this.Content.Load<Model>("XModel");
ContentManager.Load
method
Reads an asset created by the content pipeline and creates an instance of the class specified in the type parameter. Also, if you specify an asset that has already been created, it returns a reference to an existing instance.
T | No limit | Specify the type of asset. Classes that can be specified by default include "Model", "SpriteFont", "Effect", "Texture2D", "Texture", and "TextureCube", but you can also specify serializable classes such as "string", "List<T>", and classes that you have created yourself. |
assetName | string | Specify the name of the asset you want to import. Since it is a relative path from the specified root directory, you need to write the path if there is a folder. Also, note that the asset name is not the same as the file name. |
Return Values | T | Returns an instance (T) of the loaded asset. If you've loaded the same asset before, it only returns references to the same instance as before. |
The other tips assume that you are loading the content, so please refer to this page if you are unsure.
All Codes
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
namespace LoadResource
{
<summary>
ゲームメインクラス
</summary>
public class GameMain : Microsoft.Xna.Framework.Game
{
<summary>
グラフィックデバイス管理クラス
</summary>
private GraphicsDeviceManager graphics = null;
<summary>
スプライトのバッチ化クラス
</summary>
private SpriteBatch spriteBatch = null;
<summary>
GameMain コンストラクタ
</summary>
public GameMain()
{
// グラフィックデバイス管理クラスの作成
this.graphics = new GraphicsDeviceManager(this);
// ゲームコンテンツのルートディレクトリを設定
this.Content.RootDirectory = "Content";
#if WINDOWS_PHONE
// Windows Phone のデフォルトのフレームレートは 30 FPS
this.TargetElapsedTime = TimeSpan.FromTicks(333333);
// バックバッファサイズの設定
this.graphics.PreferredBackBufferWidth = 480;
this.graphics.PreferredBackBufferHeight = 800;
// フルスクリーン表示
this.graphics.IsFullScreen = true;
#endif
}
<summary>
ゲームが始まる前の初期化処理を行うメソッド
グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
</summary>
protected override void Initialize()
{
// TODO: ここに初期化ロジックを書いてください
// コンポーネントの初期化などを行います
base.Initialize();
}
<summary>
ゲームが始まるときに一回だけ呼ばれ
すべてのゲームコンテンツを読み込みます
</summary>
protected override void LoadContent()
{
// テクスチャーを描画するためのスプライトバッチクラスを作成します
this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
Texture2D texture = this.Content.Load<Texture2D>("Texture");
SpriteFont font = this.Content.Load<SpriteFont>("Font");
Model xModel = this.Content.Load<Model>(@"XModel");
}
<summary>
ゲームが終了するときに一回だけ呼ばれ
すべてのゲームコンテンツをアンロードします
</summary>
protected override void UnloadContent()
{
// TODO: ContentManager で管理されていないコンテンツを
// ここでアンロードしてください
}
<summary>
描画以外のデータ更新等の処理を行うメソッド
主に入力処理、衝突判定などの物理計算、オーディオの再生など
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Update(GameTime gameTime)
{
// Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
// ゲームを終了させます
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
// TODO: ここに更新処理を記述してください
// 登録された GameComponent を更新する
base.Update(gameTime);
}
<summary>
描画処理を行うメソッド
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Draw(GameTime gameTime)
{
// 画面を指定した色でクリアします
this.GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: ここに描画処理を記述します
// 登録された DrawableGameComponent を描画する
base.Draw(gameTime);
}
}
}