Last active
March 31, 2024 15:59
-
-
Save fallenblood7080/172dd367ed7001c65470e35453467a67 to your computer and use it in GitHub Desktop.
Save Load System for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
/* | |
* Using - https://docs.unity3d.com/2019.4/Documentation/Manual/com.unity.nuget.newtonsoft-json.html for reading and writing json | |
*/ | |
/// <summary> | |
/// Save Load System. | |
/// Similar to PlayerPref/ | |
/// supported datatypes - string,char,float,bool,int | |
/// </summary> | |
public class SaveAndLoadManager : MonoBehaviour | |
{ | |
public static SaveLoadManager SaveLoad; | |
private static string _dataPath = Application.dataPath + "/gameData.json"; | |
private JsonSerializerSettings jsonSettings; | |
void Awake() | |
{ | |
DontDestroyOnLoad(gameObject); | |
if (SaveLoad == null) | |
{ | |
SaveLoad = this; | |
} | |
else | |
{ | |
Destroy(gameObject); | |
} | |
#if UNITY_EDITOR | |
_dataPath = Application.dataPath + "/gameData.json"; | |
#else | |
_dataPath = Application.persistentDataPath + "/gameData.fallen"; | |
#endif | |
jsonSettings = new() | |
{ | |
Formatting = Formatting.Indented | |
}; | |
} | |
/// <summary> | |
/// Save user Data as Key Value Pair | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="key">key of data by which it can be accessed - MUST BE UNIQUE</param> | |
/// <param name="data">data you want to save since it generic it can take any data</param> | |
public void SaveData(string key, object data) | |
{ | |
Dictionary<string, object> dictData = new(); | |
if (File.Exists(_dataPath)) | |
{ | |
string jsonData = File.ReadAllText(_dataPath, System.Text.Encoding.UTF8); | |
dictData = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonData, jsonSettings); | |
} | |
dictData[key] = data; | |
string newData = JsonConvert.SerializeObject(dictData, jsonSettings); | |
File.WriteAllText(_dataPath, newData, System.Text.Encoding.UTF8); | |
} | |
/// <summary> | |
/// Load Data by its Key | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="key">key of data</param> | |
/// <param name="defaultValue">specify the value that it can return when data is not found</param> | |
/// <returns>return your saved Data</returns> | |
public T LoadData<T>(string key, T defaultValue = default) | |
{ | |
if (File.Exists(_dataPath)) | |
{ | |
Dictionary<string, object> gameData = new(); | |
string jsonData = File.ReadAllText(_dataPath, System.Text.Encoding.UTF8); | |
gameData = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonData, jsonSettings); | |
if (gameData.TryGetValue(key, out object data)) | |
{ | |
return FixedType<T>(data); | |
} | |
else | |
{ | |
return defaultValue; | |
} | |
} | |
return defaultValue; | |
} | |
#if UNITY_EDITOR | |
/// <summary> | |
/// Delete the game data file | |
/// </summary> | |
[MenuItem("Save Data/Delete All")] | |
public static void DeleteAllSaveData() | |
{ | |
if (File.Exists(_dataPath)) | |
{ | |
File.Delete(_dataPath); | |
} | |
} | |
#endif | |
/// <summary> | |
/// Delete the specify saved data by its key | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="key">key of dat You want to delete</param> | |
public void DeleteSaveData<T>(string key) | |
{ | |
Dictionary<string, object> dictData = new(); | |
if (File.Exists(_dataPath)) | |
{ | |
string jsonData = File.ReadAllText(_dataPath, System.Text.Encoding.UTF8); | |
dictData = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonData, jsonSettings); | |
dictData.Remove(key); | |
string newData = JsonConvert.SerializeObject(dictData, jsonSettings); | |
File.WriteAllText(_dataPath, newData, System.Text.Encoding.UTF8); | |
} | |
} | |
private T FixedType<T>(object data) | |
{ | |
if (typeof(T) == typeof(int) && data is long) | |
{ | |
// Convert long to int | |
return (T)(object)Convert.ToInt32(data); | |
} | |
else if (typeof(T) == typeof(float) && data is double) | |
{ | |
return (T)((object)Convert.ToSingle(data)); | |
} | |
// No need to cast for other types | |
return (T)data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment