Skip to content

Instantly share code, notes, and snippets.

@radiatoryang
Last active July 31, 2018 00:39
Show Gist options
  • Save radiatoryang/9317684 to your computer and use it in GitHub Desktop.
Save radiatoryang/9317684 to your computer and use it in GitHub Desktop.
a bare bones data loading / saving (for game levels, savegames, etc.) example using the built-in binary serialization in Unity... I'd suggest hooking it up to the file browser OnGUI thing here: http://forum.unity3d.com/attachment.php?attachmentid=87628&d=1392756207 (from http://forum.unity3d.com/threads/84601-File-Browser/page2)
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Text;
using System.IO;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FileManager : MonoBehaviour {
SceneData SceneSave() {
// insert whatever data you want to save in a class of type SceneData
}
void SceneLoad (SceneData data) {
// take whatever data is in SceneData and Instantiate objects, etc. based on it
}
void FileSave (string fileName) {
SceneData data = SceneSave();
Stream stream = File.Open(fileName, FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
AddSurrogates (bformatter);
bformatter.Binder = new VersionDeserializationBinder();
Debug.Log ("SAVING: " + fileName);
bformatter.Serialize(stream, data);
stream.Close();
}
void FileLoad (string fileName) {
SceneData data = new SceneData();
Stream stream = File.Open(fileName, FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
AddSurrogates (bformatter);
bformatter.Binder = new VersionDeserializationBinder();
Debug.Log ("LOADING: " + fileName);
data = (SceneData)bformatter.Deserialize(stream);
stream.Close();
SceneLoad (data);
}
// from http://msdn.microsoft.com/en-us/magazine/cc188950.aspx
void AddSurrogates ( IFormatter formatter) {
SurrogateSelector ss = new SurrogateSelector();
ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new Vector3Surrogate());
// NOTE: AddSurrogate can be called multiple times to register
// more types with their associated surrogate types, for when
// you make more ISerializationSurrogates to implement for your
// UnityEngine types...
formatter.SurrogateSelector = ss;
}
}
[System.Serializable]
public class SceneData : ISerializable {
// put your data here
public string name = "New Scene";
// serialization methods implemented by ISerializable
public SceneData (SerializationInfo info, StreamingContext ctxt) {
//Get the values from info and assign them to the appropriate properties
name = (string)info.GetValue ("name", typeof(string) );
}
public void GetObjectData (SerializationInfo info, StreamingContext ctxt) {
info.AddValue ("name", name);
}
}
// example of a serialization surrogate you would need to implement for any custom classes or UnityEngine types
public class Vector3Surrogate : ISerializationSurrogate {
public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) {
Vector3 vec = (Vector3)obj;
info.AddValue("x", vec.x);
info.AddValue("y", vec.y);
info.AddValue("z", vec.z);
}
/// This method is part of the interface ISerializationSurrogate
public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) {
Vector3 vec;
vec.x = (float)info.GetValue("x", typeof(float));
vec.y = (float)info.GetValue("y", typeof(float));
vec.z = (float)info.GetValue("z", typeof(float));
obj = vec;
return obj;
}
}
public sealed class VersionDeserializationBinder : SerializationBinder
{
public override Type BindToType( string assemblyName, string typeName )
{
if ( !string.IsNullOrEmpty( assemblyName ) && !string.IsNullOrEmpty( typeName ) )
{
Type typeToDeserialize = null;
assemblyName = Assembly.GetExecutingAssembly().FullName;
typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) );
return typeToDeserialize;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment