In the insurance app, it is time to upload the data to the server so that we will have the opportunity to follow it up later on. On the overview panel, we want the data we have filled to be uploaded to the server once we click on the “Submit” button. The idea is pretty simple, it will take save the object that is constructed by using the Case class.

To save the data that has been filled in, serialization will be used. Actually, we are using it every time we use the SerializeField or System.Serializable attributes. Here’s the definition of Serialization taken from Microsoft:

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

Thus, by using [System.Serializable] attribute in the Case class, we simply tell that this class can be turned into bytes in order to be stored.

The active case that the user fills in is being stored in the activeCase game object. To store the data with the information provided, we create a new game object of type Case.

To serialize/deserialize a object, BinaryFormatter Class is used. To access the library that allows us to use the BinaryFormatter class, we need to add the following to the namespaces in the UIManager script.

using System.Runtime.Serialization.Formatters.Binary;

Alternatively, you can just type BinaryFormatter and right click to the fix suggestions which will add the namespace automatically.

Then, we need to create a file to save the data on. Filestream class is used for this purpose (click here for more information). It will also require System.IO to be added as a namespace.

I can hear you asking “ how do we decide on the file location for the save file?”. By default, Unity provides us a default file location as a suggestion with Application.persistentDataPath. Here’s the information taken from Unity website:

Contains the path to a persistent data directory.This value is a directory path where you can store data that you want to be kept between runs. When you publish on iOS and Android, persistentDataPath points to a public directory on the device. Files in this location are not erased by app updates. The files can still be erased by users directly.

For more information about it, you can click here.

So once we click the Submit button, we will be calling the SubmitButton method from the UIManager.

As you can see above, we have created a new binaryformatter to serialize a data. Then, by using FileStream, we have opened a stream. We serialized the game object (awsCase) into the open stream (file). And lastly, we have closed the stream.

If we try to save it, it gives out an error stating that we cannot turn a texture (the format that we store the image) into a binary file. If you do not have any texture variable to be saved, that is all you need for saving your data. For saving texture, please check my next article.

To test if your saving system actually works, you can simply add the following to the bottom in order to find the destination where you are saving the data.

Debug.Log(“Application Data Path “ + Application.persistentDataPath);

After uploading the data to AWS, we will be able to load the saved data from the server. I will be posting how to upload the files and how to download them on my upcoming articles.

Unity Developer