The best way to save game data in Unity that's secure & platfrom independent

12.6k views Asked by At

I am looking for a way to save the users progress for my game, I am looking for something that can't be tampered with/modified. I would like to have to be able to be platform independent. And I would like it to be stored within the game files to the user can transfer their data to different computers (with a flash drive or something). (Correct me if I'm wrong in any area) But I believe because I need to to be secure and platform independent that removes player prefs from the equation. I know there is a way to save data by encrypting it with Binary, but I don't know how that works and if the user could transfer the data from computer to computer. Is there a better way of saving data? Is encrypting is through Binary the way to go? If so how would I do it? Thank you :) If you have any questions feel free to ask.

Edit: I understand nothing is completely secure, I'm looking for something that can stop the average user from going into a file, changing a float, and having tons and tons of money in game.

3

There are 3 answers

6
Marco Alonso Friz Palavecino On BEST ANSWER

The first way to save data in unity is use PlayerPrefs, using for example:

PlayerPrefs.SetString("key","value");
PlayerPrefs.SetFloat("key",0.0f);
PlayerPrefs.SetInt("key",0);
PlayerPrefs.Save();

and for get, you only need

PlayerPrefs.GetString("key","default");

The second way and the way that permit you stored in a independent file is use serialization, my prefer way is a use the json file for it.

1) make a class that will store the data (not necessarily it need extends of monobehaviour:

[System.Serializable]
public class DataStorer {
  public data1:String = "default value";
  public data2:Int = 4;
  public data3:bool = true;
  ....
}

and store it in another class with

DataStorer dataStorer  = new DataStorer();
.... // some change in his data
string json = JsonUtility.ToJson(this, true);//true for you can read the file
path = Path.Combine(Application.persistantDataPath, "saved files", "data.json");
File.WriteAllText(path, json);

and for read the data

string json= File.ReadAllText(path);
DataStorer dataStorer = new DataStorer();
JsonUtility.FromJsonOverwrite(json, dataStorer);

and now you dateStorer is loaded with the data in your json file.

4
zambari On

The previous answer mentiones two good methods for storing data (although there are still some quirks regarding writing files on different platforms), I'd like to add on to the subject of security, as mentioned in a comment here.

First of all, nothing is fully secure, there is always someone brighter out there that will find a flaw somewhere in your code, maybe unless you want full on crypto, which is not trivial with key management etc.

I understand from the question that he wants to prevent users from moving files between machines, or allow them to move the files between machines but seal them so that users cannot easily change data stored in them.

In either case, a trivial solution would work: generate a hashcode from your dataset, mangle with it a little bit (salt it or do whatever to jump to another hashcode). so you could have something like

{
 "name"="john",
 "score"="1234",
 "protection"="043DA33C"
}

if the 'protection' field is a hashcode of "john1234", it will not match "john9999", hence if the user doesn't know how you salt your protection, you will be able to tell that the save has been tampered with

0
TechnoLover On

I found a link of data encryption tool, which is very helpful according to your need, as you want to secure data on device (nothing 100% secure), there are 3 mode to secure data, APP, Device and Encryption key, you can choose as per your need.

See this link, it may help you. https://forum.unity.com/threads/data-encryption-tool-on-assets-store.738299/