【问题标题】:Monogame: Could not load asset as a non-content fileMonogame:无法将资产加载为非内容文件
【发布时间】:2017-01-25 21:43:12
【问题描述】:

我刚刚开始使用 MonoGame,每当我运行我的程序时,我都会收到无法加载资产作为非内容文件错误。我一直在网上搜索,但没有找到任何关于我的问题的信息。请帮忙!

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MoveDemo
{

public class MainGame : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D mainmenu;

    public MainGame()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        mainmenu = Content.Load<Texture2D>("MainMenu"); 
    }

    protected override void UnloadContent()
    {
    }


    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();


        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin();
        spriteBatch.Draw(mainmenu, new Rectangle(0, 0, 840, 480), Color.White );
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
}

【问题讨论】:

  • 我假设您已经通过内容管道工具添加了文件并且它已构建?

标签: c# monogame


【解决方案1】:

你当然可以像@SBurris所说的那样做,但我会建议一个更简单、整体更好的解决方案:

原因:

因此,当您尝试在 Visual Studio 中调试项目时,您会收到 ContentLoadException,它告诉您无法将资产作为非内容文件加载。你问什么是content file? content file 是包含已编译资产的二进制文件,并具有 .xnb 扩展名。像这样指定资源(例如纹理)的路径时:

protected override void LoadContent()
{
    this.Content.Load<Texture2D>("path/to/myTexture");
}

您实际上并没有链接到纹理的.png 文件,而是链接到保存所需数据的.xnb 内容文件。该异常只是告诉您在指定目录中未找到任何内容文件 (.xnb)。

解决办法:

因此,要解决此问题,只需打开位于项目目录中名为 Content 的文件夹中的 MonoGame Content Builder/Pipeline tool。流水线工具本身也称为Content(或 Content.mgcb),其图标为 MonoGame 的橙色徽标。

完成此操作后,通过右键单击 -&gt; Add -&gt; Existing Item 将您需要的资产添加到您的 Content Project(默认情况下称为 Content)。

然后你保存并完成!错误的。为了使内容文件编译并准备好使用,您需要先create 它。这可以通过单击Build menu 并选择Build 或按键盘上的F6 来完成。

这会将asset file 编译成content file 供您使用,您的游戏就可以读取它了。

XNA:

当我第一次开始使用 MonoGame 时,我曾使用过 Microsoft 的 XNA,当我发现 MonoGame 使用不同的方法向项目添加资产时,我遇到了和你一样的问题。那是因为我习惯了 XNA 方式,它让我只需 Add -&gt; Existing item 就可以忘记它——Visual Studio 会自动为我编译资产文件。好吧,MonoGame 不再是这种情况,所以请记住从Content Pipeline tool 构建您的Content Project!

干杯!

【讨论】:

    【解决方案2】:

    您是否告诉 Visual Studio MainMenu 是内容并复制它?

    就我而言,由于 CarBlue 位于 content 下的 Graphics 文件夹中,因此我使用以下行加载它:

    blueCarSprite = Content.Load<Texture2D>(@"Graphics\CarBlue");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多