39 public static string saveDataPath = Application.persistentDataPath;
46 [MenuItem(
"File/Open saves data folder",
false, 300)]
53 Debug.LogError(
"The folder containing the saved data can not be opened using the method OpenSavedDataFolder from the class SaveDataManager outside the Unity Editor.");
66 public static void Save<T>(T objectToSave,
string filename,
string encryptionPassword =
null, Encoding encoding =
null)
71 encoding ??= Encoding.UTF8;
72 objectToSave ??=
default(T);
73 if (
string.IsNullOrEmpty(filename))
74 throw new System.ArgumentNullException(nameof(filename));
79 #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
80 #if UNITY_WSA || UNITY_WINRT
81 UnityEngine.Windows.Directory.CreateDirectory ( filePath );
83 Directory.CreateDirectory(Path.GetDirectoryName(filePath) ??
string.Empty);
86 if (!encryptionPassword.IsNullEmptyOrWhiteSpace())
88 stream =
new MemoryStream();
92 #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
95 #if UNITY_WSA || UNITY_WINRT
96 stream =
new MemoryStream ();
98 stream = File.Create(filePath);
103 stream =
new MemoryStream();
106 stream =
new MemoryStream ();
111 serializer.Serialize(objectToSave, stream, encoding);
112 if (!encryptionPassword.IsNullEmptyOrWhiteSpace())
114 string data = System.Convert.ToBase64String(((MemoryStream) stream).ToArray());
115 string encoded = encoder.
Encode(data, encryptionPassword);
116 #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
119 #if UNITY_WSA || UNITY_WINRT
120 UnityEngine.Windows.File.WriteAllBytes ( filePath, encoding.GetBytes ( encoded ) );
122 File.WriteAllText(filePath, encoded, encoding);
127 PlayerPrefs.SetString(filePath, encoded);
131 PlayerPrefs.SetString ( filePath, encoded );
137 string data = encoding.GetString(((MemoryStream) stream).ToArray());
138 PlayerPrefs.SetString(filePath, data);
154 public static T
Load<T>(
string filename, T defaultValue,
string encryptionPassword =
null, Encoding encoding =
null,
bool supressFileNotFoundWarning =
false)
159 encoding ??= Encoding.UTF8;
160 defaultValue ??=
default(T);
161 if (
string.IsNullOrEmpty(filename))
162 throw new System.ArgumentNullException(nameof(filename));
164 T result = defaultValue;
166 #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
169 if ( !
Exists ( filePath, path ) )
172 if (!supressFileNotFoundWarning)
174 $
"The file '{GetFilePath(filename)}' was not found. You can use the parameter 'supressFileNotFoundWarning' to disable the warning or use 'Exists()' to check if such file exists or not before trying to load them.\n" +
175 "Returning the default(T) instance."
180 if (!encryptionPassword.IsNullEmptyOrWhiteSpace())
183 #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
186 #if UNITY_WSA || UNITY_WINRT
187 data = encoding.GetString (
UnityEngine.Windows.File.ReadAllBytes ( filePath ) );
189 data = File.ReadAllText(filePath, encoding);
194 data = PlayerPrefs.GetString(filePath);
197 data = PlayerPrefs.GetString ( filePath );
199 string decoded = encoder.
Decode(data, encryptionPassword);
200 stream =
new MemoryStream(System.Convert.FromBase64String(decoded),
true);
204 #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
207 #if UNITY_WSA || UNITY_WINRT
208 stream =
new MemoryStream (
UnityEngine.Windows.File.ReadAllBytes ( filePath ) );
210 stream = File.OpenRead(filePath);
215 string data = PlayerPrefs.GetString(filePath);
216 stream =
new MemoryStream(encoding.GetBytes(data));
219 string data = PlayerPrefs.GetString ( filePath );
220 stream =
new MemoryStream ( encoding.GetBytes ( data ) );
223 result = serializer.Deserialize<T>(stream, encoding);
227 result = defaultValue;
236 public static bool Exists(
string filename)
238 if (
string.IsNullOrEmpty(filename))
240 throw new System.ArgumentNullException(
"filename");
244 #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
248 #if UNITY_WSA || UNITY_WINRT
249 exists =
UnityEngine.Windows.Directory.Exists ( filePath );
251 exists = Directory.Exists(filePath);
255 #if UNITY_WSA || UNITY_WINRT
256 exists =
UnityEngine.Windows.File.Exists ( filePath );
258 exists = File.Exists(filePath);
265 return PlayerPrefs.HasKey(filePath);
268 return PlayerPrefs.HasKey ( filePath );
275 return $
"{saveDataPath}/{filename.Trim('/')}.json";
282 public static void Delete(
string filename)
284 if (
string.IsNullOrEmpty(filename))
286 throw new System.ArgumentNullException(nameof(filename));
290 #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
293 #if UNITY_WSA || UNITY_WINRT
296 File.Delete(filePath);
301 PlayerPrefs.DeleteKey(filePath);
304 PlayerPrefs.DeleteKey ( filePath );
317 #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
320 #if UNITY_WSA || UNITY_WINRT
323 DirectoryInfo info =
new DirectoryInfo(dirPath);
324 FileInfo[] files = info.GetFiles();
325 for (
int i = 0; i < files.Length; i++)
329 DirectoryInfo[] dirs = info.GetDirectories();
330 for (
int i = 0; i < dirs.Length; i++)
332 dirs[i].Delete(
true);
338 PlayerPrefs.DeleteAll();
341 PlayerPrefs.DeleteAll ();
A class to save almost any kind of data on almost all devices (not heavily tested).
static string GetFilePath(string filename)
static void Delete(string filename)
Delete the specified identifier and path.
static string saveDataPath
static bool Exists(string filename)
Checks whether the specified identifier exists or not.
static void Save< T >(T objectToSave, string filename, string encryptionPassword=null, Encoding encoding=null)
Saves data using the identifier.
static void DeleteAll()
Deletes all.
static T Load< T >(string filename, T defaultValue, string encryptionPassword=null, Encoding encoding=null, bool supressFileNotFoundWarning=false)
Loads data using identifier.
static void OpenSavedDataFolder()
Simple Encoder used by the SaveData class.
string Decode(string input, string password)
Decode the specified input with password.
string Encode(string input, string password)
Encode the specified input with password.
Json Serializer used by the SaveData class.
A collection of useful methods that did not fit into any of the other sections of the asset.
static bool IsIOSupported()
Checks if the IO is supported on current platform or not.