Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
4  
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. – DMGregory Jan 30 at 17:59
    
@DMGregory That itself would have been a worthwhile an answer I think. – Noctrine Feb 12 at 14:42
up vote 0 down vote accepted

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.

share|improve this answer
    
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. – Philipp Jan 31 at 17:18
1  
@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. – Dolfiz Jan 31 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.