Managing Content in XNA

Managing Content in XNA


One of the most powerful features of XNA is how well it handles content. Game content, also called art assets, or just assets for short, is any sort of external data that is used in your game, aside from your code. Content includes 3D models, textures for the models, shaders, 2D images, fonts, and even audio. XNA makes it easy to manage your game's content. In this tutorial we will take a look at the basics of working with your game content, so that we can use it in future tutorials.

The Content Manager & The Content Pipeline

In this tutorial, we are not going to go into great detail about how XNA handles your content, other than to say that it does it in an easy, powerful, and extensible way. There are other tutorials that discuss how the Content Pipeline works as well as how to extend it that you can look at later. For now, we will just state that all of your content goes through a component that is called the Content Pipeline when you create it. The Content Pipeline already knows exactly what to do with a very large set of file types, as shown below:

Content Type File Types
3D Models .x, .fbx
Textures/Images .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, .tga
Audio .xap (an XACT audio project), .wma, .mp3, .wav
Fonts .spritefont
Effects .fx

Of course, later, we will get an opportunity to talk about each of these in more detail, so for now, you don't need to worry about it too much. Additionally, you can create an extension to the Content Pipeline to include even more file types too.

In our game, we have an instance of a ContentManager class that we can use to easily load an object from the content pipeline, and as we'll see in a minute, it is pretty easy to do.

Adding Content to your Game

The first thing we need to do is to actually tell our game what content we want to include. To do this, go to the Solution Explorer window on the right side of the screen. At the very top is your game's solution, labeled "Solution 'CoolGame'" or whatever you called your game. Inside of that, you should see two projects, one called "CoolGame" or whatever you called your game, and one called "CoolGameContent (Content)". The first project is where all of our code will go, and the second one is where our content will be placed.


This node actually represents a folder in the project directory called CoolGameContent, which is where your content files will be stored. To add content to your game, right-click on the content project node and choose Add > Existing Item from the popup menu. This will open up a file dialog that you can use to browse your computer for the content you want in your game.

Go ahead and try it out now. Find an image file (or something else) on your computer and add it to your game. The object will now appear under your content project node, as shown in the image below.


Using Folders to Organize Content

It is always a good idea to keep your content organized. When you have a small game, this isn't as important, but as you begin to develop bigger games, this is really useful. You can organize your content by adding directories to the content project node of your game by right clicking on the content project node, and then choosing Add > New Folder.

The thing to keep in mind is that if the file you want appears somewhere under the content project node of your game, it will be available during your game. If it isn't there, you won't be able to access it, and your game will throw an exception and crash when you try to load it.

Loading Content in your Game

OK, now we are on to the real interesting part. We are now going to use the content we just added. In other tutorials, as we talk about using each of the different kinds of content in turn, we will discuss this in greater detail. For now, though, we will just do something simple. Loading content in your game is always done in about the same way. Go to your LoadContent() method in your game, and add the following line (replacing "FullLogo" with the name of your file in quotes):

Texture2D image = Content.Load<Texture2D>("FullLogo");

A Texture2D object is simply a way of storing a picture. A texture is the computer graphics term for an image that you place on a 3D object, which is one of the most common ways of using an image. There are other ways to use a Texture2D object, as well. We will talk a little more about this in other tutorials. All we really need to do to load our content is call the function Content.Load(), with the name of the asset that we want to load. In the angle brackets ("<" and ">") we put the type of content that we are loading, which in this case, is simply a Texture2D as well.

You do not need to put the extension on the file! In fact, you can't. It won't work if you do. This goes back to earlier in the tutorial when we talked about how the Content Pipeline processes the file.

Also, if you have put your assets in different directories, you will need to put the path to the asset as well. For example, if our image was in a directory called Backgrounds, we would need to say:

Texture2D image = Content.Load<Texture2D>("Backgrounds/FullLogo");

Unloading Content in your Game

For a small game, you may not ever need to unload any content in your game. However, if you have a big game, you can't just keep loading more and more content and expect it to keep working. It is a good idea to unload unnecessary content whenever you are done using it. So, for instance, you can unload all of the content for a particular level, after the level is completed, so that there is room for the next level's content to be loaded.

To do this, simply add the following line to your program:


This frees up (almost) all of the content that was loaded by your content manager. Of course, this means that if you want to reuse something that was previously loaded, you will need to load it again.

It might be a good idea to create multiple ContentManager objects to divide up the content. That way you can call Unload() for the ContentManagers that you are done with.

Even if you don't have a big game, it is still doesn't hurt to unload unused content.

What's Next?

We've discussed how you can load content or art assets in your XNA game. This will make it easy to start working on your game. There are a number of good things to do next, and you can pick whatever you want. You might want to check out the tutorial about how the Content Pipeline works, if you are interested in learning more about it. I think I would recommend going and looking at the tutorials on 2D graphics next, if you haven't already done it. But, maybe you are just ready to start working on a 3D game. If so, you should take a look at the tutorials on 3D graphics that are on this website.

Troubleshooting.png Having problems with this tutorial? Try the troubleshooting page!