StorageManager - Loading & Saving Data in Unity
Storing simple (or complex) data is an issue in any game. You will need to save user settings. You may also need to store game progress somewhere and load it again at a later time. Also, you may want to cache data briefly while playing and then access it again later in the game. Awesome Accessories offers a number of tools for this.
FilesService
First of all I would like to introduce the FilesService. It provides you with a simple way to store text-based data on the user's device, load it, move files and more. By default Application.persistentDataPath is used as the base for all requests. This ensures that you use a writable folder.
var filesService = new FilesService();
However, you can also define your own basePath.
var filesService = new FilesService(myAwesomeBasePath);
You can use the service to load text data:
string contents = filesService.LoadText("myAwesomeFile.csv");
But you can also load raw data instead:
byte[] contents = filesService.LoadBytes("myAwesomeFile.jpg");
Needless to say, you can save data:
filesService.Save("my/Awesome/File.json", "the file contents");
You can delete files:
filesService.Delete("my/Awesome/File.json");
To check if a specific file exists, you can use this function:
filesService.Exists("my/Awesome/File.json");
You can also copy and move files:
filesService.Copy("my/Awesome/SourceFile.json", "my/Awesome/TargetFile.json");
filesService.Move("my/Awesome/SourceFile.json", "my/Awesome/TargetFile.json");
You can check if a path is a folder:
filesService.IsDirectory("my/Awesome/Directory");
You can create and delete folders:
filesService.CreateDirectory("my/Awesome/Directory");
filesService.DeleteDirectory("my/Awesome/Directory");
You can also get a list of folders:
filesService.GetDirectoriese("my/Awesome/Directory")
Or, alternatively, a list of files:
filesService.GetFilese("my/Awesome/Directory", "*.json");
So you see that with the FilesService you have a tool that should make saving and loading files easier. But strictly speaking this service is not the core of this module. Instead, this module provides you with various StorageManagers.
The different Storage Services
This module provides various storage services:
JsonStorageService This service can be used to store data in a JSON file.
SimpleStorageService This service can be used to store data using Unity's PlayerPrefs.
RuntimeStorageService This service can be used to store data during runtime.
All services are based on the same interface and provide the same methods. So you can switch between the services as you like.
Set(string key, int value) This method can be used to store an integer value.
Set(string key, float value) This method can be used to store a float value.
Set(string key, string value) This method can be used to store a string value.
Set(string key, bool value) This method can be used to store a boolean value.
Set(string key, Vector2 value) This method can be used to store a Vector2 value.
Set(string key, Vector3 value) This method can be used to store a Vector3 value.
Set(string key, Vector4 value) This method can be used to store a Vector4 value.
Set(string key, Color value) This method can be used to store a Color value.
Set(string key, Color32 value) This method can be used to store a Color32 value.
Set(string key, Quaternion value) This method can be used to store a Quaternion value.
Get(string key, int defaultValue) This method can be used to get an integer value.
Get(string key, float defaultValue) This method can be used to get a float value.
Get(string key, string defaultValue) This method can be used to get a string value.
Get(string key, bool defaultValue) This method can be used to get a boolean value.
Get(string key, Vector2 defaultValue) This method can be used to get a Vector2 value.
Get(string key, Vector3 defaultValue) This method can be used to get a Vector3 value.
Get(string key, Vector4 defaultValue) This method can be used to get a Vector4 value.
Get(string key, Color defaultValue) This method can be used to get a Color value.
Get(string key, Color32 defaultValue) This method can be used to get a Color32 value.
Get(string key, Quaternion defaultValue) This method can be used to get a Quaternion value.
Clear() This method can be used to clear all stored values.
Please note that you must always use a default value for the Get methods as well. This value must have the same type as the expected return value.
When do I use which service (or manager)?
Of course, each of these services is also available as a StorageManager. As always, the managers have basically the same names as the respective services:
JsonStorageManager
SimpleStorageManager
RuntimeStorageManager
For example, you can use SimpleStorageManager to store a string:
SimpleStorageManager.service?.Set("username", "Alfred Awesome");
And this is how you can then retrieve this string:
var username = SimpleStorageManager.service?.Get("username", "");
if (username == "") {
username = "Anonymous";
}
As a general guideline for using the services or managers, you can remember the following:
If you only want to store something for a short time and it is not necessary that the data is available after the end of the game, use the RuntimeStorageManager.
If you want to store data for a longer period of time, but the amount of data is not too large, use the SimpleStorageManager. This applies for example to user settings.
If you want to store data for a longer time and the amount of data could be bigger or you just want to be on the safe side, then use the JsonStorageManager.
Your Feedback is important!
What do you think of the services presented here? Is there anything missing or does something not work as expected? As always, I'm happy to listen to your feedback. Let me know what you think about this module. You can use the comment section below the article for this. You can also find other ways to contact me here. If you found a bug or want an enhancement, please create an issue in the GitHub repository. Further documentation can be found in the README of the corresponding module.
There are no comments yet.