I am not using the PlayerPrefs nor am I using Unity's built in JsonUtility class. I'm using Newtonsofts Json Dot Net library.

Something I personally like to do when working with libraries is write my own class that acts as a "gateway" letting whatever aspects of the library I'll actually be using through, which is what the above screenshot is.

From there, I wrote a class to handle saving and loading data in Json format called JsonDataManager. The Save method converts an object of any type to a Json string and prints that string to the file at (filePath), creating it if it does not already exist. The Load method copies the data saved in a file to a string and then converts said data into a C# object.

Finally, I wrote two new methods into the GlobalVariables class: one to save an array of int variables (by name) to a file and one to create multiple global variables from the data held within a file. Since the player's score, high score, and difficulty are stored as global variables, all I need to do whenever I want to save and/or retrieve the values saved to the user's disk is call the respective method.














1 comment