Skip to content

Instantly share code, notes, and snippets.

@brenocogu
Last active July 31, 2022 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brenocogu/aa6b7e2284b54a7bfd9aa245d8938f7f to your computer and use it in GitHub Desktop.
Save brenocogu/aa6b7e2284b54a7bfd9aa245d8938f7f to your computer and use it in GitHub Desktop.
Useful Generic data serializer and loader
using System.IO;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Collections.Generic;
/// <summary>
/// Quick tip: Don't use the '.' inside fileExt parameters (i.e. correct: 'exe' | incorrect: '.exe')
/// </summary>
public class DataController
{
public static void BinnarySave<T>(T objectToSave, string fileName, string fileExt, string folderPath = "/")
{
try
{
BinaryFormatter bf = new BinaryFormatter();
string path = Application.persistentDataPath + folderPath + fileName + "." + fileExt;
FileStream fs = new FileStream(path, FileMode.Create);
bf.Serialize(fs, objectToSave);
fs.Close();
}
catch (Exception err)
{
Debug.LogWarning("Serialization error was occurred! " + err);
}
}
public static T BinnaryLoad<T>(string fileName, string fileExt, string folderPath = "/")
{
string path = Application.persistentDataPath + folderPath + fileName + "." + fileExt;
if (File.Exists(path))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(path, FileMode.Open);
T data = (T)Convert.ChangeType(bf.Deserialize(fs), typeof(T));
return data;
}
else
{
return default;
}
}
public static List<T> BinnaryListLoad<T>(string fileExt, string folder = "/")
{
List<T> grabbed = new List<T>();
string[] filePaths = Directory.GetFiles(Application.persistentDataPath + folder, "*." + fileExt);
foreach (string path in filePaths)
{
if (File.Exists(path))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(path, FileMode.Open);
T data = (T)Convert.ChangeType(bf.Deserialize(fs), typeof(T));
grabbed.Add(data);
}
}
return grabbed;
}
public static void RemoveFile(string fileName, string fileExt, string folder = "/", Action callback = null)
{
string path = Application.persistentDataPath + folder + fileName + "." + fileExt;
if (File.Exists(path))
{
File.Delete(path);
if (!File.Exists(path) && callback != null)
callback();
}
}
public static void CreateDir(string name)
{
Directory.CreateDirectory(Application.persistentDataPath + name);
}
public static bool FileExist(string fileName, string fileExt, string folder = "/")
{
string path = Application.persistentDataPath + folder + fileName + "." + fileExt;
return File.Exists(path);
}
public static void JsonSave<T>(T classSaver, string fileName, string fileExt)
{
string json = JsonUtility.ToJson(classSaver);
File.WriteAllText(Application.persistentDataPath + "/" + fileName + "." + fileExt, json);
}
public static T JsonLoad<T>(string fileName, string fileExt)
{
string json = File.ReadAllText(Application.persistentDataPath + "/" + fileName + "." + fileExt);
return JsonUtility.FromJson<T>(json);
}
}
@brenocogu
Copy link
Author

Revision #2:
-Removed debug in BinarySave
-Added new 'FileExists' function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment