Skip to content

htcni/unity-save-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Unity Save System

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.

Usage

import namespace

using BigFrogTools;

Dependencies

Requires newtonsoft json. Get it here.

Saving data

Saving simple data

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.

Retrieve data

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

Get save dump

SaveSystem.GetSaveDump()
{"localData":{"health":"100","levelCompleted":"false","distance":"15.55"}}

Deleting a value

SaveSystem.RemoveData("health")

Reset data

It removes all the data from saved file

SaveSystem.ResetSaveData();

Saving complex data

Since everything is serialized as string we can store any type of data. But we have to do some black magic while parsing it.

Store an array

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.

Convert Data

We can also parse our primitive data with this method.

int tempHealth;
int health = SaveSystem.ConvertValue(SaveSystem.GetData("health"), tempHealth)

Tips

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.

About

A simple save system for unity.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages