Git Product home page Git Product logo

unity_genericbinaryserializer's Introduction

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

unity_genericbinaryserializer's People

Contributors

herbou avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.