Serializing the data is actually a pretty clever workaround but XML is far to heavy for the job. What I would do is use a custom made, but simple, binary format so that you can control the size of the output.
Here are a couple of methods I whipped up that should work, but please note that I made them specifically to answer this question and they have not been tested.
To save the data..
private void SaveTextureData(Texture2D texture, string filename)
{
int width = texture.Width;
int height = texture.Height;
Color[] data = new Color[width * height];
texture.GetData<Color>(data, 0, data.Length);
using (BinaryWriter writer = new BinaryWriter(File.Open(filename, FileMode.Create)))
{
writer.Write(width);
writer.Write(height);
writer.Write(data.Length);
for (int i = 0; i < data.Length; i++)
{
writer.Write(data[i].R);
writer.Write(data[i].G);
writer.Write(data[i].B);
writer.Write(data[i].A);
}
}
}
And to load the data..
private Texture2D LoadTextureData(string filename)
{
using (BinaryReader reader = new BinaryReader(File.Open(filename, FileMode.Open)))
{
var width = reader.ReadInt32();
var height = reader.ReadInt32();
var length = reader.ReadInt32();
var data = new Color[length];
for (int i = 0; i < data.Length; i++)
{
var r = reader.ReadByte();
var g = reader.ReadByte();
var b = reader.ReadByte();
var a = reader.ReadByte();
data[i] = new Color(r, g, b, a);
}
var texture = new Texture2D(GraphicsDevice, width, height);
texture.SetData<Color>(data, 0, data.Length);
return texture;
}
}
Binary data is far smaller than XML data. It's about as small as you're going to get without compression. Just be aware that binary formats are pretty rigid when it comes to change. If you need to change the format it'll be easier in most cases to write out new files.
If you need to make changes to the methods, be careful to keep them in sync. Every Write should be matched with an identical Read in the exact same order and data type.
I'm interested to know how much smaller the files become. Let me know how it goes?
File
class does not seem to exist in Monogame, unfortunately. I should find another way to save on file. – Mistaken