Archive, C#, XNA
     

Embedding Assets Into Your XNA Game

Currently I am a working on a small XNA project whose solution is composed of 4 projects, a DLL, a Windows Game, a Xbox 360 game, and a level editor. Each projects needs access to my assets (currently just 2d sprites), and has raised the interesting question of how to easily share them between each project.

What I had been doing was adding the assets to the Windows game as pipeline content. After compiling it I would then copy the compiled assets from the bin directory, to the bin directories of the level editor and the Xbox 360 game. While this worked, it was error prone and time consuming.

Instead I found that I can add my assets to the DLL as embedded content, and then load them at runtime. Normally you would have probably loaded content by calling the "Load" method of "ContentManager", like:

            _content.Load<Texture2D>("mySprite");

As stated above, the problem with this solution is that "mySprite" would have had to been compiled into the content pipeline. When your assets are added as embedded content, the above method does not work. Instead, you can load your content like this:

            Assembly asm = Assembly.GetExecutingAssembly();
Texture2D.FromFile(_graphicsDevice, asm.GetManifestResourceStream("SampleProject.Textures.mySprite.png"));

I have tried this for both PNG and Targa formatted textures, and it works just fine. Now all of my assets are embedded into the same DLL that contains my game engine.

Never miss an article! Subscribe to my newsletter and I'll keep you updated with the latest content.

 

About Jason

Jason is an experienced entrepreneur & software developer skilled in leadership, mobile development, data synchronization, and SaaS architecture. He earned his Bachelor of Science (B.S.) in Computer Science from Arkansas State University.
View all posts by Jason →

Leave a Reply

Your email address will not be published. Required fields are marked *