VS2012,C#,Monogame - 加载资产异常

use*_*693 6 c# xna monogame visual-studio-2012

我几天来一直在与这些问题作斗争,浏览网络,但没有任何帮助我解决它:我在Visual Studio 2012上创建一个MonoGame应用程序,但在尝试加载纹理时,我遇到以下问题:

无法加载Menu/btnPlay资产!

我已设置内容目录:Content.RootDirectory ="Assets"; 此外,文件btnPlay.png还具有以下属性:构建操作:内容和复制到输出目录:如果较新则复制.

我的构造函数和LoadContent函数是完全空的,但看看自己:

public WizardGame()
{
    Window.Title = "Just another Wizard game";

    _graphics = new GraphicsDeviceManager(this);

    Content.RootDirectory = "Assets";
}

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

    Texture2D texture = Content.Load<Texture2D>("Menu/btnPlay");

    _graphics.IsFullScreen = true;
    _graphics.ApplyChanges();
}
Run Code Online (Sandbox Code Playgroud)

我很乐意提供任何帮助!我对这个问题非常绝望....

Ayb*_*ybe 6

在VS2012,Windows 8 64位和最新的MonoGame(3.0.1):

  • 创建一个名为Assets的子文件夹
  • 将" 复制到输出"设置"不复制"以外的任何内容
  • 在加载时将资源预先添加到纹理路径

在此输入图像描述

namespace GameName2
{
    public class Game1 : Game
    {
        private Texture2D _texture2D;
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        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
            _texture2D = Content.Load<Texture2D>("assets/snap0009");
        }

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

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(_texture2D, Vector2.Zero, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是你绘制的纹理:D

在此输入图像描述

注意:

为方便起见,我保留了内容根目录指向的原始值:Content.

但是,您也可以直接Assets在路径中指定:

Content.RootDirectory = @"Content\Assets";
Run Code Online (Sandbox Code Playgroud)

然后在不预先添加Assets到其路径的情况下加载纹理:

_texture2D = Content.Load<Texture2D>("snap0009");
Run Code Online (Sandbox Code Playgroud)

  • 我知道你没有,但问题描述他已将此更改为Assets,它可能会在某些情况下产生混淆,这就是为什么我评论排除这些情况并使你的答案更好的原因. (2认同)