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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a Debt Collector App — Part 2

Why open-source contribution have become more easier in 2020?

NumPy CheatSheets

How to Review Technical Documentation

Parallel Structure in Technical Writing

How to Deploy Website With Cloudflare Workers And Azure DevOps CI/CD

A Day in the Life: Maria Gutierrez (Director of Engineering, Intercom London)

How to get a list of every “thing” in the Universe (Exporting articles from Wikipedia by Category)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrea Alicino

Andrea Alicino

project manager by day, unity developer by night

More from Medium

Platformer: Simple Movement & Jump

Did you just hit me! — Hitbox attack system in Unity2D

Practically understanding Graphics.Blit() in Unity

Adding a player ammo indicator to the UI