miepzerino
2023-12-19 07fdedc8529dadd02c0b0940dc38f8a00e93c0a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Json;
 
public static class SaveSystem
{
    public static bool isGameLoaded = false;
    public static void SavePlayer(PlayerController player)
    {
        if (player != null)
        {
            string path = System.IO.Path.Combine(Application.persistentDataPath, "player.savefile");
 
            SaveData saveData = new SaveData(player);
            string saveDataJSON = JsonUtility.ToJson(saveData);
 
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(saveDataJSON);
 
                }
            }
        }
        else
        {
            Debug.Log("Not saved as no playerController was sent");
        }
    }
 
    public static SaveData LoadPlayer ()
    {
        SaveData result;
        string path = System.IO.Path.Combine(Application.persistentDataPath, "player.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<SaveData>(saveData);
                }
            }
        }
        else
        {
            Debug.LogError("Save file not found in: " + path);
            return null;
        }
        return result;
    }
}