| 1234567891011121314151617181920212223242526272829303132333435 |
- using UnityEngine;
- using System.IO;
- public class SaveManager : MonoBehaviour
- {
- private string filePath;
- private void Start()
- {
- filePath = Application.persistentDataPath + "/savegame.json";
- }
- public void SaveGame(GameData data)
- {
- string json = JsonUtility.ToJson(data);
- File.WriteAllText(filePath, json);
- Debug.Log("Game Saved to " + filePath);
- }
- public GameData LoadGame()
- {
- if (File.Exists(filePath))
- {
- string json = File.ReadAllText(filePath);
- GameData data = JsonUtility.FromJson<GameData>(json);
- Debug.Log("Game loaded from " + filePath);
- return data;
- }
- else
- {
- Debug.Log("Save file not found!");
- return null;
- }
- }
- }
|