miepzerino
2023-12-25 c6b7ce89c8d35af5fca62c1ddc97f4b1f5d31f86
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Json;
 
public static class SaveSystem
{
    public static bool isGameLoaded = false;
    public static void SavePlayer(SaveDataPlayer player)
    {
        if (player != null)
        {
            string path = System.IO.Path.Combine(Application.persistentDataPath, "player.savefile");
 
            string saveDataJSON = JsonUtility.ToJson(player);
 
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(saveDataJSON);
 
                }
            }
        }
        else
        {
            Debug.Log("Not saved as no playerData was sent");
        }
    }
 
    public static SaveDataPlayer LoadPlayer()
    {
        SaveDataPlayer 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<SaveDataPlayer>(saveData);
                }
            }
        }
        else
        {
            Debug.LogError("Save file not found in: " + path);
            return null;
        }
        return result;
    }
    public static void SaveMapState(SaveDataMap mapState)
    {
        if (mapState != null)
        {
            string path = System.IO.Path.Combine(Application.persistentDataPath, "map.savefile");
            string saveDataJSON = JsonUtility.ToJson(mapState);
 
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(saveDataJSON);
 
                }
            }
        }
        else
        {
            Debug.Log("Not saved as no mapData was sent");
        }
    }
 
    public static SaveDataMap LoadMapState()
    {
        SaveDataMap result;
        string path = System.IO.Path.Combine(Application.persistentDataPath, "map.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<SaveDataMap>(saveData);
                }
            }
        }
        else
        {
            Debug.LogError("Save file not found in: " + path);
            return null;
        }
        return result;
    }
}