Saving & Loading Texture Files In Unity

Saving and loading the image files is quite tricky. As we cannot store the photo taken as a texture, instead of storing the photoTaken as a texture type, we need to store it in a byte array.

Update on Case Class

Now, we need to update the scripts that includes the photoTaken variable in a way that they will rebuild the photo from the byte array to be used as an image.

First, we need to convert the texture to Texture2D. Then Convert it to bytes.

TakePhotoPanel Script

Rather than assigning the photoTaken.texture, we assign the imgData. Now, the photo taken by the user is stored within the UIManager. However, we will need to reverse the operation in order to call the photo at the Overview Panel. Luckily, Unity has a ready method for this operation : ImageConversion.LoadImage. For more information about it, you can click here to reach the manual.

However, in order to call the photo taken from the device, we will need to access the path that the device stores the photo taken.

In the TakePhotoPanel script, we added a private string imgPath variable. TakePicture method has been updated as below :

From TakePhotoPanel Script

And the processInfo (that we call once we hit the next button) has been updated as below:

From TakePhotoPanel Script

On the ProcessInfo method above, we null check the imgdata byte array as it returns a null exception error if we try to build and test it within the editor. Necessary null check code has been added for this reason.

To show up the image taken to the user, we will need to call it from the overview panel script. It has been updated as below:

Unity Developer