Advertisement
Eiromplays

Save Profiles

Sep 24th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6.  
  7. public static class SaveProfile
  8. {
  9.     public static void ProfileSave(ProfileData profile)
  10.     {
  11.         List<ProfileData> profileData = LoadProfiles(); //get the current profiles
  12.         Debug.Log(profile.profileName + " " + profile.companyName + " " + profile.profileImage + " " + profile.profileID);
  13.  
  14.         profileData.Add(profile); //adds profile to the list
  15.  
  16.         BinaryFormatter formatter = new BinaryFormatter();
  17.         string path = Application.persistentDataPath + "/Profiles/profiles.dat";
  18.         FileStream stream = new FileStream(path, FileMode.Create);    
  19.         formatter.Serialize(stream, profileData); //saving the whole list
  20.         stream.Close();
  21.     }
  22.  
  23.     public static List<ProfileData> LoadProfiles()
  24.     {
  25.         if (File.Exists(Application.persistentDataPath + "/Profiles/profiles.dat"))
  26.         {
  27.             if(new FileInfo(Application.persistentDataPath + "/Profiles/profiles.dat").Length == 0)
  28.             {
  29.                 return null;
  30.             }
  31.             else
  32.             {
  33.                 BinaryFormatter formatter = new BinaryFormatter();
  34.                 FileStream stream = new FileStream(Application.persistentDataPath + "/Profiles/profiles.dat", FileMode.Open);
  35.                 List<ProfileData> profileData = (List<ProfileData>)formatter.Deserialize(stream);
  36.                 Debug.Log(profileData[0]);
  37.                 stream.Close();
  38.                 return new List<ProfileData>();
  39.             }
  40.         }
  41.         else
  42.         {
  43.             Debug.LogError("Save file not found in " + Application.persistentDataPath + "/Profiles/profiles.dat");
  44.             return null;
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement