Generic BinarySerializer is a great and easy to use tool that helps you to save/load and secure your game data easily, Click on the image to watch the video tutorial :
- Add
GenericBinarySerializer
package to your project. - Create a class (or struct) to hold your data and mark it as
Serializable
:
📄 PlayerData.cs
[System.Serializable]
public class PlayerData
{
//This class contains only player data that you want to save.
//don't add any logic code here (methods,..).
public string nickName = "Default Name";
public int score = 0;
public Color color = Color.white;
public Vector2 pos = Vector2.zero;
public Quaternion rot = Quaternion.identity;
}
obj = BinarySerializer.Load<T> ("filename");
Example :
📄 Player.cs
public class Player : MonoBehaviour
{
// References
// ...
// ...
// Your data holder reference :
public PlayerData playerDataInstance = new PlayerData ();
void Start ()
{
//Load data from file.
playerDataInstance = BinarySerializer.Load<PlayerData> ("player.txt");
}
}
BinarySerializer.Save (obj, "filename");
Example :
📄 Player.cs
public class Player : MonoBehaviour
{
// References
// ...
// ...
// Your data holder reference :
public PlayerData playerDataInstance = new PlayerData ();
void Start ()
{
//Save new Data to file after change.
BinarySerializer.Save (playerDataInstance, "player.txt");
}
}
BinarySerializer.HasSaved ("filename");
Example :
if (BinarySerializer.HasSaved("player.txt")){
//do something.
}
BinarySerializer.DeleteDataFile ("filename");
BinarySerializer.DeleteAllDataFiles ( );
BinarySerializer.GetDataPath ( );
⚠Notes! :
- The Load method already has a check for file existance, that's why you need to add default values to your Data Holder class fields, because the BinarySerializer's Load method returns a new instance of the Data if it's not saved before.
- Not all data types are allowed inside Data holder class.
- all variables that's not part of the Unity engine are allowed : int, float, bool, string, char, ....
- Concerning UnityEngine types you can only use : Vector2, Vector3, Vector4, Color, and Quaternion
- You can also save : Arrays, Lists, .... of thoes allowed types.
Except the 5 types mentioned above (Vector2, Vector3, Vector4, Color, and Quaternion),, all variables of UnityEngine are not allowed :
- Transform, Gameobject, SpriteRenderer, BoxCollider, Mesh, .....