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

6

我已经花了好几天时间来解决这个问题,在网上浏览了许多资料,但没有任何帮助我解决它的方法:我正在使用 Visual Studio 2012 创建一个 MonoGame 应用程序,但是在尝试加载纹理时,遇到了以下问题:

无法加载 Menu/btnPlay 资源!

我设置了内容目录:Content.RootDirectory = "Assets"; 并且将 btnPlay.png 文件的属性设置为:生成操作(Build Action):Content 和 复制到输出目录(Copy to Output directory):如果较新则复制。

我的构造函数和 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();
}

我很高兴能提供帮助!关于这个问题,我感到非常绝望......

1个回答

6
在VS2012、Windows 8 64位以及最新的MonoGame(截至今天3.0.1版本)下:
  • 创建一个名为Assets的子文件夹
  • Copy to Output设置为除不复制之外的任何其他选项
  • 在加载纹理时在其路径前加上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);
        }
    }
}

这是您绘制的纹理:D
注意:
出于方便起见,我保留了内容根目录指向的原始值:Content
然而,您也可以在路径中直接指定Assets
Content.RootDirectory = @"Content\Assets";

然后加载纹理时,不需要在路径前添加 Assets

_texture2D = Content.Load<Texture2D>("snap0009");

1
好的帖子和漂亮的视觉效果。只是我缺少一个部分,你需要解释他需要将Content.RootDirectory =“Assets”设置回“Content”。视觉效果解释了你的地图结构,但也许在两个句子中写下关于Content映射的小部分也很好。 - MrME
实际上,我从来没有说过要指定根目录 :) 为什么我认为这样更好?因为这些是开箱即用的设置。 - aybe
2
我知道你还没有,但问题描述中他已将其更改为“资产”,在某些情况下可能会引起混淆,这就是为什么我评论以排除这些情况并使您的答案更好的原因。 - MrME

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接