xna 4.0 and loading images fails

2020-04-21 01:43发布

问题:

I'm trying to load png images into my game, but for some reason it can't find the images.

This is my Title screen that is trying to load the images:

public TitleScreen(ContentManager contentManager)
  {
     titleScreen = contentManager.Load<Texture2D>("gfx\\titleScreen");
     bgScreen = contentManager.Load<Texture2D>("gfx\\bgScreen");
     arialFont = contentManager.Load<SpriteFont>("Arial");
  }

Here I'm setting the root directory for the content:

Content.RootDirectory = "Content";

When my program hits titlescreen, it fails to find the image, however the path to it is correct and the images are also set to be Content and copied to output directory.

When I try to debug it, it tells me it can't find image "Content\gfx\titleScreen.xnb". For some reason it is trying to load an xnb file, is there anywhere that I can change it, or do I need to do anything else?

回答1:

In XNA Images are compiled .xnb files and not .png or .jpg

XNA 3.0 || XNA 3.1
You add your Content to the Content folder within your project

XNA 4.0
You have a Content Project and you add a Content Reference in your current project

Also Make sure you set the properties of the images: Right Click on Content Item -> Properties -> Set the following

  • Build Action: Compile
  • Content Importer/Processor: Texture - XNA Framework (Textures)
  • Content Importer/Processor: Sprite Font Description - XNA Framework (Fonts)
  • Do not copy


回答2:

Additionally remember to rebuild solution after adding new resource to content project.



回答3:

In XNA you typically load processed files (xnb files). You'd add a content project to your solution, and reference that in your main game project (usually this is set up when you create a new game).

You add your files (assets) in there. When your game is built the content project is built, which processes your assets, producing xnb files, which is what the ContentManager.Load method wants.



标签: c# xna-4.0