Newer
Older
HardPoint-Project-Abertay-University-Unity3D / Assets / Scripts / Systems / SaveSystem / SaveManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;

public class SaveManager : MonoBehaviour
{
    string DirPath = Application.dataPath + "/Saves";
    public void CreateNewSave(string toSave, string fileName)
    {
        string filePath = Application.dataPath + "/Saves/" + fileName+".json";

        if (!Directory.Exists(DirPath))
        {
            Directory.CreateDirectory(Application.dataPath + "/Saves");
        }
        File.WriteAllText(filePath, toSave);
    }

    public bool HasSave(string fileName)
    {
        return File.Exists(DirPath + "/" + fileName + ".json");
    }

    public SaveStruct ReadSave(string fileName)
    {
        if (Directory.Exists(DirPath))
        {
            if (File.Exists(DirPath + "/" + fileName + ".json"))
            {
                string pathToSave = DirPath + "/" + fileName + ".json";
                StreamReader streamReader = new StreamReader(pathToSave);
                string temp = streamReader.ReadToEnd();
                SaveStruct json = JsonUtility.FromJson<SaveStruct>(temp);
                return json;
            }
        }
        Debug.LogError("Read Save Failed!");
        return new SaveStruct();
    }
}

[Serializable]
public struct SaveStruct
{
    public int currency;
    public List<ClassStats> classStats;

    public SaveStruct(int currency, List<ClassStats> classStats)
    {
        this.currency = currency;
        this.classStats = classStats;
    }
}