2017-01-23 39 views
2

我刚刚开始使用MonoGame,每当我运行我的程序时,我都会收到无法将资产加载为非内容文件错误的情况。我一直在寻找网络,并没有发现任何关于我的问题。请帮忙!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); 
    } 
} 
} 
+0

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

回答

1

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

enter image description here

在我的情况下,由于CarBlue是在内容的Graphics文件夹我这一行加载:

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

你当然可以像@SBurris说,但我会建议一个更简单,更全面的问题解决方案:

原因:

所以,当你尝试调试在Visual Studio项目,你会得到一个ContentLoadException,它告诉你的资产无法加载作为非内容文件。你问什么是content file? A content file是一个包含已编译资产的二进制文件,其扩展名为.xnb。当指定这样的路径,您的资产(如纹理):

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

你实际上并没有链接到.png文件的质感,而您链接到持有你需要的数据.xnb内容文件。例外只是告诉你在指定的目录中找不到内容文件(.xnb)。

解决办法:

因此,要解决这个问题,只要打开MonoGame Content Builder/Pipeline tool,它驻留在你的项目的目录名为Content文件夹中。管道工具本身也被称为Content(或Content.mgcb),并具有MonoGame的橙色标志,因为它是图标。

完成之后,通过右键单击将您需要的资产添加到Content Project(默认情况下称为Content)。

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

这会为你编译asset filecontent file,你的游戏将准备好读取它。

XNA:

当我第一次开始与MonoGame,我有经验,与微软的XNA,当我发现MonoGame利用不同的方法来增加资产的一个项目,我有同样的问题,像你。那是因为我习惯了XNA的方式,这让我只需要简单地Add -> Existing item而忘了它 - Visual Studio会自动为我编译资产文件。那么,MonoGame就不再这样了,所以记得从Content Pipeline tool开始建立Content Project

干杯!