Loading Content

Complete Sample

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

Download GameLoop_Sample.zip

Loading Content

You need a content project in your game if you are using Visual Studio 2010 Express to compile and build your project. For more information, see How to: Add Game Assets to a Content Project.

To load content and ensure it is reloaded when necessary

  1. Derive a class from Game.

  2. Override the LoadContent method of Game.

  3. In the LoadContent method, load your content using the ContentManager.

    SpriteBatch spriteBatch;
    // This is a texture we can render.
    Texture2D myTexture;
    
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        myTexture = Content.Load<Texture2D>("mytexture");
    }
    
  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
    }
    

See Also

Reference

Game Class
LoadContent
UnloadContent
Game Members
Microsoft.Xna.Framework Namespace