- Home /
C# how to setup a Binary Serialization
Hi everyone, I need help setting up a binary Serialization in unity. I have no clue how to set one up but I would like to so I can save my values to a File. I don't want to use Playerprefs because I want to have total control over my save files.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour{
public int ExampleInt;
public bool ExampleBool;
public float ExampleFloat;
public string ExampleString;
public int[] ExampleArray;
public List<string> ExampleList = new List<string>();
//What comes next?
}
Answer by skalev · Dec 14, 2012 at 02:25 AM
Edit :
So, in your serialization class, add this function:
// === This is required to guarantee a fixed serialization assembly name, which Unity likes to randomize on each compile
// Do not change this
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;
// The following line of code returns the type.
typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
return typeToDeserialize;
}
return null;
}
}
The next phase is to actually serialize. In order to serialize a class it needs to be tagged with [System.Serializable].
Now, the actual serialization process is pretty simple:
public static void Save(MyClassIWantToSerialize class, string filePath)
{
try
{
Stream stream = File.Open(filePath, FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
bformatter.Serialize(stream, class);
stream.Close();
}
catch (UnauthorizedAccessException) //many more exception might happen, check documentation
{}
}
I found that a while ago before I started this question. It didn't help me with my setup.
Ok. well that is how you do it. If you can shed more light on WHY this isn't helpful, we can try and help you further.
The reason why it isn't helpful is I don't understand what is going on within the answer. Its bits and pieces of binary serialization and not a full script and since it is not a full script its hard for me to figure out how it all works.
fair enough. Answer edited.
also, i recommend reading this :
http://blogs.unity3d.com/2012/10/25/unity-serialization/
alot of good information, even if you aren't going to use it.
I'm sorry I'm still not catching on. What I need is a full script so I can understand how it works.your new post is still bits and pieces.
Answer by jogo13 · Dec 13, 2012 at 11:02 PM
It's in C# but this may help: http://pixelplacement.com/2012/03/21/simple-state-saving/
I don't want to save the state of an object. I just want to save the values of my variables with Binary serialization.
Answer by MidnightStudiosInc · Jul 01, 2015 at 03:41 PM
I would recommend using GameObject Serializer Pro over BinaryFormatter because:
It's built on top of protobuf-net, which is very efficient and unlike BinaryFormatter is forward-compatable with future versions of .Net/Mono
There is built-in support for serializing many primitive Unity types like Vector3 and Quaternion
Answer by herbou · Jun 24, 2020 at 11:34 AM
A Generic BinarySerializer tool , Easy to use. Supports : Vectors, Colors, and Quaternions too.
Your answer
Follow this Question
Related Questions
Serializing strings to XML C# 1 Answer
Player lives script help 1 Answer
C# -- Build character unit from script 1 Answer
C# How to Drag and Scale with Mouse Window 0 Answers
Best practice to store and load a specific set of objects? 3 Answers