- Home /
Binary Save Class Array
Hey guys,
I am quite new to programming and especially saving, so I watched a couple of Tutorials and tried to rebuild a binary save / load system which seems to work fine for some variables, however an arrasy containing variables from another class does not seem to get saved. Would appriciate if anyone could tell me whihc mistake I made. Here is my Save/Load script:
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class SerializationManager
{
public static bool Save(string saveName, object saveData)
{
BinaryFormatter formatter = GetBinaryFormatter();
if (!Directory.Exists(Application.persistentDataPath + "/TapJoy"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/TapJoy");
}
string path = Application.persistentDataPath + "/TapJoy/" + saveName + ".save";
FileStream file = File.Create(path);
formatter.Serialize(file, saveData);
file.Close();
return true;
}
public static object Load(string path)
{
if (!File.Exists(path))
{
return null;
}
BinaryFormatter formatter = GetBinaryFormatter();
FileStream file = File.Open(path, FileMode.Open);
try
{
object save = formatter.Deserialize(file);
file.Close();
return save;
}
catch
{
Debug.LogErrorFormat("Failed to load file at {0}", path);
file.Close();
return null;
}
}
public static BinaryFormatter GetBinaryFormatter()
{
BinaryFormatter formatter = new BinaryFormatter();
return formatter;
}
}
And then:
using System.Collections;
using UnityEngine;
[System.Serializable]
public class SaveData
{
private static SaveData _current;
public static SaveData current
{
get
{
if (_current == null)
{
_current = new SaveData();
}
return _current;
}
set
{
if (value != null)
{
_current = value;
}
}
}
public PageValues pageValues = new PageValues();
}
And Finally my custom classes:
using System;
using UnityEngine;
[System.Serializable]
public class PageValues
{
public Tiles[] tiles;
public float timer;
public int starRating;
public int failCounter;
[Serializable]
public class Tiles
{
public bool isColored = false;
public Color color;
}
}
Somehow the Tiles[] tiles; array seems not to get saved.
Answer by Bunny83 · Oct 03, 2020 at 03:46 PM
Unity's Color struct is not marked with the Serializable attribute and therefore is not serialize by the binaryformatter. You could implement a serialization surrogate for the Color struct or use a seperate serializable struct to hold the color information
Anyways, I would not recommend using the BinaryFormatter. It has several issues, specifically:
The binary format is well known and doesn't provide much security / protection against manipulations. Security is often one of the main reasons by people seem to choose the binary formatter.
The format is quite verbose which makes it easy to actually reconstruct your data classes from a save file without seeing or decompiling your code.
Due to the tight coupling with your data classes you can easily run into versioning issues whenever you want to add, remove or change any field in your data structure. This is mainly an issue when you update your application and users already have an old save file.
Since the format is that verbose in the average case it's not really smaller than json and in some edge cases can be even larger (never try to save multidimensional arrays or generic types with it).
The binary nature of the save file makes it difficult to debug any issues you may have.
The binary remoting format that is used by he binary formatter also makes your application vulnerable to deserialization attacks because one can embed any class inside the binary stream which is happily deserialized by the binary formatter. Microsoft even has a security warning against the use of the BinaryFormatter
That's why in many cases it's better to use json or another human readable format. To make manipulations more difficult you can always add some obfuscation at the end. The BinaryFormatter was never meant for saving / loading information to / from files. It's just part of the remoting protocol for remote procedure calls.
Though the binary formatter has one feature that most serializers usually do not support which is polymorphism. However you rarely would need something like that. Anyways it's up to you what you're using in the end.