Hello, I am working on a game with a camera that can take and save pictures. A few tutorials I saw pointed me towards System.IO.File.WriteAllBytes and byte arrays as a way to save the images I take at runtime, and this works just fine, but there are 2 things I’m trying to do that I can’t figure out
1 I want to be able to store the pictures I take into a list that doesn’t get erased when I stop playing. The best way I thought to do this was using a scriptable object with a Texture2D list which can store all images for easy access. This doesn’t seem to work though, when taking more than one picture I get a TypeMismatch error on the list in the inspector, and when I stop playing it says all textures are missing, despite the fact they are present in my folder window. I suspect this is because I’m trying to add the temporary texture2d I create in script to make the final png rather than the actual png. I’m not sure how to to keep a reference after using WriteAllBytes, and I’d like to know if this is possible. Any other methods of storing all my images would also be helpful.
2 I want to be able to set the texture importer settings when I take the picture. I tried using OnPreprocessTexture(), but this seems to apply to my entire project, which isn’t what I want. I also tried using presets, but I’m not sure how to apply it to the final png since I’m not sure how to reference it after creating it as I mentioned in point 1.
public void SaveCameraView()
{
//Render image from camera view
RenderTexture screenTexture = new RenderTexture(_pictureLength, _pictureHeight, 32);
RenderCamera.targetTexture = screenTexture;
RenderTexture.active = screenTexture;
RenderCamera.Render();
//Apply rendered image to texture
Texture2D renderedTexture = new Texture2D(_pictureLength, _pictureHeight);
renderedTexture.ReadPixels(new Rect(0, 0, _pictureLength, _pictureHeight), 0, 0);
renderedTexture.Apply();
RenderTexture.active = null;
PictureCompressionSettings.ApplyTo(renderedTexture); //Attempt to apply presets
//Save image to PNG file
byte[] byteArray = renderedTexture.EncodeToPNG();
string savePath = $"/Captures/{CaptureVault.name}/cameracapture{CaptureVault.Captures.Count}.png";
System.IO.File.WriteAllBytes(Application.dataPath + savePath, byteArray);
AssetDatabase.Refresh();
//Add image to gallery scriptable object
CaptureVault.Captures.Add(renderedTexture);
}