How To: Load Content

Demonstrates how you can load content and ensure that the content will be reloaded at the appropriate times.

The methods used to load and unload resources have changed in XNA Game Studio 3.0. LoadGraphicsContent and UnloadGraphicsContent have become simply LoadContent and UnloadContent, and the Boolean parameters are no longer necessary. For backward compatibility, this version still includes LoadGraphicsContent and UnloadGraphicsContent.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Loading Content

To load content and ensure it will be reloaded when necessary

  1. Derive a class from Game.

  2. Override the LoadContent method of Game.

  3. In the LoadContent method, load your content, including resources loaded by the ContentManager.

    protected override void LoadContent()
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        // TODO: Load your game content here            
        Box = Content.Load<Model>("box");
  4. Override the UnloadContent method of Game.

  5. In the UnloadContent method, unload resources that are not managed by the ContentManager.

    protected override void UnloadContent()
        // TODO: Unload any non ContentManager content here

Loading Content from a Game Library

XNA Game Studio allows code and content to be run from Game Library projects that are added as references to Game projects. If you use a Game Library, you can embed binary resources directly in the Game Library and load them from within. This allows you to distribute code that displays textures, models, or fonts (such as a DrawableGameComponent) in a .DLL without distributing the .xnb files separately. Note that embedded resources are loaded into memory with the .DLL, and cannot be unloaded from main memory.

To add content to a Game Library

  1. Build an existing project containing the content you wish to add.
  2. In a library project, choose Add, New Item and select "Resources File."
  3. If the Resource Designer is not opened automatically, double-click the .resx file in the Solution Explorer.
  4. From the Resource Designer, choose Add Resource, Add Existing File.
  5. Navigate to the "bin\x86\Debug\Content" directory of the project that built the content you wish to add.

    This assumes it was built as an x86 Debug project.

  6. Select the .xnb file for the content you wish to add to the library.

    Make sure the dialog box is displaying "All Files."

Once content has been added to the Resource Designer, any code running from within the Library can load the content with a special ContentManager.

To load content from a Game Library

  1. Define a new ContentManager.

  2. Create a new instance of the ResourceContentManager class and assign it to your ContentManager.

    The second parameter to the ResourceContentManager constructor identifies the resource project that contains your embedded resources.

    ContentManager content;
    public GameComponent1(Game game)
        : base(game)
        content = new ResourceContentManager(game.Services, 
  3. In the LoadContent method, load your content normally using your ContentManager.
    Model box;
    protected override void LoadContent()
        box = content.Load<Model>("box2");


Community Additions