I am serializing some data, which I already have complete. Using the BinaryFormatter to serialize, I get a rather large file size for saving very little. Around 11kb for about 4 objects name (string) and transform information (9 floats x 4). You would think that may be around 1152bytes for the floats, and around 960bytes for the 16bit string of 15 characters each. So you would expect maybe around 3k of data at max, but I am getting 11k. I know 11kb isn’t huge, but it is still bigger than I expected. Is there a different serialization method that will work on all the platforms (Web, iOS, android) that will result in a smaller file size?
You could try piping your serialized data through a GZipStream. It is implemented in Mono, so it should work on all platforms supported by Unity.
In Unity Serializer I use a modified version of SharpZip which is the fastest thing that I’ve been able to find. I also have a version of 7zip which is more compressed but very slow. BinaryFormatter is a bit bloated as it does store all of the type information mulitiple times.
Feel free just to grab the SharpZIP folder out of Unity Serializer.
Compression looks like this:
var m = new MemoryStream();
var br = new BinaryWriter(m);
var z = new DeflaterOutputStream(m);
br.Write(data.Length);
z.Write(data, 0, data.Length);
z.Flush();
z.Close();
var data = m.GetBuffer();
Inflation looks like this:
byte[] output = null;
var m = new MemoryStream(data);
var z = new InflaterInputStream(m);
var br = new BinaryReader(m);
var length = br.ReadInt32();
output = new byte[length];
z.Read(output, 0, length);
z.Close();
m.Close();
I recommend storing the large data portion in its own ScriptableObject and then use PreferBinarySerialization: Unity - Scripting API: PreferBinarySerialization
Also, If you use a specific naming scheme for this type of ScriptableObject, you can track it with Git LFS, if you happen to use Git LFS.