- Home /
What's a fast serializer that works cross platform that can be used for saving/loading custom levels?
I'm looking for a serializer that works across multiple platforms including those that don't support JIT compilation(eg. IOS). I would also prefer if the serialized object is non-human readable(eg. no xml/json).
Binaryformatter is a good choice but relatively slow.
Protobuf is a lot faster but requires pre-compilation in order to work on platforms which don't support JIT compilation, which i can't do since it breaks some of my code.
Is there any other serializer that i can use which has a similar speed to protobuf? (I mainly care about the deserialization speed and not too much on the serialization speed)
The question is a bit vague. What do you want to serialize? Arbitrary native object trees? What's the purpose of the serializer? Why does it have to be fast? Network transfer or something different? The BinaryFormatter is not only slow but also has a lot of overhead as it serializes the fully qualified class names as string. If you work in a controlled environment it's easier and smaller to create case specific serialization
Im creating an in game level editor. The level data itself is a class which i want to write to a file which can be stored.
But it's most likely not just a single class? Does it have Vector2 / 3 values in there? That's in general a problem for most serializers as Vector3 does not have the Serializable attribute. $$anonymous$$any internal types of Unity are not really prepared for general serialization.
As i said the easiest solution would be to just roll your own format. Just use a BinaryReader / BinaryWriter and write out your data the way you like.
I'm happy to provide some example code, but without any insight what your "data" looks like it would be a shot in the dark.
ps: If it's a level editor, why does the speed of the BinaryFormatter actually matter? Not that i would recommend using it, i'm just curious.
Answer by Bunny83 · Oct 16, 2017 at 05:14 PM
You may have a look at this post. Reflection doesn't require JIT support. Only dynamic code generation is not supported. I haven't used any Protobuf implementations yet, but i'm sure there are implementations which work on AOT platforms.
edit
Here's a quick example how you can manually serialize objects using the BinaryReader / Writer:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class LevelObject
{
public string name;
public Vector3 position;
public Quaternion rotation;
public void Serialize(BinaryWriter aWriter)
{
aWriter.Write(name);
position.WriteTo(aWriter);
rotation.WriteTo(aWriter);
}
public void Deserialize(BinaryReader aReader)
{
name = aReader.ReadString();
position = aReader.ReadVector3();
rotation = aReader.ReadQuaternion();
}
}
public class Level
{
public string name;
public List<LevelObject> objects = new List<LevelObject>();
public void Serialize(BinaryWriter aWriter)
{
aWriter.Write(name);
aWriter.Write(objects.Count);
for (int i = 0; i < objects.Count; i++)
objects[i].Serialize(aWriter);
}
public void Deserialize(BinaryReader aReader)
{
name = aReader.ReadString();
int count = aReader.ReadInt32();
objects.Clear();
for(int i = 0; i < count; i++)
{
var obj = new LevelObject();
obj.Deserialize(aReader);
objects.Add(obj);
}
}
}
public static class Extensions
{
public static void WriteTo(this Vector3 aVec, BinaryWriter aWriter)
{
aWriter.Write(aVec.x);
aWriter.Write(aVec.y);
aWriter.Write(aVec.z);
}
public static Vector3 ReadVector3(this BinaryReader aReader)
{
return new Vector3(aReader.ReadSingle(), aReader.ReadSingle(), aReader.ReadSingle());
}
public static void WriteTo(this Quaternion aRot, BinaryWriter aWriter)
{
aWriter.Write(aRot.x);
aWriter.Write(aRot.y);
aWriter.Write(aRot.z);
aWriter.Write(aRot.w);
}
public static Quaternion ReadQuaternion(this BinaryReader aReader)
{
return new Quaternion(aReader.ReadSingle(), aReader.ReadSingle(), aReader.ReadSingle(), aReader.ReadSingle());
}
}
public class Loader : MonoBehaviour
{
public Level level;
public void SaveToStream(FileStream aStream)
{
using (var writer = new BinaryWriter(aStream))
level.Serialize(writer);
}
public void SaveToFile(string aFileName)
{
using (var fs = File.OpenWrite(aFileName))
SaveToStream(fs);
}
public void LoadFromStream(FileStream aStream)
{
using (var reader = new BinaryReader(aStream))
{
level = new Level();
level.Deserialize(reader);
}
}
public void LoadFromFile(string aFileName)
{
using (var fs = File.OpenRead(aFileName))
LoadFromStream(fs);
}
}
Of course this is just an example. Of course if you use polymorphism in your object tree you have to handle that yourself. However most serialization solutions do not directly support polymorphism as the actual runtime type would need to be encoded in some way so the deserializer can recreate the correct type. The BinaryFormatter does this by serializing the full class name as string which makes the result quite large.
Your answer
Follow this Question
Related Questions
Binary serialization layout on different platforms 0 Answers
Why does the prefabOverride not work on fields marked as SerializeField? 0 Answers
Which types are actually serializable? Is the documentation incorrect or am I? 3 Answers
Error when trying to Serialize a field that is in a class 0 Answers
What is the best/easiest way to save a game (or scene) at runtime? 1 Answer