Registering and loading content in MonoGame

Page updated :

In order to create a texture to draw as a sprite, i'll show you how to prepare and load an image file.

Make the image you want to load first. The place doesn't matter anywhere.

読み込む画像を用意

After you create your MonoGame project, double-click the Content.mgcb file in the Content folder to open it.

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

If visual studio only displays text and monogame pipeline doesn't start, start it from the Start menu. After booting, open the Content.mgcb file from the "File" -Open menu.

「MonoGame Pipeline」を起動

When you open the Content.mgcb file, monogame pipeline starts. MonoGame Pipeline is similar to the configuration in the Content folder of XNA Game Studio, so if you've ever used XNA Game Studio, it's easy to get used to it.

「MonoGame Pipeline」が起動

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

Select the image file you want to use because the file selection dialog is displayed when you select Edit → Add → "Existing Item" from the menu.

画像ファイルの選択

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

You can choose which one, but we're copying and adding it here.

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

The selected image has been added under Content. When you select an image file, Importer and Processor are available for selection, just like XNA Game Studio. You can change the parameters, but for some reason the color information cannot be changed.

追加された画像

Let's try to build it as it is for now. You can think of it as building an .xnb file that was in XNA.

Select Build → Build from the menu.

ビルドを行う

Mgcb files are a type of project file, so they are asked if they want to save it. Select Yes to save.

プロジェクト保存確認

When the build starts, a message appears on the right. You will see a successful or abnormal termination, build time, and so on. If an error occurs, a detailed message is displayed, and it corrects 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. Only the additional part of the code is described once.

Defines a texture that contains images imported into a field.

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

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

Loads and creates a texture in 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's 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.

スプライトの表示