Wednesday, November 27, 2013

Use TitleContainer instead of ContentManager for MonoGame for Custom Content Pipeline Processing

So, I've been playing with MonoGame recently and one of the biggest challenges I've had is trying to figure out just how in the world I want to import content.  Especially custom content.

Typically, you load content using the ContentManager.Load<ContentType>(somePathHere) -- but what if I want to manually de/serialize things?  A lot of folks say, "Write a custom content pipeline to handle it."

Problem is -- that's reinventing the wheel.  I already have de/serialization methods.  I know how things are formatted.  The thing that I want now, is to load my content through this without having to do crazy additional content pipeline steps...

After some digging, I found that that you can use the...

Microsoft.Xna.Framework.TitleContainer.OpenStream(somePathHere) method to get you a stream to your content.  There are some caveats -- you will probably still have to build your items into a content project of some sort.  For Windows builds, this is pretty easy, since it's just a matter of dropping files over.  On Android, you apparently have to mark items to be built as an asset.  I haven't looked too deeply into any other platforms than Windows and Android, but I imagine Linux and Mac behave the same as Windows, leaving iOS as the only other weird question.

That being said, I'm going to be abstracting my code into this kind of a pattern (service), so I can manage the content pipeline myself.  I like the idea of being able to manage my own memory without all the extra details.  I'll also be using the Texture2D.FromStream method in conjunction with this, so I can grab my tiles, etc.

Hope that helps some folks!