diff --git a/Assets/Scripts/Utilities/SaveUtility.cs b/Assets/Scripts/Utilities/SaveUtility.cs new file mode 100644 index 0000000..96bb9b0 --- /dev/null +++ b/Assets/Scripts/Utilities/SaveUtility.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Utilities +{ + public static class SaveUtility + { + /* --- Usage ---- + + -- Lets say we have a serialized class called Save -- + + [System.Serializable] + public class Save + { + public int testInt; + public List TestStrings = new List(); + } + + -- For saving this class we would do the following -->> -- + + Save save = new Save(); + save.testInt = 5; + save.TestStrings.Add("Hello"); + + SaveUtility.Save(save, "testPath"); + + -- For loading this class we would do the following --> -- + + Save save = SaveUtility.Load("testPath"); + + Important : Since save operation uses PlayerPrefs and does not contain any encrypting. Dont save&load any confidential data using this class. + + --- End of Usage ---- */ + + + /// + /// Saves any serialized class on the specified savepath. + /// + public static void Save(this T save, string savePath) + where T : class + { + var data = JsonUtility.ToJson(save); + PlayerPrefs.SetString(savePath, data); + } + + /// + /// Loads and returns the saved class on the specified savepath. + /// If no save is found, it will return a new instance of the class. + /// + /// Loaded serialized save class + public static T Load(TSt path) + where T : class, new() + where TSt : IComparable, ICloneable, IConvertible, IComparable, IEnumerable, IEnumerable, IEquatable + { + var data = new T(); + + if (PlayerPrefs.HasKey(path as string) == false) + { + data.Save(path as string); + } + + { + data = JsonUtility.FromJson(PlayerPrefs.GetString(path as string)); + } + return data; + } + } +} diff --git a/Assets/Scripts/Utilities/SaveUtility.cs.meta b/Assets/Scripts/Utilities/SaveUtility.cs.meta new file mode 100644 index 0000000..390ab63 --- /dev/null +++ b/Assets/Scripts/Utilities/SaveUtility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7d8aad24cf3d7485c9507bf763b79d42 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: