herbou / Unity_GenericBinarySerializer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unity Generic Binary Serializer

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 :

Tutorial


☴ How to use ?

  1. Add GenericBinarySerializer package to your project.
  2. 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;
}

☴ Methods and properties :

Load data :

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");
	}
}

Save data :

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");
	}
}

Check if data is already saved :

BinarySerializer.HasSaved ("filename");

Example :

if (BinarySerializer.HasSaved("player.txt")){
	//do something.
}

Delete saved file :

BinarySerializer.DeleteDataFile ("filename");

Delete all saved files :

BinarySerializer.DeleteAllDataFiles ( );

Get the path where data is saved. :

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.

Allowed types :

  • 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.

Unallowed 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, .....




❤️ Donate

Paypal

About

License:MIT License


Languages

Language:C# 100.0%