Rei's code

mail@pastecode.io avatar
unknown
csharp
2 years ago
3.0 kB
2
Indexable
Never
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public static class SaveManager
{
    public static void SaveFile<T>(T objectT, string fileName, bool toBinary = false, string fileType = "json", string PathFolders = "")
    {
        if (toBinary)
            SaveToFilePath(objectT, fileName, fileType, PathFolders);
        else
            SaveToPlayerPref(objectT, fileName);
        
    }

    private static void SaveToFilePath<T>(T objectT, string fileName, string fileType, string PathFolders)
    {
        string path = string.Concat(Application.persistentDataPath, "/", PathFolders, fileName, ".", fileType);

        if (!Directory.Exists(string.Concat(Application.persistentDataPath, "/", PathFolders)))
            Directory.CreateDirectory(string.Concat(Application.persistentDataPath, "/", PathFolders));

        BinaryFormatter binaryFormatter = new BinaryFormatter();
        FileStream fileStream = new FileStream(path, FileMode.Create);
        binaryFormatter.Serialize(fileStream, objectT);
        fileStream.Close();
    }

    private static void SaveToPlayerPref<T>(T objectT, string fileName)
    {
        PlayerPrefs.SetString(fileName, JsonUtilityHandler.ConvertObjectToJson(objectT));
        PlayerPrefs.Save();
    }

    public static T Load<T>(string fileName, bool fromBinary = false, string fileType = "json", string PathFolders = "") where T : class
    {

        T objectLoaded = null;

        if (fromBinary == false)
            objectLoaded = LoadFromPlayerPref<T>(fileName);
        else
            objectLoaded = LoadFromFilePath<T>(fileName, fileType, PathFolders);
       
      
        if (objectLoaded == null)
            Debug.Log("No Save File");

        return objectLoaded;

    }

    private static T LoadFromPlayerPref<T>(string fileName) where T : class
    {
        
        if (PlayerPrefs.HasKey(fileName))
            return JsonUtilityHandler.LoadFromJson<T>(PlayerPrefs.GetString(fileName));
        
        return null;
    }

    private static T LoadFromFilePath<T>(string fileName, string fileType, string PathFolders) where T : class
    { 
        string path = string.Concat(Application.persistentDataPath, "/", PathFolders, fileName, ".", fileType);

        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream fileStream = new FileStream(path, FileMode.Open);
            T loadedObject =(T)formatter.Deserialize(fileStream) ;
            fileStream.Close();
            return loadedObject;
        }
        return null;
    }
}

public static class JsonUtilityHandler
{
    public static string ConvertObjectToJson (object data) 
    => JsonUtility.ToJson(data);
    
    public static void LoadOverrideFromJson(string json, object data)
    => JsonUtility.FromJsonOverwrite(json, data);
    
    public static T LoadFromJson<T>(string json) 
    =>JsonUtility.FromJson<T>(json);

}