2017-01-23 64 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

乾杯!