Loading Content from a Game Library

XNA Game Studio 4.0

Demonstrates how to load content from a game library.

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.

    Ensure 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.

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