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;
|
}
|
}
|