UnityGameProjectsCode / RelianceGame / Save Data Control / SaveSystem.cs
SaveSystem.cs
Raw
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public static class SaveSystem
{
    public static void SaveAllData(SaveFileManager saveMgr, HUBTracker tracker, InventoryManager manager, string fileName)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/" + fileName + ".sav";
        FileStream stream = new FileStream(path, FileMode.Create);

        SaveGameData data = new SaveGameData(saveMgr, tracker, manager);

        formatter.Serialize(stream, data);

        stream.Close();
        Debug.Log("Save game data saved to file at " + path);
    }

    public static void SaveFileData(SaveFileManager saveMgr)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/SaveFileNames.data";
        FileStream stream = new FileStream(path, FileMode.Create);

        FileData data = new FileData(saveMgr);

        formatter.Serialize(stream, data);

        stream.Close();
        Debug.Log("Data saved to file at " + path);
    }

    public static void SaveSettingsData(SaveFileManager saveMgr)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/Settings.set";
        FileStream stream = new FileStream(path, FileMode.Create);

        SettingsData data = new SettingsData(saveMgr);

        formatter.Serialize(stream, data);

        stream.Close();
        Debug.Log("Settings data saved to file at " + path);
    }

    public static SaveGameData LoadAllSaveData(string fileName)
    {
        string path = Application.persistentDataPath + "/" + fileName + ".sav";

        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);
            SaveGameData data = formatter.Deserialize(stream) as SaveGameData;
            Debug.Log("Save game data retrieved from file at " + path);
            stream.Close();

            return data;
        }
        else
        {
            Debug.LogWarning("Save game data not found in " + path);
            return null;
        }
    }

    public static SettingsData LoadSettingsData()
    {
        string path = Application.persistentDataPath + "/Settings.set";

        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            SettingsData data = formatter.Deserialize(stream) as SettingsData;
            Debug.Log("Settings data retrieved from file at " + path);
            stream.Close();

            return data;
        }
        else
        {
            Debug.LogWarning("Settings data not found in " + path);
            return null;
        }
    }

    public static FileData LoadFileData()
    {
        string path = Application.persistentDataPath + "/SaveFileNames.data";

        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            FileData data = formatter.Deserialize(stream) as FileData;
            Debug.Log("File data retrieved from file at " + path);
            stream.Close();

            return data;
        }
        else
        {
            Debug.LogWarning("File data not found in " + path);
            return null;
        }
    }

    public static void DeleteSave(string fileName)
    {
        string path = Application.persistentDataPath + "/" + fileName + ".sav";
        File.Delete(path);
        Debug.Log("Save Game file deleted at " + path);
    }

    public static bool CheckIfSaveDataExists(string fileName)
    {
        string path = Application.persistentDataPath + "/" + fileName + ".sav";

        if (File.Exists(path))
        {
            //Debug.Log("Save data file was found at " + path);
            return true;
        }
        else
        {
            Debug.LogWarning("Save data file could not be found at " + path);
            return false;
        }
    }

    public static bool CheckIfSettingsExists()
    {
        string path = Application.persistentDataPath + "/Settings.set";

        if (File.Exists(path))
        {
            //Debug.Log("Settings file was found at " + path);
            return true;
        }
        else
        {
            Debug.LogWarning("Settings file could not be found at " + path);
            return false;
        }
    }

    public static bool CheckIfFileInfoExists()
    {
        string path = Application.persistentDataPath + "/SaveFileNames.data";

        if (File.Exists(path))
        {
            //Debug.Log("File info was found at " + path);
            return true;
        }
        else
        {
            Debug.LogWarning("File info could not be found at " + path);
            return false;
        }
    }
}