Service Application: AWS Part 1
Objective: Saving and Loading Data
Now that the application can take all the required information in and present it in the Overview Panel, we need to be able to hit save and save it for later use. There are a few ways to save data such as Player Prefs but that isn’t the best option here and specially if there is sensitive information. We also want to be able to retrieve the data later and maybe even from another device. This is where AWS comes in but we’ll come back to that. For now we need to save the data. A common form of saving would be to use JSON but for this project we will use serialization to save the data in a binary file.
We start by creating a SubmitCase in the UIManager that will be called when the Save Button is pressed. Next we create a new case that will be saved to AWS. we populate the new case using the active case values. A binary formatter is then used to change the case into a text file.
We create a BinaryFormatter(bf) which uses the namespace System.Runtime.Serialization.Formatters.Binary then a FileStream(fs) which uses the System.IO namespace to create a file.
bf then uses fs to serialize the case before closing fs.
If we run this we end up with an error as Unity can’t serialize the image so some refactoring is needed. First we change the photo variable in the case from a RawImage to a Byte Array. Next we go back to the TakePhotoPanel script and create public Byte Array for the image data. We then make the texture readable after the image/photo has been taken and set the img data byte array using texture.EncodeToPNG. We can then set the activeCase.photoTaken value that is now a byte array instead of a RawImage to the encoded texture.
Finally inside the OverviewPanel script before we can set the overview panel image we now need to decode the Byte Array of the image so we can set the PhotoTaken image UI slot to the image. We do this by creating a new Texture2D and loading the img data into the texture before setting the PhotoTaken texture.
We can see from the images below taken from mobile and an image of my company while working, the app works as and the casefile indeed gets saved as expected.
We can now move to uploading the file(.dat) to AWS.
If no image is taken we get some slight issues but the app still works but that’s not what we want. To fix this. a public Texture2D was added and set in the inspector using an image saying no image. Then in the ProcessInfo method we check if the Byte Array is not null or empty meaning we took a photo before setting the activeCase.photoTaken Byte Array and on the else meaning empty we set the activeCase.photoTaken to the encoded noImg Byte Array.
This produces the following OverviewPanel with an image saying no image.