1
0

SaveManager.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using UnityEngine;
  2. using System.IO;
  3. public class SaveManager : MonoBehaviour
  4. {
  5. private string filePath;
  6. private void Start()
  7. {
  8. filePath = Application.persistentDataPath + "/savegame.json";
  9. }
  10. public void SaveGame(GameData data)
  11. {
  12. if (filePath == null)
  13. filePath = Application.persistentDataPath + "/savegame.json";
  14. string json = JsonUtility.ToJson(data);
  15. File.WriteAllText(filePath, json);
  16. Debug.Log("Game Saved to " + filePath);
  17. }
  18. public GameData LoadGame()
  19. {
  20. if (filePath == null)
  21. filePath = Application.persistentDataPath + "/savegame.json";
  22. if (File.Exists(filePath))
  23. {
  24. string json = File.ReadAllText(filePath);
  25. GameData data = JsonUtility.FromJson<GameData>(json);
  26. Debug.Log("Game loaded from " + filePath);
  27. return data;
  28. }
  29. else
  30. {
  31. Debug.Log("Save file not found!");
  32. return null;
  33. }
  34. }
  35. }