VS2012, C #, Monogame - load resource exception

I have been struggling with these problems for several days, browsing the network, but nothing helped me solve this: I create a MonoGame application on Visual Studio 2012, but when I try to load the texture, I get the following problem:

Failed to load Menu / btnPlay resource!

I set the content directory: Content.RootDirectory = "Assets"; In addition, the btnPlay.png file has the following properties: Create action: contents and copy to output directory: copy if new.

My LoadContent constructors and functions are completely empty, but see for yourself:

public WizardGame() { Window.Title = "Just another Wizard game"; _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Assets"; } protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. _spriteBatch = new SpriteBatch(GraphicsDevice); Texture2D texture = Content.Load<Texture2D>("Menu/btnPlay"); _graphics.IsFullScreen = true; _graphics.ApplyChanges(); } 

I would be happy for any help! I am totally desperate for this problem ....

+6
source share
1 answer

In VS2012, the 64-bit version of Windows 8 and the latest version of MonoGame today (3.0.1):

  • create a subfolder named Assets
  • set Copy to Output to anything else than Don't Copy
  • add resources to your texture path at boot time.

enter image description here

 namespace GameName2 { public class Game1 : Game { private Texture2D _texture2D; private GraphicsDeviceManager graphics; private SpriteBatch spriteBatch; 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 _texture2D = Content.Load<Texture2D>("assets/snap0009"); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.Draw(_texture2D, Vector2.Zero, Color.White); spriteBatch.End(); base.Draw(gameTime); } } } 

Here is your texture painted: D

enter image description here

Note:

For convenience, I saved the original value, which indicates the root directory of the content: Content .

However, you can also directly specify Assets in the path:

 Content.RootDirectory = @"Content\Assets"; 

Then load your texture without adding Assets to its path:

 _texture2D = Content.Load<Texture2D>("snap0009"); 
+6
source

Source: https://habr.com/ru/post/957784/


All Articles