XNA Content Pipeline Extension - Download other content to the user processor

I am currently experimenting with XNA Content Pipeline extensions. As part of this experiment, I am trying to upload a file containing another content item that needs to be downloaded. For instance:

public class CustomItem { public string Name; public Texture2D Texture; } 

Now in my content handler, I can create a new instance of "CustomItem" and initialize the "Name" field, as this is just a string. However, I cannot load the texture file while compiling the content (NOTE: Texture is just an example, ideally I would like to be able to load any other type of content).

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 an example of what would be ideal }; } // ... end class ... 

Does anyone know if this is really possible, and if so, how to do it? I would prefer not to follow the path of late loading of other content elements, if possible, or to create my own content loading using binary readers and writers.

+4
source share
1 answer

You cannot use Texture2D in the content pipeline. You should use Texture2DContent , which is the proxy type for the first. In turn, you must have a mechanism in your type to allow the member to be Texture2DContent at the time the content was created, but Texture2D at run time. This article gives you three ways to do this.

You can use ContentProcessorContext.BuildAndLoadAsset to get a Texture2DContent object. This texture data will be embedded in your .xnb file for this asset.

If you really don't need to use the texture data in the pipeline, and in particular, if you intend to use the same texture between multiple assets, you can use ContentProcessorContext.BuildAsset to get the ExternalReference texture, which is built into your own .xnb file, external for your .xnb file resource (and the ContentManager will handle the download for you).

+5
source

All Articles