- Home /
Smaller serialization file size
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?
Answer by whydoidoit · Oct 08, 2012 at 06:30 PM
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();
You could just use Unity Serializer actually - up to you.
It can serialize just about anything BinaryFormatter can:
var data = UnitySerializer.Serialize(anyOldClassInstance);
AnyOldClassInstance myCopy = UnitySerializer.Deserialize<AnyOldClassInstance>(data);
Although it only uses the compression by default when it's saving whole scenes - you'd still have compress it yourself or call the helper function CompressionHelper.Compress on the results to get a compressed string out of it (suitable for sending over the wire to servers etc).
Would doing it that way serialize things based the Serializable and NonSerialized attributes?
Answer by shaderop · Oct 08, 2012 at 06:23 PM
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.
This could work if all else fails, but if there is a better serialization method out there, I could avoid this extra step.
Answer by k76 · Mar 03, 2021 at 05:40 AM
I recommend storing the large data portion in its own ScriptableObject and then use PreferBinarySerialization: https://docs.unity3d.com/ScriptReference/PreferBinarySerialization.html
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.
Your answer
Follow this Question
Related Questions
Send model/GameObject from Server to Client 0 Answers
List Type Mismatch 0 Answers