Saving and Loading from Resources
Resources allows you to include and load files which will be included at runtime, including Easy Save 2 save files.
Saving to Resources
Easy Save can only save to the Resources folder from inside the Editor as the Resources folder doesn’t exist when your Unity application gets built.
If you want to save data to Resources with the intention of loading later, any files must end in the extension .bytes.
Because ES2.Save accepts an absolute path, we can use Application.dataPath to get the absolute path to our Assets folder, and then add to the end of that the path to our Resources folder.
Alternatively, you can save data to a folder of your choice and then drag it into the Resources folder.
Also note that if you want files to be visible immediately in Unity after creating them, you will need to call AssetDatabase.Refresh(), or refresh the assets from within Unity.
C#
1 2 |
ES2.Save(123, Application.dataPath+"/Resources/myFile.bytes?tag=myInt"); AssetDatabase.Refresh(); |
JS
1 2 |
ES2.Save(123, Application.dataPath+"/Resources/myFile.bytes?tag=myInt"); AssetDatabase.Refresh(); |
Loading from Resources
To Load from Resources, you must set your Save Location to Resources. You can do this using Parameters, or using an ES2Settings object.
Note that unlike saving, we don’t have to provide the path to the Resources folder.
C#
1 2 3 4 5 6 7 |
// Load from Resources using the savelocation parameter int myInt = ES2.Load<int>("myFile.bytes?tag=myInt&savelocation=resources"); // Load from Resources using ES2Settings ES2Settings settings = new ES2Settings(); settings.saveLocation = ES2Settings.SaveLocation.Resources; int myInt = ES2.Load<int>("myFile.bytes?tag=myInt", settings); |
JS
1 2 3 4 5 6 7 |
// Load from Resources using the savelocation parameter var myInt = ES2.Load.<int>("myFile.bytes?tag=myInt&savelocation=resources"); // Load from Resources using ES2Settings var settings = new ES2Settings(); settings.saveLocation = ES2Settings.SaveLocation.Resources; var myInt = ES2.Load<int>("myFile.bytes?tag=myInt", settings); |