SaveManager.cs 815 B

1234567891011121314151617181920212223242526272829303132333435
  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. string json = JsonUtility.ToJson(data);
  13. File.WriteAllText(filePath, json);
  14. Debug.Log("Game Saved to " + filePath);
  15. }
  16. public GameData LoadGame()
  17. {
  18. if (File.Exists(filePath))
  19. {
  20. string json = File.ReadAllText(filePath);
  21. GameData data = JsonUtility.FromJson<GameData>(json);
  22. Debug.Log("Game loaded from " + filePath);
  23. return data;
  24. }
  25. else
  26. {
  27. Debug.Log("Save file not found!");
  28. return null;
  29. }
  30. }
  31. }