0
\$\begingroup\$

I'm trying to implement a custom ContentPipeline extension that writes a Surface object with a Texture2D member.

My input file is an xml with fields for all of my Surfaces and their members. In the xml, my Surface's Texture2D is represented by a path to an image file. In my ContentProcessor, I want to load the Texture2D from the image file path, then write that texture data straight into my object's xnb with the rest of a Surface's members.

Right now, I've got the ContentTypeWriter writing a different ("CompiledSurface") object with a Texture2DContent member. The Texture2DContent is created in the ContentProcessor using BuildAndLoadAsset(string path).

My importer, processor, and writer all seem to be doing their jobs. But in my ContentTypeReader, I can't figure out how to convert the Texture2DContent back to a Texture2D in order to create my runtime object.

Any help is much appreciated.

class Surface
{
    protected Rectangle bounding_box;
    protected Texture2D texture;
    ...
}

class CompiledSurface
{
    public Texture2DContent texture;
    public int x;
    public int y;
    ...
}
\$\endgroup\$
  • \$\begingroup\$ It would be helpful if you posted the code of the content writer. \$\endgroup\$ – craftworkgames Jun 2 '17 at 4:18
0
\$\begingroup\$

I've got it! An extremely simplified example:

Loaded each Texture2D in custom ContentProcessor like this...

// Create external ref to texture source file (.bmp, .png, etc)
ExternalReference<Texture2DContent> tex_ref = new ExternalReference<Texture2DContent>(path_to_image_file);

// Build & Load texture
Texture2DContent texture_content = context.BuildAndLoadAsset<Texture2DContent, Texture2DContent>(tex_ref, "TextureProcessor");

Then serialized Texture2DContent in custom ContentTypeWriter like this...

// Write as Texture2DContent
output.WriteObject(texture_content);

Conversion happens during de-serialization, in custom ContentTypeReader...

// But read back as Texture2D
Texture2D texture = input.ReadObject<Texture2D>();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.