- Home /
How to load data from this serializable class?
I have this code (which I will show below), but I can't figure out how I'm supposed to load data from it OR alter the data. Right now there's just one variable "levelReached" but I want to add more later. If someone could explain this code to me I would be extremely grateful! And now for the code:
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Runtime.Serialization;
using System.Reflection;
// === This is the info container class ===
[Serializable ()]
public class SaveData : ISerializable {
// === Values ===
// Edit these during gameplay
public int levelReached = 1;
// === /Values ===
// The default constructor. Included for when we call it during Save() and Load()
public SaveData () {}
// This constructor is called automatically by the parent class, ISerializable
// We get to custom-implement the serialization process here
public SaveData (SerializationInfo info, StreamingContext ctxt)
{
// Get the values from info and assign them to the appropriate properties. Make sure to cast each variable.
// Do this for each var defined in the Values section above
levelReached = (int)info.GetValue("levelReached", typeof(int));
}
// Required by the ISerializable class to be properly serialized. This is called automatically
public void GetObjectData (SerializationInfo info, StreamingContext ctxt)
{
// Repeat this for each var defined in the Values section
info.AddValue("levelReached", levelReached);
}
}
// === This is the class that will be accessed from scripts ===
public class SaveLoad {
public static string currentFilePath = "SaveData.templejoestuff"; // Edit this for different save files
// Call this to write data
public static void Save () // Overloaded
{
Save (currentFilePath);
}
public static void Save (string filePath)
{
SaveData data = new SaveData ();
Stream stream = File.Open(filePath, FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
bformatter.Serialize(stream, data);
stream.Close();
}
// Call this to load from a file into "data"
public static void Load () { Load(currentFilePath); } // Overloaded
public static void Load (string filePath)
{
SaveData data = new SaveData ();
Stream stream = File.Open(filePath, FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
data = (SaveData)bformatter.Deserialize(stream);
stream.Close();
// Now use "data" to access your Values
}
}
// === 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;
}
}
Answer by Bunny83 · Aug 10, 2014 at 04:43 PM
In line 50 you create a temporary instance of your SaveData class. In line 51 you would set the things you want to save. In this example there's only the "levelReached" variable.
So in line 51 you would set the reached level like this:
data.levelReached = yourReachedLevel;
When loading in line 70 (the comment already tell you that) you can access this variables again by reading data.levelReached back:
yourReachedLevel = data.levelReached;
If you want to expand the data that is stored, you just need to:
add more variables to the SaveData class.
implementing the serializing of those variables inside "GetObjectData" like the example one.
implement the deserializing inside the serializarion constructor (line 23)
When saving you need to set your variables (again line 51)
When loading you need to read back your variables (again line 70)
is yourReachedLevel the variable used by all other classes?
Your answer
Follow this Question
Related Questions
Persisting data between scenes: can't find an answer 2 Answers
saving array to bin file 0 Answers
PlayerPrefs not saving in build 1 Answer
how to save and load component values from unity editor? 1 Answer
Save Game Sceenes user quit aplication, Can I save the scene as a whole while leaving the game? 0 Answers