Search Unity

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

Discussion in 'Scripting' started by IsGreen, Sep 2, 2014.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    To serialize objects, must precede the class by attribute [System.Serializable]:

    Code (CSharp):
    1. [System.Serializable]
    2. public class nameClass{
    3.    
    4.     ...
    5.    
    6. }
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Runtime.Serialization.Formatters.Binary;
    3. using System.IO;
    4.  
    5.  
    6. namespace GenericData{
    7.    
    8.     /// Generic Data class
    9.     public static class Data{
    10.        
    11.         /// <summary>Save Generic Data.
    12.         /// <para>Save file as Object in Persistent Data Path. <see cref="UnityEngine.Application.persistentDataPath"/> for more information.</para>
    13.         /// </summary>
    14.         public static bool SavePDP(System.Object data,string fileName){ return Save(data,Application.persistentDataPath+fileName); }
    15.         /// <summary>Save Generic Data.
    16.         /// <para>Save file as Object in custom Path.</para>
    17.         /// </summary>
    18.         public static bool Save(System.Object data, string pathFileName){
    19.            
    20.             FileStream file;
    21.            
    22.             try{ file = File.Create(pathFileName); }
    23.             catch { return false; }
    24.            
    25.             BinaryFormatter bf = new BinaryFormatter();
    26.            
    27.             try{ bf.Serialize(file,data); }
    28.             catch {
    29.                
    30.                 file.Close();
    31.                 File.Delete(pathFileName);
    32.                 return false;
    33.                
    34.             }
    35.            
    36.             file.Close();
    37.             return true;
    38.            
    39.         }
    40.        
    41.         /// <summary>Load Generic Data.
    42.         /// <para>Load file as Object from Persistent Data Path. <see cref="UnityEngine.Application.persistentDataPath"/> for more information.</para>
    43.         /// </summary>
    44.         public static System.Object LoadPDP(string fileName){ return Load(Application.persistentDataPath+fileName); }
    45.         /// <summary>Load Generic Data.
    46.         /// <para>Load file as Object from custom Path.</para>
    47.         /// </summary>
    48.         public static System.Object Load(string pathFileName){
    49.            
    50.             if(!File.Exists(pathFileName)) return null;
    51.            
    52.             BinaryFormatter bf = new BinaryFormatter();
    53.             FileStream file = File.Open(pathFileName,FileMode.Open);
    54.            
    55.             System.Object data;
    56.            
    57.             try{ data = bf.Deserialize(file); }
    58.             catch {
    59.                
    60.                 file.Close();
    61.                 return null;
    62.                
    63.             }
    64.            
    65.             file.Close();
    66.             return data;
    67.            
    68.         }
    69.        
    70.     }
    71.    
    72. }
    And this, test script example:

    Code (CSharp):
    1. using UnityEngine;
    2. using GenericData;
    3.  
    4. [System.Serializable]
    5. public class oneClass{
    6.    
    7.     public int integer1 = 1;
    8.     public int integer2 = 2;
    9.    
    10. }
    11.  
    12. [System.Serializable]
    13. public class twoClass{
    14.    
    15.     public float sencillo1 = 5f;
    16.     public float sencillo2 = 6f;
    17.    
    18. }
    19.  
    20. [System.Serializable]
    21. public class threeClass{
    22.    
    23.     public double doble1 = 10.123421234;
    24.     public double doble2 = 11.123412342;
    25.    
    26. }
    27.  
    28. public class genericTest : MonoBehaviour {
    29.    
    30.     // Use this for initialization
    31.     void Start () {
    32.        
    33.         oneClass clas1 = new oneClass();
    34.         twoClass clas2 = new twoClass();
    35.         threeClass clas3 = new threeClass();
    36.        
    37.         Data.SavePDP(clas1,"clase1");
    38.         Data.SavePDP(clas2,"clase2");
    39.         Data.SavePDP(clas3,"clase3");
    40.        
    41.         oneClass c1 = (oneClass)Data.LoadPDP("clase1");
    42.         twoClass c2 = (twoClass)Data.LoadPDP("clase2");
    43.         threeClass c3 = (threeClass)Data.LoadPDP("clase3");
    44.        
    45.         Debug.Log(c1.integer1+" : "+c1.integer2);
    46.         Debug.Log(c2.sencillo1+" : "+c2.sencillo2);
    47.         Debug.Log(c3.doble1+" : "+c3.doble2);
    48.        
    49.     }  
    50.    
    51. }
    Save and load the class data in the form of type System.Object, so loading must explicitly specify the actual type of the object.
     
  2. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    More than a year old post, but I just wanted to say thanks for this. Time saver. :)
     
  3. ssmas

    ssmas

    Joined:
    Jan 3, 2019
    Posts:
    3
    Literally awesome, way better than Playerprefs