Save System Help

Hi peeps!

I need someone to point me in the right direction for developing a save system.

I’m relatively new to Unity, but am fairly comfortable with scripting in JavaScript. I’m designing a simulation game, and have reached the point that I need to start considering implementing a save system. After careful thought, I realize I need to save practically everything. First off…is this going to be a big problem…is there a practical limit to the size of the save? Secondly, is there a way to do this within JavaScript…everything I’ve seen has been in referrence to C#.
And finally, I don’t expect anybody to write my code…but could someone point me in the right direction as far as a good tutorial or referrence for this?

Thanks much!
Brian

Look into System.Runtime.Serialization, System.Runtime.Serialization.Formatters.Binary and System.IO. This is C# code, but I think Unity Javascript is really C# in disguise.

To Save:

IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, objectToSerialize);
stream.Close();

To Load:

IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Object deserializedObject = formatter.Deserialize(stream);
stream.Close();

The Unity editor causes problems with Saving and Loading because it creates a new assembly every time it runs. So you’d have to create a class like this:

sealed class DeserializationBinderEditor : SerializationBinder
{
	public override Type BindToType(string assemblyName, string typeName)
	{
		if(string.IsNullOrEmpty(assemblyName)  string.IsNullOrEmpty(typeName))
		{
			return null;
		}
		
		Type typeToDeserialize = null;
		
		assemblyName = Assembly.GetExecutingAssembly().FullName;
		typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
		
		return typeToDeserialize;
	}
}

then when you load, it’s a little different. You just add one line

IFormatter formatter = new BinaryFormatter();
formatter.Binder = new DeserializationBinderEditor();
Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Object deserializedObject = formatter.Deserialize(stream);
stream.Close();