Jason Whitehorn

MarshalByRefObject.net
posts - 50, comments - 26, trackbacks - 6

My Links

News

Archives

Post Categories

.NET

Java

Proud Member Of...

XNA

Embedding Assets Into Your XNA Game

UPDATE (12/17/2007): My blog has moved. This post is now located at: http://jason.whitehorn.ws/2007/01/22/Embedding+Assets+Into+Your+XNA+Game.aspx




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.

Print | posted on Sunday, January 21, 2007 2:43 PM | Filed Under [ XNA ]

Feedback

Gravatar

# re: Embedding Assets Into Your XNA Game

You could have done that without adding them to the dll. Each project would have just referenced them from the same file folder use the FromFile method.

However, when you load content like this, you lose all of the functionality that loading content through the content pipeline provides to you. The "FromFile" method isn't a recommend way for putting content into your game unless you absolutely have to have dynamic content at runtime (and even then, there are some other solutions)

A better way is to create an MSBuild project that builds the content for you using the content pipeline and puts it into a central location. Then all of your projects that need that content can just reference that folder and files created by the build project.

Then you still gain all the benefits of the content pipeline but still can have content referenced by multiple projects.

The XNA MSDN forums have a couple posts on how to do this if you're interested.
1/21/2007 3:12 PM | George
Comments have been closed on this topic.

Powered by: