SaveSystem.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. public static class SaveSystem
  5. {
  6. public static void SaveGame(GameManager gameManager)
  7. {
  8. BinaryFormatter formatter = new BinaryFormatter();
  9. string path = Application.persistentDataPath + "/game.death";
  10. FileStream stream = new FileStream(path, FileMode.Create);
  11. GameData data = new GameData(gameManager);
  12. formatter.Serialize(stream, data);
  13. stream.Close();
  14. }
  15. public static GameData LoadGame()
  16. {
  17. string path = Application.persistentDataPath + "/game.death";
  18. if (File.Exists(path))
  19. {
  20. BinaryFormatter formatter = new BinaryFormatter();
  21. FileStream stream = new FileStream(path, FileMode.Open);
  22. GameData data = formatter.Deserialize(stream) as GameData;
  23. stream.Close();
  24. return data;
  25. }
  26. else
  27. {
  28. Debug.LogError("Save file doesn't exist: " + path);
  29. return null;
  30. }
  31. }
  32. }