BinaryFormatter - Complete Binary Serialization Library for Unity

Hi.

BinaryFormatter is an Fast, Lightweight Binary serialization/deserialization library for Unity projects.

Download

Features

  • Serializing Collections: Lists, Dictionaries, IEnumerable
  • Serializing KeyValuePair
  • Serializing ISerializable
  • Serializing Multi-Dimensional Arrays
  • Surrogate Serialization
  • Serializing Almost anything (Automatically serializes public fields and properties)
  • Deserializing IDeserializationCallback
  • Fast and Helpful Customer Support
  • Free & Open Source
  • Easy to Use
  • Cross Platform (Let us know if you have any problem with any platform)

Getting Started
Just add

using BayatGames.Serialization.Formatters.Binary;

then you are ready to go.

BinaryFormatter provides some static methods for fast serialization of objects to binary:

using BayatGames.Serialization.Formatters.Binary;
...
byte[] buffer = BinaryFormatter.SerializeObject ("Hello World");

Resources

License

MIT @ Bayat Games

Hey there!
Thanks for the free serializer!
Can this script serialize-deserialize an instance of a whole MonoBehaviour script with all it’s variables or will I have to manually feed the variables one by one?

Hi.

Yes, as far as the fields and properties are supported types.

Thanks.

Hi again :slight_smile:
I was implementing serialization for a ScriptableObject with your library but it gets null reference exceptions:

I just serialize a ScriptableObject instance into a byte array and then try to deserialize it.
Serialization works ok. I have even written the output to a file and verified the field names were clearly readable with the notepad. So serialization is not the problem.
The problem is when I try to deserialize the byte array to a ScriptableObject. I get this error no matter what:

Seems ScriptableObjects are not compatible with your library deserializer.
Any insights?

Yes, they aren’t compatible.

I am going to make this library compatible with all Unity types such as ScriptableObjects and MonoBehaviours.

For a quick fix, Open BinaryObjectReader script inside BayatGames/BinaryFormatter/Scripts and replace:

if ( type.IsValueType )
{
    result = Activator.CreateInstance ( type );
}
else
{
    result = FormatterServices.GetUninitializedObject ( type );
}

with this code:

if (type.IsSubclassOf(typeof(UnityEngine.ScriptableObject))) {
    result = UnityEngine.ScriptableObject.CreateInstance ( type );
}
else if ( type.IsValueType )
{
    result = Activator.CreateInstance ( type );
}
else
{
    result = FormatterServices.GetUninitializedObject ( type );
}

At start of ReadObject method.
This will fix the issue.

Thanks.

1 Like

Hey thank you very much!
I was debugging it right now and was close to this, but you were quicker! :wink:

by the way, you missed “UnityEngine.” before ScriptableObject, since you are not using UnityEngine in the first place.
Corrected for the record:

    if (type.IsSubclassOf(typeof(UnityEngine.ScriptableObject))) {
        result = UnityEngine.ScriptableObject.CreateInstance ( type );
    }
    else if ( type.IsValueType )
    {
        result = Activator.CreateInstance ( type );
    }
    else
    {
        result = FormatterServices.GetUninitializedObject ( type );
    }

Oh, right, i missed it, thanks.

Does it work?

Thanks.

Yeah! everything is fine and dandy!
I got my save system implemented now with your serializer and some AES encryption.

The slimmer the implementation the better!
Thank you again! :smile:

1 Like

You can use Save Game Free, a free and complete save game solution.

Also, i am going to add BinaryFormatter and JsonFormatter to Save Game Free in the next update.

Thanks.
Best Regards.

The thing is, the more sophisticated the save system, the worse it handles fringe cases like my project.
Sure, if you’re making some generic unity stuff, your save system is the way to go.
But if you’re making procedural stuff you will have to get rid of all that sophistication anyway and use the barebones of the system with more code management overhead in the end.
That’s why I prefer making it slim “manually” :wink:

1 Like

Hmm, looks awesome, you are professional.

A professional takes care of performance and code control, you are one of them.

Enjoy!

Thanks.

Oh, thanks! :sunglasses:
Anyway, you saved me a week or more of work and LOTS of headaches with your serializer (I hate meta-code :eyes: )
Thanks for that!
I’ll get back to work now… :stuck_out_tongue:

1 Like

Hello again!
Just a message to let you know your serializer doesn’t like structs like Vector2, Quaternion, Vector3…
This is what I get when I try to serialize a Vector2:

Yes, it is not working with Vector and some other struct types, because they are a little complex and need a custom implementation, i have planned to allow adding custom types serialization.

A quick fix is to use Surrogate Serialization for Vector types.

Thanks.

I’m afraid it’s above my competences: .NET serialization is an expertise on it’s own… :stuck_out_tongue:
Let me know when/if you update your library to allow those.
In the mean time I’ll be just using common types and I’ll recreate the structs manually on deserialization.
Not a big deal. :wink:

I have lots of work to do, you can find them at here: Trello

But i will increase the BinaryFormatter and JsonFormatter priority.

Thanks.

Oh! Don’t bother, you have done enough already! :slight_smile:

2 Likes

IMPORTANT!!!
Does it work on Client/Server connected throught WEBGL?

Yes, if you use the same version of BinaryFormatter on your client and server WebGL, and no modification on storing it on the database.

1 Like