【问题标题】:VS2012, C#, Monogame - load asset exceptionVS2012,C#,Monogame - 加载资产异常
【发布时间】:2023-03-13 18:31:01
【问题描述】:

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

无法加载菜单/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();
}

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

【问题讨论】:

    标签: c# visual-studio-2012 xna monogame


    【解决方案1】:

    在 VS2012、Windows 8 64 位和今天最新的 MonoGame (3.0.1) 下:

    • 创建一个名为 Assets 的子文件夹
    • 复制到输出设置为不要复制以外的任何内容
    • 加载时将 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");
    

    【讨论】:

    • 好帖子和漂亮的视觉效果。只有我缺少你解释他需要设置 Content.RootDirectory = "Assets"; 的地方。返回“内容”。视觉效果解释了您的地图结构,但也许用两句话写下关于内容地图的那一小部分也很好。
    • 其实我从来没有说过要指定根目录:) 为什么我觉得这样更好?因为这些是开箱即用的设置。
    • 我知道您没有,但问题描述他已将其更改为资产,并且在某些情况下可能会造成混淆,这就是我评论排除这些情况并使您的答案更好的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2013-05-21
    • 2020-05-04
    • 1970-01-01
    相关资源
    最近更新 更多