Search code examples
unity-game-enginesaveplatform-independent

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


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.


Solution

  • 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.