ContentLoadException in MonoGame
Asked Answered
I

5

4

I've been trying load a texture in MonoGame using Xamarin Studio. My code is set up as below :

#region Using Statements
using System;

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

#endregion

namespace TestGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //Game World
        Texture2D texture;
        Vector2 position = new Vector2(0,0);

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

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize ()
        {
            // TODO: Add your initialization logic here
            base.Initialize ();

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent ()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch (GraphicsDevice);

            //Content
            texture = Content.Load<Texture2D>("player");
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update (GameTime gameTime)
        {
            // For Mobile devices, this logic will close the Game when the Back button is pressed
            if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed) {
                Exit ();
            }
            // TODO: Add your update logic here         
            base.Update (gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw (GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear (Color.CornflowerBlue);

            //Draw

            spriteBatch.Begin ();
            spriteBatch.Draw (texture, position, Color.White);
            spriteBatch.End ();

            base.Draw (gameTime);
        }
    }
}

When I debug it it gives me the error :

Microsoft.Xna.Framework.Content.ContentLoadException: Could not load player asset as a non-content file! ---> Microsoft.Xna.Framework.Content.ContentLoadException: The directory was not found. ---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\Flame\Documents\Projects\TestGame\TestGame\bin\Debug\Content\player.xnb'. ---> System.Exception:

--- End of inner exception stack trace ---

at at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)

at at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)

at at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)

at at System.IO.File.OpenRead(String path)

at at Microsoft.Xna.Framework.TitleContainer.OpenStream(String name)

at at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)

--- End of inner exception stack trace ---

at at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)

at at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)

--- End of inner exception stack trace ---

at at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)

at at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName)

at TestGame.Game1.LoadContent() in c:\Users\Flame\Documents\Projects\TestGame\TestGame\Game1.cs:0

at at Microsoft.Xna.Framework.Game.Initialize()

at TestGame.Game1.Initialize() in c:\Users\Flame\Documents\Projects\TestGame\TestGame\Game1.cs:0

at at Microsoft.Xna.Framework.Game.DoInitialize()

at at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)

at at Microsoft.Xna.Framework.Game.Run()

at TestGame.Program.Main() in c:\Users\Flame\Documents\Projects\TestGame\TestGame\Program.cs:0

So what am I doing wrong?

Incendiarism answered 9/5, 2013 at 17:7 Comment(2)
Does player.xnb exist in the Content folder under bin/Debug?Tinge
What platform are you building for? iOS or Android? At least for iOS I have had to create a Monogame content project and use the Monogame texture content processer so that the resulting xnb files would be compatible with iOS which uses its own texture format. Also, as mentioned by Richard's answer you will need to set the 'Build actions' to 'Content' and 'Copy to output directory' to 'Copy if newer'Monday
T
8

Set the 'Build action' of the png file to 'Content' and set 'Copy to output directory' to 'Copy if newer'.

You can bring up the properties window in Xamarin Studio by ctrl clicking the file and pressing properties.

You shouldn't include the file extension.

Therefrom answered 17/7, 2013 at 20:24 Comment(2)
this is the same deal in Visual Studio when dealing with Content Project resources.Lamarckism
This also worked for me using MonoGame in Xamarin Studio when developing a Cross-Platform Desktop game.Quentinquercetin
S
2

You need to add the file to the Content directory of your solution and set it to content / copy if newer in the properties window. It will then be copied to the output directory during the build process.

Note that you can either use a precompiled XNB file (usually created with XNA game studio) or you can use raw PNG image files. If you use the latter you'll need to add the file extension in your code.

Stlouis answered 9/5, 2013 at 21:51 Comment(2)
The file has been added to the Content directory and has already been copied over. Its is a PNG image. Even if I set the code as : texture = Content.Load<Texture2D>("player.png") It does not work.Incendiarism
Interesting. If you've done everything described here you should at least have the file in your bin/Debug/Content folder after building. Double check the filename (case sensitivity) and properties. Maybe try raising this question on the MonoGame forums.Stlouis
B
1

Just use this way

using (var stream = TitleContainer.OpenStream ("Content/charactersheet.png"))
{
    characterSheetTexture = Texture2D.FromStream (this.GraphicsDevice, stream);

}

instead of

texture = Content.Load<Texture2D>("player");
Boyles answered 20/12, 2016 at 17:4 Comment(0)
R
1

Move Content folder to Assets and for each resource file set the 'Build action' to 'AndroidAsset' and set 'Copy to output directory' to 'Copy if newer'.

Ramble answered 8/8, 2017 at 18:53 Comment(0)
B
-4

To get it working just follow my instruction:

Simple 2D game with Xamarin

I know its little long but it works trust me, in case of additional questions feel free to ask :D regards Schreda

Babylonia answered 16/7, 2013 at 21:36 Comment(1)
#17008608Babylonia

© 2022 - 2024 — McMap. All rights reserved.