[CodeSource]Generic class to save/load any serialized object.

To serialize objects, must precede the class by attribute [System.Serializable]:

[System.Serializable]
public class nameClass{
   
    ...
   
}

This way, the class will be ready to be saved or loaded via serialization.

This is the namespace ‘GenericData’ where the functions saved and loaded into the class Data:

using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


namespace GenericData{
   
    /// Generic Data class
    public static class Data{
       
        /// <summary>Save Generic Data.
        /// <para>Save file as Object in Persistent Data Path. <see cref="UnityEngine.Application.persistentDataPath"/> for more information.</para>
        /// </summary>
        public static bool SavePDP(System.Object data,string fileName){ return Save(data,Application.persistentDataPath+fileName); }
        /// <summary>Save Generic Data.
        /// <para>Save file as Object in custom Path.</para>
        /// </summary>
        public static bool Save(System.Object data, string pathFileName){
           
            FileStream file;
           
            try{ file = File.Create(pathFileName); }
            catch { return false; }
           
            BinaryFormatter bf = new BinaryFormatter();
           
            try{ bf.Serialize(file,data); }
            catch {
               
                file.Close();
                File.Delete(pathFileName);
                return false;
               
            }
           
            file.Close();
            return true;
           
        }
       
        /// <summary>Load Generic Data.
        /// <para>Load file as Object from Persistent Data Path. <see cref="UnityEngine.Application.persistentDataPath"/> for more information.</para>
        /// </summary>
        public static System.Object LoadPDP(string fileName){ return Load(Application.persistentDataPath+fileName); }
        /// <summary>Load Generic Data.
        /// <para>Load file as Object from custom Path.</para>
        /// </summary>
        public static System.Object Load(string pathFileName){
           
            if(!File.Exists(pathFileName)) return null;
           
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(pathFileName,FileMode.Open);
           
            System.Object data;
           
            try{ data = bf.Deserialize(file); }
            catch {
               
                file.Close();
                return null;
               
            }
           
            file.Close();
            return data;
           
        }
       
    }
   
}

And this, test script example:

using UnityEngine;
using GenericData;

[System.Serializable]
public class oneClass{
   
    public int integer1 = 1;
    public int integer2 = 2;
   
}

[System.Serializable]
public class twoClass{
   
    public float sencillo1 = 5f;
    public float sencillo2 = 6f;
   
}

[System.Serializable]
public class threeClass{
   
    public double doble1 = 10.123421234;
    public double doble2 = 11.123412342;
   
}

public class genericTest : MonoBehaviour {
   
    // Use this for initialization
    void Start () {
       
        oneClass clas1 = new oneClass();
        twoClass clas2 = new twoClass();
        threeClass clas3 = new threeClass();
       
        Data.SavePDP(clas1,"clase1");
        Data.SavePDP(clas2,"clase2");
        Data.SavePDP(clas3,"clase3");
       
        oneClass c1 = (oneClass)Data.LoadPDP("clase1");
        twoClass c2 = (twoClass)Data.LoadPDP("clase2");
        threeClass c3 = (threeClass)Data.LoadPDP("clase3");
       
        Debug.Log(c1.integer1+" : "+c1.integer2);
        Debug.Log(c2.sencillo1+" : "+c2.sencillo2);
        Debug.Log(c3.doble1+" : "+c3.doble2);
       
    }   
   
}

Save and load the class data in the form of type System.Object, so loading must explicitly specify the actual type of the object.

5 Likes

More than a year old post, but I just wanted to say thanks for this. Time saver. :slight_smile:

Literally awesome, way better than Playerprefs