Software7

Personal Developer Notebook

Custom Content Importer in MonoGame

There is a feature of XNA called the Content Pipeline, a pre-compiler step for preparing assets to use at runtime. XNA offers the possibility to use Custom Content Importer.

This is a two step process. At compile time the following happens:

  • there is a file with custom content

  • this file will be imported at compile time into an instance of a your custom class

    (you write a custom class, a content importer, and content processor for this)

  • this object will be written binary encoded into a .xnb file, still at compile time

    (you write a content writer)

Finally at runtime

  • at runtime this .xnb is read by a content reader class

    (you can guess it, you create the content reader yourself)

  • then have your object of the custom class you defined

With MonoGame e.g. on WinRT it works almost the same. Instead of a XNA ‘Windows Game Library (4.0)’  you can use e.g. a ‘Class Library (Windows Store apps)’ in the WinRT project. Be sure to use the same name for the library and the class. The source files can be copied over from your XNA Game Library. Please note: the Assembly Name must match, otherwise it won’t work.

Assembly Name

The Assembly Name must match

In this example we will create a hand coded ContentTypeWriter and ContentTypeReader, but there is an automatic serialization feature for .xnb files using reflection. So why use hand written code?

Simply performance. You still don’t have desktop performance on current Phone devices. If you try to read e.g. 80K vertices on the Phone hand written Reader and Writer are a magnitude faster 😉

This example should only demonstrate the topic by a simple example. The custom data class looks like:

public class Skills
    {
        private string _name;
        private int _defend;
        private int _attack;

        public string name { get; set; }
        public int defend { get; set; }
        public int attack { get; set; }

    }

The custom content file has the following structure:

<string> <int> <int>

The following example is used during the screencast:

spell 15 5

I have created a 6 minute screencast to show alls steps from creating

  • XNA project from scratch
  • creating the WinRT project from scratch

If you only want to watch the WinRT part and skip the XNA section just jump ahead to 3 minutes and 44 seconds.