Saving & Loading Data in Unity

my test app is getting down to brass tacks, in this and future articles we’ll be covering data persistence i.e., saving and retrieving the data we’re going to insert through the app.

In this first part, we will prepare the data to be uploaded, in the not too distant future, to our AWS server.
During the development of the app, we saw that when switching between screens some data needed to be persistent like the case number. And all the data we were filling out would go into the final overview panel.

To accomplish this we need a space where we can insert the data we are collecting, a method to send it, and one to recall it when the panel passes to the next one.

Regarding the space, I have built a class that I called “Case” where I insert all the data that I will save. At the head of this class, I will use the label “System.Serializable” so that it can be visible in the Inspector when it is declared public in my UIManager script.

To pass the parameters to the next panel I need two methods, one comes from my ‘IPanel’ interface that allows me to update my case object every time I go on, the other is the OnEnable() method that loads the data directly by calling the UIManager and populates its fields when the next panel is activated.

So for each of my panels in the OnEnable method, I load from my activeCase the information I need like the caseID, and in the ProcessInfo I send the data to the activeCase in the UIManager.

All very nice and very simple when it comes to numeric values and strings, the situation changes when one of the values to be saved is an image, more specifically a texture.
In this case, we have to convert the image into an array of bytes.

The conversion from image to a byte array is done with the EncodeToPNG() method which is a method of the Texture2D class.

Reconstructing the image requires a few more steps but nothing that difficult. We have to create a new Texture2D and use the LoadImage() method that takes as an argument just an array of bytes.

Now all our data are ready, the next step is to prepare our server to store them.

project manager by day, unity developer by night