| | |
| | | public static class SaveSystem |
| | | { |
| | | public static bool isGameLoaded = false; |
| | | public static void SavePlayer(PlayerController player) |
| | | public static void SavePlayer(SaveDataPlayer player) |
| | | { |
| | | if (player != null) |
| | | { |
| | | string path = System.IO.Path.Combine(Application.persistentDataPath, "player.savefile"); |
| | | |
| | | SaveData saveData = new SaveData(player); |
| | | string saveDataJSON = JsonUtility.ToJson(saveData); |
| | | string saveDataJSON = JsonUtility.ToJson(player); |
| | | |
| | | using (FileStream fs = new FileStream(path, FileMode.Create)) |
| | | { |
| | |
| | | } |
| | | else |
| | | { |
| | | Debug.Log("Not saved as no playerController was sent"); |
| | | Debug.Log("Not saved as no playerData was sent"); |
| | | } |
| | | } |
| | | |
| | | public static SaveData LoadPlayer () |
| | | public static SaveDataPlayer LoadPlayer() |
| | | { |
| | | SaveData result; |
| | | SaveDataPlayer result; |
| | | string path = System.IO.Path.Combine(Application.persistentDataPath, "player.savefile"); |
| | | if (File.Exists(path)) |
| | | { |
| | |
| | | using (StreamReader sr = new StreamReader(fs)) |
| | | { |
| | | string saveData = sr.ReadToEnd(); |
| | | result = JsonUtility.FromJson<SaveData>(saveData); |
| | | result = JsonUtility.FromJson<SaveDataPlayer>(saveData); |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | Debug.LogError("Save file not found in: " + path); |
| | | return null; |
| | | } |
| | | return result; |
| | | } |
| | | public static void SaveMapState(SaveDataMap mapState) |
| | | { |
| | | if (mapState != null) |
| | | { |
| | | string path = System.IO.Path.Combine(Application.persistentDataPath, "map.savefile"); |
| | | string saveDataJSON = JsonUtility.ToJson(mapState); |
| | | |
| | | using (FileStream fs = new FileStream(path, FileMode.Create)) |
| | | { |
| | | using (StreamWriter sw = new StreamWriter(fs)) |
| | | { |
| | | sw.Write(saveDataJSON); |
| | | |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | Debug.Log("Not saved as no mapData was sent"); |
| | | } |
| | | } |
| | | |
| | | public static SaveDataMap LoadMapState() |
| | | { |
| | | SaveDataMap result; |
| | | string path = System.IO.Path.Combine(Application.persistentDataPath, "map.savefile"); |
| | | if (File.Exists(path)) |
| | | { |
| | | using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) |
| | | { |
| | | using (StreamReader sr = new StreamReader(fs)) |
| | | { |
| | | string saveData = sr.ReadToEnd(); |
| | | result = JsonUtility.FromJson<SaveDataMap>(saveData); |
| | | } |
| | | } |
| | | } |