Search Unity

BinaryFormatter - Complete Binary Serialization Library for Unity

Discussion in 'Assets and Asset Store' started by hasanbayat, Sep 16, 2017.

  1. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    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

    Code (CSharp):
    1. using BayatGames.Serialization.Formatters.Binary;
    then you are ready to go.

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

    Code (CSharp):
    1. using BayatGames.Serialization.Formatters.Binary;
    2. ...
    3. byte[] buffer = BinaryFormatter.SerializeObject ("Hello World");
    Resources
    License

    MIT @ Bayat Games
     
    Last edited: Nov 28, 2017
  2. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    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?
     
  3. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    Hi.

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

    Thanks.
     
  4. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    Hi again :)
    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?
     
  5. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    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:
    Code (CSharp):
    1. if ( type.IsValueType )
    2. {
    3.     result = Activator.CreateInstance ( type );
    4. }
    5. else
    6. {
    7.     result = FormatterServices.GetUninitializedObject ( type );
    8. }
    with this code:
    Code (CSharp):
    1. if (type.IsSubclassOf(typeof(UnityEngine.ScriptableObject))) {
    2.     result = UnityEngine.ScriptableObject.CreateInstance ( type );
    3. }
    4. else if ( type.IsValueType )
    5. {
    6.     result = Activator.CreateInstance ( type );
    7. }
    8. else
    9. {
    10.     result = FormatterServices.GetUninitializedObject ( type );
    11. }
    At start of ReadObject method.
    This will fix the issue.

    Thanks.
     
    Last edited: Oct 25, 2017
    AntiSilence likes this.
  6. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    Hey thank you very much!
    I was debugging it right now and was close to this, but you were quicker! ;)
     
  7. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    by the way, you missed "UnityEngine." before ScriptableObject, since you are not using UnityEngine in the first place.
    Corrected for the record:

    Code (CSharp):
    1.     if (type.IsSubclassOf(typeof(UnityEngine.ScriptableObject))) {
    2.         result = UnityEngine.ScriptableObject.CreateInstance ( type );
    3.     }
    4.     else if ( type.IsValueType )
    5.     {
    6.         result = Activator.CreateInstance ( type );
    7.     }
    8.     else
    9.     {
    10.         result = FormatterServices.GetUninitializedObject ( type );
    11.     }
    12.  
     
  8. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    Oh, right, i missed it, thanks.

    Does it work?

    Thanks.
     
  9. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    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! :D
     
    Last edited: Oct 25, 2017
    hasanbayat likes this.
  10. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    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.
     
  11. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    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" ;)
     
    hasanbayat likes this.
  12. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    Hmm, looks awesome, you are professional.

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

    Enjoy!

    Thanks.
     
  13. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    Oh, thanks! :cool:
    Anyway, you saved me a week or more of work and LOTS of headaches with your serializer (I hate meta-code o_O )
    Thanks for that!
    I'll get back to work now... :p
     
    hasanbayat likes this.
  14. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    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:

     
  15. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    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.
     
  16. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    I'm afraid it's above my competences: .NET serialization is an expertise on it's own... :p
    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. ;)
     
  17. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
  18. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    Oh! Don't bother, you have done enough already! :)
     
    AntiSilence and hasanbayat like this.
  19. ElCastro

    ElCastro

    Joined:
    Jan 22, 2019
    Posts:
    1
    IMPORTANT!!!
    Does it work on Client/Server connected throught WEBGL?
     
  20. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    Yes, if you use the same version of BinaryFormatter on your client and server WebGL, and no modification on storing it on the database.
     
    xpxilom likes this.