Saving an Object in Unity
Objective: Save a object to the user’s system for later use.
The object we’d like to save is a case file. It is made up of a few strings and a photo converted into an array of bytes. (See https://medium.com/nerd-for-tech/encoding-decoding-a-picture-in-unity-82af70b36853 to learn how to convert a photo to bytes.)
When the user hits the submit button, we’ll save the info they’ve inputted. First we’ll create a new instance of a Case and copy the user’s input to it.
Then we’ll save the case as a file by opening a new BinaryFormatter. We’ll assign the file name and the path where the file will be stored, then append it all with the .dat file extension.
Next we’ll open a FileStream to create the file on the path. Then we’ll serialize the object and save it. Finally, we’ll close the FileStream and log the filepath.
If we follow the path, we can see our file successfully saved!
In the next article, we’ll learn how to upload this file to Amazon Web Services (AWS) so the data can be accessed from almost anywhere!