Registering and loading content in MonoGame

Page update date :
Page creation date :

In this section, I'll show you how to prepare and import an image file to create a texture for drawing as a sprite.

Prepare the image to load first. It doesn't matter where you are.

読み込む画像を用意

After you create a monoGame project, double-click the Content.mgcb file in the Contents folder to open it.

「Content.mgcb」ファイルを開く

If you only see the text in Visual Studio and monoGame Pipeline doesn't start, start it from the Start menu. After startup, open the "Content.mgcb" file from "File" - "Open" in the menu.

「MonoGame Pipeline」を起動

When you open the "Content.mgcb" file, "MonoGame Pipeline" will start. MonoGame Pipeline is similar to the configuration in the XNA Game Studio Content folder, so if you've ever used XNA Game Studio, you'll be familiar with it.

「MonoGame Pipeline」が起動

Register the image you want to use. Unfortunately, it can not be added by drag and drop, so add it from the menu.

When you select "Edit" >Add >Existing Item from the menu, the file selection dialog is displayed, so select the image file you want to use.

画像ファイルの選択

When you select a file, you can choose to copy and add the file or add it as a link.

You can choose either, but we're copying and adding them here.

コピーとして追加するかリンクとして追加するか

The selected image has been added under Content. When you select an image file, you can select importers and processors just like XNA Game Studio. You can also change the parameters, but for some reason the color information does not seem to be changed.

追加された画像

I will try to build as it is for the time being. You can think of it as the same build when you created the .xnb file that was in XNA.

Select Build > Build from the menu.

ビルドを行う

The mgcb file is a kind of project file, so you'll be asked if you want to save it. Select Yes to save.

プロジェクト保存確認

A message appears on the right when the build starts. Normal completion or abend, build time, and so on. If there is an error, a detailed message is displayed and you will be corrected accordingly.

ビルド結果

When the build is complete, the content is ready.

The code to import an image and draw it as a sprite is exactly the same as xna. Just include the additional part of the code.

Defines a texture that contains the imported image in the field.

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

/// <summary>
/// テクスチャー
/// </summary>
private Texture2D texture = null;

Loads and creates a texture within the LoadContent method. By default, the asset name is the file name (no extension).

protected override void LoadContent()
{
  // Create a new SpriteBatch, which can be used to draw textures.
  spriteBatch = new SpriteBatch(GraphicsDevice);

  // TODO: use this.Content to load your game content here

  // テクスチャーをコンテンツパイプラインから読み込む
  this.texture = this.Content.Load<Texture2D>("XNATips");
}

The drawing portion of the sprite in the Draw method. It is exactly the same code as XNA.

protected override void Draw(GameTime gameTime)
{
  GraphicsDevice.Clear(Color.CornflowerBlue);

  // TODO: Add your drawing code here

  // スプライトの描画準備
  this.spriteBatch.Begin();

  // スプライトを描画する
  this.spriteBatch.Draw(this.texture, new Vector2(100.0f, 100.0f), Color.White);

  // スプライトの一括描画
  this.spriteBatch.End();

  base.Draw(gameTime);
}

When you run it, you can see that the sprite appears in the imported image.

スプライトの表示