I'm currently experimenting with Xna Content Pipeline extensions. Within that experimentation, I'm trying to load a file that contains another 'content item' that is in need of loading. For example:
public class CustomItem
{
public string Name;
public Texture2D Texture;
}
Now, in my content processor, I can create a new instance of 'CustomItem' and initialize the Name field, since it's simply a string. However, I can't load the texture file during content compilation (NOTE: The texture is just an example, ideally I'd like to be able to load any other content type).
What I'm looking for is something like:
// ... start class ...
public override CustomItem Process(SomeInputFormat input, ContentProcessorContext context)
{
return new CustomItem()
{
Name = input.ItemName,
Texture = context.LoadAsset<Texture2D>(input.ItemTexturePath) // I realise LoadAsset<T>() does not exist - it's an example of what would be ideal
};
}
// ... end class ...
Does anyone know if this is actually possible, and if so, how to go about it? I'd rather not go down the route of late loading the other content items if possible, or creating my own, custom content loading using binary readers and writers.
You can't use Texture2D
in the content pipeline. You have to use Texture2DContent
, which is a proxy-type for the former. In turn, you must have a mechanism in your type for allowing the member to be Texture2DContent
at content build time, but Texture2D
at run-time. This article gives you three ways of doing this.
You can use ContentProcessorContext.BuildAndLoadAsset
to get your Texture2DContent
object. This texture data will be embedded into your .xnb
file for that asset.
If you don't actually need to use the texture data in the pipeline, and in particular if you intend to share the same texture between multiple assets, you can use ContentProcessorContext.BuildAsset
to get an ExternalReference
to the texture, which is built into its own .xnb
file, external to your asset's .xnb
file (and ContentManager
will handle the loading for you).