2015-10-06 70 views
0

最近我已經開始使用MonoGame並開始學習3D遊戲編程,下面是關於如何添加3D模型的一些在線教程我得到了下面的代碼,應該顯示一個3d立方體和照相機我控制平移周圍:無法加載MonoCube資產作爲非內容文件

public class Game1 : Game 
{ 
    GraphicsDeviceManager graphics; 

    Vector3 camTarget; 
    Vector3 camPosition; 
    Matrix projectionMatrix; 
    Matrix viewMatrix; 
    Matrix worldMatrix; 

    Model model; 


    // orbit 
    bool orbit; 


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

    protected override void Initialize() 
    { 

     base.Initialize(); 


     camTarget = new Vector3(0f, 0f, 0f); 
     camPosition = new Vector3(0f, 0f, -100f); 

     projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f), GraphicsDevice.DisplayMode.AspectRatio, 1f, 1000f); 

     viewMatrix = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up); 

     worldMatrix = Matrix.CreateWorld(camTarget, Vector3.Forward, Vector3.Up); 

    } 

    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 

     model = Content.Load<Model>("MonoCube"); 
     // TODO: use this.Content to load your game content here 
    } 

    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

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

     // TODO: Add your update logic here 

     if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
     { 
      camPosition.X -= 1f; 
      // remove this to create a rotation 
      camTarget.X -= 1f; 
     } 
     if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
     { 
      camPosition.X += 1f; 
      // remove this to create a rotation 
      camTarget.X += 1f; 
     } 
     if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
     { 
      camPosition.Y -= 1f; 
      // remove this to create a rotation 
      camTarget.Y -= 1f; 
     } 
     if (Keyboard.GetState().IsKeyDown(Keys.Down)) 
     { 
      camPosition.Y += 1f; 
      // remove this to create a rotation 
      camTarget.Y += 1f; 
     } 

     if (Keyboard.GetState().IsKeyDown(Keys.OemPlus)) 
     { 
      camPosition.X += 1f; 
      // remove this to create a rotation 

     } 
     if (Keyboard.GetState().IsKeyDown(Keys.OemMinus)) 
     { 
      camPosition.X -= 1f; 
      // remove this to create a rotation 

     } 
     if (Keyboard.GetState().IsKeyDown(Keys.Space)) 
     { 
      orbit = !orbit; 

     } 
     if (orbit) 
     { 
      Matrix rotationMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(1f)); 
      camPosition = Vector3.Transform(camPosition, rotationMatrix); 
     } 

     viewMatrix = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up); 


     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 


     GraphicsDevice.Clear(Color.CornflowerBlue); 

     // TODO: Add your drawing code here 

     foreach(ModelMesh mesh in model.Meshes) 
     { 
      foreach(BasicEffect effect in mesh.Effects) 
      { 
       effect.View = viewMatrix; 
       effect.World = worldMatrix; 
       effect.Projection = projectionMatrix; 
       //mesh.Draw(); 
      } 
      mesh.Draw(); 
     } 
     base.Draw(gameTime); 
    } 
} 

的問題是,當我嘗試啓動遊戲,我得到以下錯誤:

型Microsoft.Xna的」未處理的異常.Framework.Content.ContentLoadException'發生在MonoGame.Framework.dll 附加信息:不能將MonoCube資產加載爲非內容文件!

而我不知道問題是什麼,管道有模型的.dae文件和png,並且生成良好,內容管道.mgcb文件位於Content文件夾內,因此不是問題,我知道,我會很感激任何和所有的幫助,因爲我現在在這方面虧本。

回答

1

您可以嘗試使用XNB文件以確保加載沒有問題。微軟有幾個例子,這是第一個彈出對我來說: http://xbox.create.msdn.com/en-US/education/catalog/lab/marble_maze

可能是一個愚蠢的建議,但要確保編譯內容文件位於內容文件夾爲主營項目,而不是內容項目。

您定位的是哪個平臺?有一段時間沒有使用過monogame,但以前你不能在ios上使用Spritefont編譯的窗口,反之亦然。在內容項目中查看.dae文件的選項。