Contributors: Jonathan Carter
Documentation Valid for Version: 1.0.1
Last Updated: 31/08/2020
The package has four folders:
Editor/Carter Games/Save Manager: SaveDataEditor.cs
Resourses/Carter Games/Save Manager: IconSM.png
Scripts/Carter Games/Save Manager: SaveManager.cs, SaveData.cs & ExtraSaveData.cs + /Example: ExampleClass.cs & SaveManagerExample.cs
Readme: Text file that links to here
This SaveManager supports the all build versions listed below, Note WebGL support is planned in a future update:
When you first install the package you will notice that the SaveData.cs class is empty, this is intentional as we don't knwo what you want to save. You can add values to save in two ways.
1) By using the SaveData Editor window provided in the asset package
2) By writing in the fields yourself into the class.
Note, not all values in Unity as saveable, or serializable as it is known. A list of that can be saved with this asset is listed below, you can refer to the official documentation for other types that can be serialized if you add them manually:
This asset officially supports:
- Vector2 (using SaveVector2 Class)
- Vector3 (using SaveVector3 Class)
- Vector4 (using SaveVector4 Class)
- Color (using SaveColor Class)
- Custom Classes
See more on this here: https://docs.unity3d.com/Manual/script-Serialization.html#FieldSerliaized2
Unsure how the asset works? before going further we recommend you open the example scene and give it a go to see that asset in action. Note this will not ework if you have edited the SaveData.cs class in anyway. The scene has a prompt at the top so you can get it back up if you want to try it out.
The Editor Window
For ease of use, the Save Manager comes with its own editor window which is designed to help new users setup the SaveData.cs class without needing to write it themselves. The editor has 3 tabs, "Create New SaveData", "Edit Existing SaveData", "About Asset".
Create New SaveData
This tab gives you the tools to create a new SaveData class, it is best to use this when you are settings up the class for the first time or need to clear the class.
When you first open the tab you will see this screen. Press the "+ Add Field" to start creating the new file.
The following will be generated, each grouping of these options is one variable. From here you can select what type the variable should be from the yellow dropdown list. Set whether or not the type is an Array or List as well as set the name for the variable.
If you are using a class value you will also be promoted to enter the class name into an additional field. Please make sure you enter a class name and that it is spelt correctly! This field is CaSe SeNsItIvE.
Once you are happy with the fields, press the "Generate New SaveData Class" button to generate the SaveData file. Note the editor will reload at this point.
Edit Exsisting SaveData
If you have already generated a SaveData.cs you may use this window to edit the class, add additional fields and remove unnecessary fields. There are a couple of different buttons compared to the creation tab. When you first open this tab there won't be anything below these buttons. Press the "Refresh File" button the load the values currently in the SaveData.cs class. Then make the changes you wish and press the "Save Changes" button to update the SaveData.cs class with the changes you've made.
The about asset tab provides some basic information about the asset. These include the version of the asset you are using, the release date of the version you are using as well as links to this documentation and our community discord where we provide asset support.
All of this up until now has been to create the class that is used in the manager. This section will go over how to use the manager.
This manager is a static manager, so there is no need for a reference to be made to the class for it to be used. Instead all you need to do is call the SaveManager class followed by the method you wish to call. To save the game you will need to pass through an instance of the SaveData class holding all the value you wish to save. It is best that you have this on a Game Manager or something similar that you can access when needed. You may also load the game before making the changes needed and saving it again, though this is less efficient. We leave this part up to you as everyone does this differently based on the product they are working on.
Save Manager Methods:
Saves the game using the provided SaveData instance overwriting the previous data saved.
Loads the game if the game has been saved before, if not there will be a message in the console.
You can access all the values you have placed in the SaveData class as you would with any other class, say you have a string for the player name and a float for the player score & a SaveData instance called data, you would access this via the following:
However, you will note that Vectors and Colors have a custom class assigned to them. These types will not look the same in the inspector, but function as you would expect in code. Below is how you would access the Vector2 value from the within a SaveVector2 for an example:
If you run into a problem or get an error and are unsure, feel free to drop me an email at (email@example.com) and I'll do my best to help you out.
My SaveData.cs class has an error after I generated/changed the class from the editor:
Please make sure you didn't do any of the following:
- Set a class field that was not spelt correctly
- Have another script in the same namescape called SaveData.cs
I can't save a Vector or Color:
Please make sure the ExtraSaveTypes.cs class is imported and that you are using the Save variants added in this manager, so "SaveVector2" & "SaveColor".
© Copyright 2019 - 2020 Carter Games - All Rights Reserved