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 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?
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.
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 );
}
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”
Oh, thanks!
Anyway, you saved me a week or more of work and LOTS of headaches with your serializer (I hate meta-code )
Thanks for that!
I’ll get back to work now…
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.
I’m afraid it’s above my competences: .NET serialization is an expertise on it’s own…
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.