0
\$\begingroup\$

I've wanted to save progress of my game (i don't have the code , sorry) without using PlayerPrefs. If it's possible ; with UnityEngine.

\$\endgroup\$
2
  • 4
    \$\begingroup\$ This is done the same way as saving files in regular C#. The only unity/gaming-specific consideration is that you should save to the persistent data path Unity provides. The rest is vanilla C# file IO - check StackOverflow for details on that. \$\endgroup\$
    – DMGregory
    Jan 30, 2016 at 17:59
  • \$\begingroup\$ @DMGregory That itself would have been a worthwhile an answer I think. \$\endgroup\$ Feb 12, 2016 at 14:42

1 Answer 1

0
\$\begingroup\$

Let's say you've got a set of variables that stand for your save game entity. You can, for example, create a class within your save manager script like this:

[Serializable]
public class GameSave {
    public DateTime lastMod  { get; set; }
    public int games { get; set; }
    public int coins { get; set; }
    public int bestScore { get; set; }
}

Don't forget the [Serializable] attribute. Then you can write a couple of methods that helps you handle the loading/saving logic, as follow:

public void SaveGame(GameSave gameSave) {
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create(Application.persistentDataPath + "/save_game.dat");
    bf.Serialize(file, gameSave);
    file.Close();
}

public GameSave LoadGame() {
    if (File.Exists(Application.persistentDataPath + "/save_game.dat")) {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Open(Application.persistentDataPath + "/save_game.dat", FileMode.Open);
        GameSave gameSave = (GameSave) bf.Deserialize(file);
        file.Close();
        return gameSave;
    } else {
        Debug.Log(string.Format("File doesn't exist at path: {0}{1}", Application.persistentDataPath, "/save_game.dat"));
        return null;
    }

}

Consider also that in this way your save game data will be easily accessible from malicious users who want to hack your game. A much better approach involves an encryption/decryption logic. You can, as a simple example, save your SaveGame object as a json string and than encrypt+save into a file. Anyway, the only way to implement a robust encryption logic is through a client-server handling of encryption keys.

\$\endgroup\$
2
  • \$\begingroup\$ Regarding encrypting savegames: This issue is addressed in the question "How to encrypt Save Files without using a key?" Spoiler: It's of questionable usefulness. \$\endgroup\$
    – Philipp
    Jan 31, 2016 at 17:18
  • 1
    \$\begingroup\$ @Philipp Good answer. [Un]fortunately, with services like Play Game Services and Game Center, the need of, at least, leaderboards shared across players is big and so the importance of legit scores. The problem has no solution, we can just try to stop less motivated malicious users making their work harder. \$\endgroup\$
    – Dolfiz
    Jan 31, 2016 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .