├── DataToSave └── Player.cs ├── ISaveAndLoad.cs ├── LICENSE ├── README.md ├── SaveableObject.cs └── SavingManager.cs /DataToSave/Player.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | public class Player : ISaveAndLoad 6 | { 7 | public int health; 8 | public float POSITIONx; 9 | public float POSITIONy; 10 | public float POSITIONz; 11 | 12 | public void Load(int LoadingIndex) 13 | { 14 | PlayerData pd = SavingManager.LoadData(PlayerData.key, LoadingIndex); 15 | 16 | POSITIONx = pd.POSx; 17 | POSITIONy = pd.POSy; 18 | POSITIONz = pd.POSz; 19 | 20 | health = pd.life; 21 | } 22 | 23 | public void Save(int SavingIndex) 24 | { 25 | PlayerData pd = new PlayerData(POSITIONx, POSITIONy, POSITIONz, health); 26 | SavingManager.SaveData(pd, SavingIndex); 27 | } 28 | } 29 | 30 | [System.Serializable] 31 | public class PlayerData : SaveableObject 32 | { 33 | public static string key = "Player"; 34 | 35 | public float POSx; 36 | public float POSy; 37 | public float POSz; 38 | 39 | public int life; 40 | 41 | public PlayerData(float x, float y, float z, int life) 42 | { 43 | POSx = x; 44 | POSy = y; 45 | POSz = z; 46 | 47 | this.life = life; 48 | } 49 | 50 | public override string GetKey() 51 | { 52 | return key; 53 | } 54 | 55 | public override string GetPrefabPath() 56 | { 57 | return "Path of the prefab that you want to instantiate"; 58 | } 59 | 60 | // use this function only if your using unity, you can use this function along if GetPrefabPath to Instantiate all entities that you want when you're loading the game 61 | public override bool isInstantiatable() 62 | { 63 | return true; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /ISaveAndLoad.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public interface ISaveAndLoad 4 | { 5 | void Save(int indexToSave); 6 | void Load(int indexToLoad); 7 | } 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Nicolas Pereira Coutinho 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Save and Load System 2 | A simple, but yet very powerful save system that i have made using C#/.NET and generics... 3 | I've made for using in games made with Unity, but with very few alterations you can use it for everything that you want. 4 | 5 | ## How To Use: 6 | 7 | The **Player.cs** Class is an example of a file that you would want to save, the class must derive From the **ISaveAndLoad** interface and Implement the Save and Load methods. 8 | 9 | To save the player's data you need to Create a class just for the data, like i did in the example file **Player.cs**. 10 | 11 | Inside of this file you have a class called **PlayerData** that derives from **SaveableObject** and the class needs to have the ```[System.Serializable]``` Attribute, you must define a **key** for this class, i used in this example "**Player**" as the key, but you can create any **key** that you want. 12 | 13 | ### Are you using Unity? 14 | 15 | - IF YOU ARE using this save system for a Unity game, you can implement the ```GetPrefabPath()``` and ```IsInstantiable()``` methods, these are the methods that you can use to define if you want to Instantiate the Object when loading the game or not. 16 | 17 | - IF YOU ARE NOT using this save system for a Unity game, you can just delete these methods from the **SaveableObject** class. 18 | 19 | 20 | The Saving Manager is where all the magic happens, the first thing that you want to do when you acess this script is to define the **SavingPath** variable, you need to define a path to save your files. 21 | 22 | After defining the path to save your files, you are ready to go. you can save your data by calling the ```SaveData<>()``` method in the player class or any class the you create that derives from ISaveAndLoad interface and passing 2 arguments. 23 | 24 | The first argument(or parameter) is the dataToSave, this would be your **PlayerData** class or any class that you create that derives form **SaveableObject**. 25 | 26 | And the second argument is an integer called **Save**, this parameter is useful if you want to have multiple save files, but if you just want to have one save file, you can pass the number 1(or any number that you want) to the argument. 27 | 28 | If want to load the file, is basically the same process, first you need to call the ```LoadData<>()``` method in the player class or any class the you create that derives from **ISaveAndLoad** interface. 29 | In the first argument you pass the key, in this example case the key would be "Player", and the second parameter you need to put the same number that you put in the ```SaveData<>()``` method. 30 | -------------------------------------------------------------------------------- /SaveableObject.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | 4 | [System.Serializable] 5 | public abstract class SaveableObject 6 | { 7 | public abstract bool isInstantiatable(); 8 | 9 | public abstract string GetKey (); 10 | 11 | public virtual string GetPrefabPath () 12 | { 13 | Debug.LogError ("Method not implemented"); 14 | 15 | return ""; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /SavingManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Runtime.Serialization.Formatters.Binary; 6 | 7 | namespace survivalPrototype 8 | { 9 | public static class SavingManager 10 | { 11 | public static string SavingPath; 12 | 13 | public static void SaveData(T dataToSave, int Save) where T : SaveableObject 14 | { 15 | if (!Directory.Exists (SavingPath)) 16 | Directory.CreateDirectory (SavingPath); 17 | 18 | string path = SavingPath + "Save" + Save.ToString() + "/" + dataToSave.GetKey() +".banana"; 19 | 20 | BinaryFormatter bf = new BinaryFormatter (); 21 | FileStream fs = new FileStream (path, FileMode.Create); 22 | 23 | bf.Serialize (fs, dataToSave); 24 | fs.Close (); 25 | } 26 | 27 | public static T LoadData(string key, int Save) where T : SaveableObject 28 | { 29 | string path = SavingPath + "Save" + Save.ToString() + "/" + key +".banana"; 30 | 31 | if (File.Exists (path)) 32 | { 33 | FileStream fs = new FileStream (path, FileMode.Open); 34 | BinaryFormatter bf = new BinaryFormatter (); 35 | 36 | T objectToReturn = (T)bf.Deserialize (fs); 37 | fs.Close (); 38 | 39 | return objectToReturn; 40 | } 41 | 42 | return default(T); 43 | } 44 | 45 | public static bool HasSaveFile(string key, int Save) 46 | { 47 | return System.IO.File.Exists(SavingPath + "Save" + Save.ToString() + "/" + key +".banana"); 48 | } 49 | 50 | public static bool HasAnySaveFile(int save) 51 | { 52 | if (Directory.GetFiles (SavingPath + "Save" + save.ToString ()).Length > 0) 53 | { 54 | return true; 55 | } 56 | 57 | return false; 58 | } 59 | 60 | public static string[] GetObjectsToInstantiate(int save) 61 | { 62 | string[] dirs = Directory.GetFiles (SavingPath + "Save" + save.ToString()); 63 | 64 | List objectsToInstantiatePath = new List (); 65 | 66 | for (int i = 0; i < dirs.Length; i++) 67 | { 68 | FileStream fs = new FileStream (dirs[i], FileMode.Open); 69 | BinaryFormatter bf = new BinaryFormatter (); 70 | 71 | SaveableObject saveObject = (SaveableObject)bf.Deserialize (fs); 72 | fs.Close (); 73 | if (saveObject.isInstantiatable()) 74 | { 75 | string pathToInstantiateObject = saveObject.GetPrefabPath (); 76 | objectsToInstantiatePath.Add (pathToInstantiateObject); 77 | } 78 | } 79 | 80 | return objectsToInstantiatePath.ToArray (); 81 | } 82 | } 83 | } --------------------------------------------------------------------------------