dronkowitz

SaveLoad.cs

Feb 27th, 2021 (edited)
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  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.     public int EmeraldsCount
  49.     {
  50.         get
  51.         {
  52.             int count = 0;
  53.             foreach (bool b in ChaosEmerald)
  54.             {
  55.                 if (b) count++;
  56.             }
  57.             return count;
  58.         }
  59.     }
  60.    
  61.     public float TotalTime
  62.     {
  63.         get
  64.         {
  65.             float seconds = 0;
  66.             foreach(float time in Times)
  67.             {
  68.                 seconds += time;
  69.             }
  70.             return seconds;
  71.         }
  72.     }
  73.  
  74. }
  75.  
  76. public enum TEAMS
  77. {
  78.     Sonic,
  79.     Dark,
  80.     Rose,
  81.     Chaotix
  82. }
  83.  
Add Comment
Please, Sign In to add comment