SaveManager
A custom Unity SaveManager that allows easy saving & loading of Variables
com.medallyon.savemanager 
Install via UPM
Add to Unity Package Manager using this URL
https://www.pkglnk.dev/medallyon-savemanager.git README Markdown
Copy this to your project's README.md
## Installation
Add **SaveManager** to your Unity project via Package Manager:
1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/medallyon-savemanager.git
```
[](https://www.pkglnk.dev/pkg/medallyon-savemanager)Dependencies (1)
README
Unity SaveManager
A custom Unity SaveManager that allows easy saving & loading of Variables in any of your Scripts.
Installation
Method A - OpenUPM
- Install the
openupm-cli. - Open a new terminal or cmd in your project root directory.
- Run
openupm install com.medallyon.savemanager. - Go back to Unity and allow it to install the package into your project.
Method B - manifest.json
manifest.json- Open the
manifest.jsonfile found in your project root directory underPackages > manifest.json - Add
"com.medallyon.savemanager": "https://github.com/medallyon/unity-savemanager.git",to thedependenciesobject. - Go back to Unity and allow it to install the package into your project.
Method C - Install via GitHub
- In the Unity Editor, open the Package Manager window (
Window > Package Manager). - Click the ➕ sign in the top-left corner of the Package Manager and select Add package from git URL...
- Insert
https://github.com/medallyon/unity-savemanager.gitand click Add.
Usage
You can use this SaveManager library out-of-the-box as part of your Scripts. For examples, see the Samples folder.
The [Save] Attribute
You can attach the [Save] Attribute to any Field or Property in your Scripts. The SaveManager is able to store and load primitive types (int, string, bool, etc.) without any extra processing.
For non-primitive types, such as your own custom classes, you will be required to implement the ISaveable interface in your desired MonoBehaviours. Read on to find out how to get started with this.
The ISaveable Interface
The ISaveable Interface requires you to add the public void OnRestore(bool isFirstLoad, Dictionary<string, object> data) { } method. The OnRestore method is called on every MonoBehaviour that implements this Interface.
The OnRestore Method
The bool isFirstLoad Parameter
bool isFirstLoad Parameter isFirstLoad denotes whether a Data file was found and loaded. If this evaluates to true, the data parameter will not contain any entries, and it usually means that this is the first time that the game was started. If this evaluates to false, the data parameter will contain entries for all variables that are decorated with the [Save] Attribute in this MonoBehaviour.
The Dictionary<string, object> data Parameter
Dictionary<string, object> data Parameter The data parameter is a Dictionary that maps the name (string) of the Saved Field or Property to its value (object). You will have to cast the value into the desired type. For complex values, you may have to do more processing using methods from the Newtonsoft.Json Assembly, such as JsonConvert.DeserializeObject<T>. More samples will follow illustrating how to cast this data properly.
Accessing Variables and their Values through data
data Since the data parameter is a Dictionary, we can use its indexer to access Variables via their names. Here, the C# nameof keyword comes in handy. See the following example on how to access the saved _playerID variable:
public class MySaveableComponent : MonoBehaviour, ISaveable
{
[Save] private Guid _playerID = Guid.Empty;
// This happens before 'Start' is called
public void OnRestore(bool isFirstLoad, Dictionary<string, object> data)
{
// If the '_playerID' Variable was never saved, use a desired default Value
if (isFirstLoad)
{
_playerID = Guid.NewGuid();
return;
}
// Get the name of the saved Variable as a string ("_playerID")
string variableName = nameof(_playerID);
// Access the value of the saved Variable through the Dictionary's indexer
string variableValue = (string)data[variableName];
// Parse the string Value as a 'Guid' and set the Value for '_playerID'
_playerID = Guid.Parse(variableValue);
}
}
The contents of OnRestore in the above example could also be condensed into a single statement:
_playerID = isFirstLoad ? Guid.NewGuid() : Guid.Parse((string)data[nameof(_playerID)]);
Contributing
Any Contributions are welcome through Pull Requests. More details will follow.
No comments yet. Be the first!