- Home /
Saving game data: BinaryFormatter vs SoapFormatter
I'm making a save system for my game. Because I have plenty of behaviors in the scene that need to save their runtime data, I call a function on each of them, so they can add the variables they need to the stream:
void SaveState(IFormatter f, Stream saveStream)
{
f.Serialize(saveStream, myTotalHealth);
f.Serialize(saveStream, myTotalDPM);
}
The stream is a FileStream, which saves to a file; I use a BinaryFormatter as the formatter parameter to the function. Which means the data in the file is not human readable.
Now in order to debug a serialization problem, I would like to use a formatter that is text based, for example XML, so I can read the file and see what's wrong. You would think that since BinaryFormatter is implementing an IFormatter interface, all you've got to do is use an XMLFormatter implementing the same interface. And indeed, it seems that .Net has a SoapFormatter that does that. And only the one :/
But for some reason, this SoapFormatter is not accessible in Unity - I think it's not part of the supported .Net subset, and I haven't been able to figure out how to add it to the project.
There is also an XmlSerializer, but since it needs a specific data type in the constructor, I think I would have to create a new one for each variable that needs to be serialized - which would be extremely inefficient. And worst of all, since the XmlSerializer isn't an IFormatter, I would have to change lots of code whenever I want to switch between binary and text.
So my questions are:
Is there a way to make the .Net SoapFormatter work in Unity code?
Is there another solution that could let me switch easily between binary and text formatting?