Sunday, June 13, 2010

XNA – Creating a Custom Content Pipeline

 

There are nine steps to create a custom content pipeline in Microsoft’s XNA Studio

  1. Add Project.
  2. Add Reference to Microsoft.XNA.Framework.Content.Pipeline.
  3. Add Namespaces.
  4. Specify Functionality to Override.
  5. Compile.
  6. Reference your Custom Processor from Your Game.
  7. Set Dependencies.
  8. Choose Custom Processor.

Add Project

AddProjectThe content pipeline will always execute when you compile the game.  Regardless of the targeted platform the importer and processor will run in a Microsoft Windows environment.   This means we need to create a separate project.

 

selectContentPipeline

    1. Right Click on the Solution
    2. Click on Add
    3. Choose New Project
    4. Select Content Pipeline Extension Library
    5. Give it a name.

Add Reference

  1. Expand the new project
  2. Right Click on References.
  3. Select Microsoft.Xna.Framework.Content.Pipeline

AddReference

image

 

 

Add Namespaces

You will want to make sure you have access to the functions you will need.  Add the following using statements to the new pipeline.

using System. IO;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework. Content.Pipeline.Processors;

 

Specify Functionality to Override

In this example I will be extending the TextureProcessor.  Override the Process function.
namespace CustomContentPipeline
{

     [ContentProcessor(DisplayName = "CustomContentPipeline") ]
     public class ExtentedTextureProcessor : TextureProcessor
     {
         public override TextureContent
         Process(TextureContent input, ContentProcessorContext context)
         {
             return base.Process(input, context);
         }
    }

}


This is where you would write your custom code.


Compile


Now we compile to create the assembly.  You will want to use the build option or press F6.  Pressing F5 will try to run the solution.


Reference your Custom Processor from Your Game


Just like we added the content pipeline reference to our new pipeline we need to add a reference to our pipeline in our game.  Follow the same directions in step 2.  You will need to select the projects tab to see your new pipeline.


AddReferenceProject


Set Dependencies


Because the game project is dependant on the pipeline this is a good time to setup a dependency to the custom processor.



  1. Right Click to Solution

  2. Select Project Dependencies

  3. Select the Game from the drop down menu.

  4. Make sure the content pipeline is selected.

Choose Custom Processor


Once you have added the texture to your Content folder you will want to choose your new processor.  Right click the texture and select property.  Once the properties window is open select the content processor drop down and select your new processor.


ChooseProcessor