XNA, Proxyclasses to 'Serialize' assets using the IntermediateSerializer
As some of you’ve might seen, I’ve been breaking my head over the following problem for the past few days:
How do I serialize the class like the following without creating a special ‘intermediate’ struct for each and ever classes
Even though I’ve marked the Texture2D as a shared resource, I still can’t serialize it using the IntermediateSerializer because a Texture2D always has a reference to a GraphicsDevice object, and the IS can’t serialize that.
Fortunately some helpfull people in #XNA on Efnet (ecosky and flashed, that’s you guys!) told me about proxyclasses, first I was confused and thought they wanted me to write a proxy for every class in my project, but soon they helped me understand that I should write a proxy class for Texture2D. I just had to write a proxy class that can be serialized and that will store the path to texture, so that it can reload itself when deserialized.
I quickly came up with the following class, very simple but it will suffice for now:
Now we just need to replace Texture2D with Texture2DProxy everywhere, and update some code. In the end we can now serialize and deserialize the MyData class like this:
Btw, serializing like this will generate the following XML file:
With this problem out of the way, I can now finally start working on a way to save and restore levels in Hollandia. Oh btw, you can ofcourse do the same trick with SoundEffects and Models or anything else for that matter.