OstGeneralen
OstGeneralen

Reputation: 3

MonoGame Windows OpenGL project "Could not load asset as non-content file" Asset is set to Copy Always

Recently I have been having some troubles loading assets as Texture2D. I have tried changing the path to "..\assetName", "assetName", "assetName.png" and "..\assetName.png" (as suggested by one of the answers to this question but neither of them does the job. I have also tried using the MonoGame content pipeline when adding the file and the "Copy to Output Directory" parameter is currently set to "Copy Always".

I tried creating a new project with some really simple code for loading the content file and it reads as follows

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

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

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

    /// <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);

        // TODO: use this.Content to load your game content here
        texture = Content.Load<Texture2D>("Character1");
    }

But I get the same error every time.

The exception message reads as the title says "Could not load Character1 asset as a non-content file" and the inner exception says "The content file was not found."

I have also checked the folder where the file is supposed to be copied to and it contains both the .png and the .xnb

Upvotes: 0

Views: 255

Answers (1)

SamTh3D3v
SamTh3D3v

Reputation: 9944

MonoGame team has introduced a new and better way to manage Content in your project by using the MonoGame Pipeline application,

to correctly add a sprite, first open the Content.mgcb file using the MonoGame Pipeline tool (you should find it where you've installed monogame)

enter image description here

then from the edit menu, Add > Existing Item, and select your image,

enter image description here

Build the content and you should be good to go

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

Read more about managing content from here.

EDIT To load the Sprite, use the exact same way

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

        // TODO: use this.Content to load your game content here
        texture = Content.Load<Texture2D>("Character1");
    }

Upvotes: 2

Related Questions