Skip to content

Commit 0d9de44

Browse files
authored
Merge pull request #14 from coryleach/dev
Get/Load from StreamingAssets
2 parents 4d66aa5 + a2a8166 commit 0d9de44

5 files changed

Lines changed: 86 additions & 31 deletions

File tree

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
<h1 align="center">Gameframe.SaveLoad 👋</h1>
2-
<p>
3-
<img alt="Version" src="https://img.shields.io/badge/version-1.0.7-blue.svg?cacheSeconds=2592000" />
4-
<a href="https://twitter.com/Cory Leach">
5-
<img alt="Twitter: coryleach" src="https://img.shields.io/twitter/follow/coryleach.svg?style=social" target="_blank" />
6-
</a>
1+
<p align="center">
2+
<img align="center" src="https://raw.githubusercontent.com/coryleach/UnityPackages/master/Documentation/GameframeFace.gif" />
73
</p>
4+
<h1 align="center">Gameframe.SaveLoad 👋</h1>
5+
6+
<!-- BADGE-START -<!-- BADGE-END -->
87

98
Serialization helper utility that supports save, load and encryption.
109

@@ -13,15 +12,15 @@ Serialization helper utility that supports save, load and encryption.
1312
#### Using UnityPackageManager (for Unity 2019.3 or later)
1413
Open the package manager window (menu: Window > Package Manager)<br/>
1514
Select "Add package from git URL...", fill in the pop-up with the following link:<br/>
16-
https://github.com/coryleach/UnitySaveLoad.git#1.0.7<br/>
15+
https://github.com/coryleach/UnitySaveLoad.git#1.0.8<br/>
1716

1817
#### Using UnityPackageManager (for Unity 2019.1 or later)
1918

2019
Find the manifest.json file in the Packages folder of your project and edit it to look like this:
2120
```js
2221
{
2322
"dependencies": {
24-
"com.gameframe.saveload": "https://github.com/coryleach/UnitySaveLoad.git#1.0.7",
23+
"com.gameframe.saveload": "https://github.com/coryleach/UnitySaveLoad.git#1.0.8",
2524
...
2625
},
2726
}
@@ -83,13 +82,13 @@ In player settings add the string 'JSON_DOT_NET' to Scripting Define Symbols.
8382

8483
👤 **Cory Leach**
8584

86-
* Twitter: [@coryleach](https://twitter.com/coryleach)
85+
* Mastodon: [@coryleach@mastodon.gamedev.place](https://mastodon.gamedev.place/@coryleach)
8786
* Github: [@coryleach](https://github.com/coryleach)
8887

8988

9089
## Show your support
91-
9290
Give a ⭐️ if this project helped you!
9391

92+
9493
***
9594
_This README was generated with ❤️ by [Gameframe.Packages](https://github.com/coryleach/unitypackages)_

Runtime/SaveLoadManager.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,34 @@ public void Save(object obj, string filename, string folder = null)
7777
/// Gets the list of save files that have been created
7878
/// </summary>
7979
/// <param name="folder">sub folder</param>
80-
/// <param name="extension">include only files with this extension</param>
80+
/// <param name="extension"></param>
81+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
8182
/// <returns>list of file names (excludes the path)</returns>
82-
public string[] GetFiles(string folder = null, string extension = null)
83+
public string[] GetFiles(string folder = null, string extension = null, bool streamingAssets = false)
8384
{
8485
if (string.IsNullOrEmpty(folder))
8586
{
8687
folder = defaultFolder;
8788
}
88-
return SaveLoadUtility.GetSavedFiles(folder,baseFolder, extension);
89+
return SaveLoadUtility.GetSavedFiles(folder,baseFolder, extension, streamingAssets);
90+
}
91+
92+
/// <summary>
93+
/// Gets the list of save files that have been created
94+
/// </summary>
95+
/// <param name="list">list to be populated with file names</param>
96+
/// <param name="folder">sub folder</param>
97+
/// <param name="extension"></param>
98+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
99+
/// <returns>list of file names (excludes the path)</returns>
100+
public void GetFiles(List<string> list, string folder = null, string extension = null, bool streamingAssets = false)
101+
{
102+
if (string.IsNullOrEmpty(folder))
103+
{
104+
folder = defaultFolder;
105+
}
106+
107+
SaveLoadUtility.GetSavedFiles(list, folder,baseFolder, extension, streamingAssets);
89108
}
90109

91110
/// <summary>
@@ -126,11 +145,12 @@ public T Copy<T>(T obj)
126145
/// </summary>
127146
/// <param name="filename">Name of file to load from</param>
128147
/// <param name="folder">Name of folder containing the file</param>
148+
/// <param name="streamingAssets">Load file from streaming assets</param>
129149
/// <typeparam name="T">Type of object to be loaded from file</typeparam>
130150
/// <returns>Instance of object loaded from file</returns>
131-
public T Load<T>(string filename, string folder = null)
151+
public T Load<T>(string filename, string folder = null, bool streamingAssets = false)
132152
{
133-
return (T)Load(typeof(T), filename, folder);
153+
return (T)Load(typeof(T), filename, folder, streamingAssets);
134154
}
135155

136156
/// <summary>
@@ -139,15 +159,16 @@ public T Load<T>(string filename, string folder = null)
139159
/// <param name="type">Type of object to be loaded</param>
140160
/// <param name="filename">Name of file to load object from</param>
141161
/// <param name="folder">Name of folder containing the file to be loaded</param>
162+
/// <param name="streamingAssets">Load file from streaming assets</param>
142163
/// <returns>Instance of object to be loaded. Null if file did not exist.</returns>
143-
public object Load(Type type, string filename, string folder = null)
164+
public object Load(Type type, string filename, string folder = null, bool streamingAssets = false)
144165
{
145166
if (string.IsNullOrEmpty(folder))
146167
{
147168
folder = defaultFolder;
148169
}
149170
var saveLoadMethod = GetSaveLoadMethod(saveMethod);
150-
return SaveLoadUtility.Load(type, saveLoadMethod,filename,folder, baseFolder);
171+
return SaveLoadUtility.Load(type, saveLoadMethod,filename,folder, baseFolder, streamingAssets);
151172
}
152173

153174
/// <summary>

Runtime/SaveLoadUtility.cs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static class SaveLoadUtility
1111
private const string DefaultFolderName = "SaveLoad";
1212
private const string DefaultBaseFolderPath = "GameData";
1313

14-
public static string GetSavePath(string folderName = null, string baseFolderPath = null)
14+
public static string GetSavePath(string folderName = null, string baseFolderPath = null, bool streamingAssets = false)
1515
{
16-
return GetRuntimeSavePath(folderName, baseFolderPath);
16+
return !streamingAssets ? GetRuntimeSavePath(folderName, baseFolderPath) : GetStreamingAssetsSavePath(folderName, baseFolderPath);
1717
}
1818

1919
public static string GetRuntimeSavePath(string folderName = null, string baseFolderPath = null)
@@ -29,7 +29,24 @@ public static string GetRuntimeSavePath(string folderName = null, string baseFol
2929
}
3030

3131
var savePath = $"{Application.persistentDataPath}/{baseFolderPath}/";
32-
savePath = savePath + folderName + "/";
32+
savePath = $"{savePath}{folderName}/";
33+
return savePath;
34+
}
35+
36+
public static string GetStreamingAssetsSavePath(string folderName = null, string baseFolderPath = null)
37+
{
38+
if (string.IsNullOrEmpty(folderName))
39+
{
40+
folderName = DefaultFolderName;
41+
}
42+
43+
if (string.IsNullOrEmpty(baseFolderPath))
44+
{
45+
baseFolderPath = DefaultBaseFolderPath;
46+
}
47+
48+
var savePath = $"{Application.streamingAssetsPath}/{baseFolderPath}/";
49+
savePath = $"{savePath}{folderName}/";
3350
return savePath;
3451
}
3552

@@ -77,10 +94,11 @@ public static void Save(object saveObject, ISerializationMethod serializationMet
7794
/// <param name="filename"></param>
7895
/// <param name="folderName"></param>
7996
/// <param name="baseFolderPath"></param>
97+
/// <param name="streamingAssets"></param>
8098
/// <returns></returns>
81-
public static object Load(System.Type objectType, ISerializationMethod serializationMethod, string filename, string folderName = null, string baseFolderPath = null)
99+
public static object Load(System.Type objectType, ISerializationMethod serializationMethod, string filename, string folderName = null, string baseFolderPath = null, bool streamingAssets = false)
82100
{
83-
var savePath = GetSavePath(folderName, baseFolderPath);
101+
var savePath = GetSavePath(folderName, baseFolderPath, streamingAssets);
84102
var saveFilename = savePath + GetSaveFileName(filename);
85103

86104
object returnObject = null;
@@ -104,11 +122,12 @@ public static object Load(System.Type objectType, ISerializationMethod serializa
104122
/// </summary>
105123
/// <param name="folderName">folder containing the save files</param>
106124
/// <param name="baseFolderPath">base path to the folder</param>
107-
/// <param name="extension">include only files with the specified extension</param>
125+
/// <param name="extension">include only files with this extension</param>
126+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
108127
/// <returns>list of file names</returns>
109-
public static IEnumerable<string> EnumerateSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null)
128+
public static IEnumerable<string> EnumerateSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null, bool streamingAssets = false)
110129
{
111-
var savePath = GetSavePath(folderName,baseFolderPath);
130+
var savePath = GetSavePath(folderName,baseFolderPath,streamingAssets);
112131

113132
//If directory does not exist we're done
114133
if (!Directory.Exists(savePath))
@@ -126,13 +145,29 @@ public static IEnumerable<string> EnumerateSavedFiles(string folderName = null,
126145
/// <summary>
127146
/// Creates an array list of save files in the given folder and path
128147
/// </summary>
129-
/// <param name="folderName">folder containing the save files</param>
130-
/// <param name="baseFolderPath">base path to the folder</param>
148+
/// <param name="folderName"></param>
149+
/// <param name="baseFolderPath"></param>
150+
/// <param name="extension">include only files with this extension</param>
151+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
152+
/// <returns>Array of file names</returns>
153+
public static string[] GetSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null, bool streamingAssets = false)
154+
{
155+
return EnumerateSavedFiles(folderName, baseFolderPath, extension, streamingAssets).ToArray();
156+
}
157+
158+
/// <summary>
159+
/// Populates a given array with a list of save files in the given folder and path
160+
/// </summary>
161+
/// <param name="list">list to be populated with file names</param>
162+
/// <param name="folderName"></param>
163+
/// <param name="baseFolderPath"></param>
131164
/// <param name="extension">include only files with this extension</param>
165+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
132166
/// <returns>Array of file names</returns>
133-
public static string[] GetSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null)
167+
public static void GetSavedFiles(List<string> list, string folderName = null, string baseFolderPath = null, string extension = null, bool streamingAssets = false)
134168
{
135-
return EnumerateSavedFiles(folderName, baseFolderPath, extension).ToArray();
169+
list.Clear();
170+
list.AddRange(EnumerateSavedFiles(folderName, baseFolderPath, extension, streamingAssets));
136171
}
137172

138173
/// <summary>

Tests/Editor/SaveLoadUtilityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void CanGetFiles()
8383
SaveLoadUtility.Save(testSave,serializationMethod,filename,folder);
8484

8585
var files = SaveLoadUtility.GetSavedFiles(folder);
86-
Assert.IsTrue(files.Length == 1);
86+
Assert.IsTrue(files.Length == 1,$"Total Save Files: {files.Length} Expected 1");
8787

8888
//Files should contain a list of names that exactly match the file name used
8989
//omits the path of the file

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.gameframe.saveload",
33
"displayName": "Gameframe.SaveLoad",
4-
"version": "1.0.7",
4+
"version": "1.0.8",
55
"description": "Serialization helper utility that supports save, load and encryption.",
66
"keywords": [],
77
"author": {

0 commit comments

Comments
 (0)