Pigeon shit

[ Pigeon shit ] Unity data persistence – serialization

After looking at the found options I decided to go with serialization and if feasible google play services. This because serialization basically supports everything and is easy to implement. You just have to make sure all the data that you want to save has the serialize tag like so:

[System.Serializable]
public class GameData { 
//all data goes here
//
}

And then you can save the data with a function like this:

using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public void Save(GameData data){
   BinaryFormatter bf = new BinaryFormatter();
   FileStream file = File.Create(Application.persistentDataPath + “/gamedata.gd”);
   bf.Serialize(file, data);
   file.Close();
}

Pigeon shit

[ Pigeon shit ] Unity data persistence

Hello there, been a while..
I have been working on the data persistence of the game, the choice was made to do local storage and if feasible online storage (in the cloud~). After a short research I had found the following possiblities:

Playerprefs
The most easy and standard used solution. Playerpres supports:

get/set floats
get/set ints (booleans are possible if you convert them to integers ea 0-1).
get/set strings

– Writing / reading  XML / JSON formats
I don’t think I’ll pick this one, the format is very human readable and players could mess with it to cheat.

Serialization
With this we can write and read whole classes with nested classes etc in them and is safer than XML/JSON because it’s less readable. Serialization supports:

custom non abstract classes with [Serializable] attribute.
custom structs with [Serializable] attribute. (Added in Unity 4.5)
references to objects that derive from UnityEngine.Object
primitive data types (int, float, double, bool, string, etc.)
array of a fieldtype we can serialize
List<T> of a fieldtype we can serialize

Google play games services API
this can be used to do the online-cloud storage of our game data so the players data is linked with their play store account and his/her progress is linked on multiple devices.
Have to look more into this in the future when we integrate google play.