C# MonoGame Help? (Content.Load(“Invade

2019-07-01 12:00发布

问题:

I am Making a Space Invaders Game using Open GL in MonoGame and I am trying to load a texture that I have added to the Content folder (It is a PNG file called "Invader")
The code that I use is:

invader = Content.Load<Texture2D>("Invader");

However when I attempt to run it It says:

ContentLoadException was unhandled
could not load Invader as a non-content file!

回答1:

I am trying to load a texture that I have added to the Content folder (It is a PNG file called "Invader") invader = Content.Load("Invader");

Actually, you can load the PNG content that has been added to the Content folder directly like so:

invader = Content.Load<Texture2D>("Invader");

Note that the filename is case senitive on some platforms so be careful that it matches exactly. Also, make sure you've set the file to Content / Copy if newer in the Properties window.

The alternative is to compile your assets into optimized binary XNB files using the XNA Game Studio Content Pipeline or the MonoGame Content Pipeline. This will give you better performance but carries extra development overhead.

I should also mention that when rendering your sprites as raw PNG files you should use BlendState.NonPremultiplied in the call to SpriteBatch.Begin for best results. I've been doing it this way in my games for a while and I'm pretty happy with the results.



回答2:

MonoGame does not fully implement the content manager. Typically, you build the content separately, and import the built content files into your project. Then you can load them as usual.

To build the content files, you can use an XNA or MonoGame content builder such as this one. If you prefer, you can use command lines as part of your project's build process so that content is built automatically.



回答3:

Make sure your Build Action is set to Content for the .png in question. Do this by right clicking the file and selecting properties.