A simple unity save system to save data to a file. It store data in key-value format, similar to PlayerPerfs but a bit more flexible way.
import namespace
using BigFrogTools;
Requires newtonsoft json. Get it here.
Simple data can be saved very easily using a key-value pair format.
SaveSystem.SetData("health", 100);
SaveSystem.SetData("levelCompleted", false);
SaveSystem.SetData("distance": 15.55f)
Note: All the values get converted into string.
SaveSystem.GetData("health");
SaveSystem.GetData("distance")
Since all the values get saved as string. You need to parse it into proper data type.
int.parse(SaveSystem.GetData("health"));
float.parse(SaveSystem.GetData("distance"));
bool.parse(SaveSystem.GetData("levelCompleted"));
SaveSystem.GetSaveDump()
{"localData":{"health":"100","levelCompleted":"false","distance":"15.55"}}
SaveSystem.RemoveData("health")
It removes all the data from saved file
SaveSystem.ResetSaveData();
Since everything is serialized as string we can store any type of data. But we have to do some black magic while parsing it.
int[] someData = new int[5] { 15, 6, 7, 9, 2 };
SaveSystem.SetData("someData", someData);;
string tmpArr = SaveSystem.GetData("someData");
int[] newArr;
int[] myArr = SaveSystem.ConvertValue(tmpArr, newArr);
//float[] myArr = SaveSystem.ConvertValue(tmpArr, someData);
After it we loop through the elements.
We can also parse our primitive data with this method.
int tempHealth;
int health = SaveSystem.ConvertValue(SaveSystem.GetData("health"), tempHealth)
For more complex data I would suggest to store it in class then serialize it and save it to file. Since this serialization will keep data type intact and maintainable;
public class User {
public string username;
public int points;
public int xp;
}
User u = new User();
string userData = JsonConvert.SerializeObject(u);
SaveSystem.SetData("user", userData);
// Get data
u = JsonConvert.DeserializeObject<User>(SaveSystem.GetData("user"));
// Access
//u.points
We can store multiple serialize class and access it with a key. Since it's valid JSON we can sync this data with our server.