dronkowitz

SaveLoad.cs

Feb 27th, 2021 (edited)
919
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Runtime.Serialization.Formatters.Binary;
  2. using System.IO;
  3. using UnityEngine;
  4. public static class SaveLoad
  5. {
  6.     public static int slotSelected;
  7.     public static SaveData SavedGame;
  8.  
  9.     private static string SaveSlotName(int saveSlot)
  10.     {
  11.         return Application.persistentDataPath + "/Slot" + saveSlot + ".sav";
  12.     }
  13.     public static void SaveGame(SaveData dataToSave, int saveSlot)
  14.     {
  15.         BinaryFormatter bf = new BinaryFormatter();
  16.         FileStream file = File.Create(SaveSlotName(saveSlot));
  17.         bf.Serialize(file, dataToSave);
  18.         file.Close();
  19.         Load(saveSlot);
  20.  
  21.     }
  22.  
  23.     public static bool Load(int saveSlot)
  24.     {
  25.         if (File.Exists(SaveSlotName(saveSlot)))
  26.         {
  27.             BinaryFormatter bf = new BinaryFormatter();
  28.             FileStream file = File.Open(SaveSlotName(saveSlot), FileMode.Open);
  29.  
  30.             SavedGame = (SaveData)bf.Deserialize(file);
  31.             file.Close();
  32.             return true;
  33.         }
  34.         else return false;
  35.     }
  36. }
  37.  
  38. [System.Serializable]
  39. public class SaveData
  40. {
  41.     public float[] Times = new float[21];
  42.    
  43.     public int Score=0;
  44.     public string CurrentLevel="Sea Gate";
  45.     public bool[] ChaosEmerald=new bool[7];
  46.     public int TeamUsed=0;
  47.     public int Lives=3;
  48.    
  49.  
  50. }
  51.  
RAW Paste Data