Nerd For Tech
Published in

Nerd For Tech

Saving an Object in Unity

Objective: Save a object to the user’s system for later use.

An object stored as a .dat file. Not exactly user-friendly…

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!

--

--

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