Question about Memory Leaking When saving RenderTexture as PNG
In my app I need to save a render texture as a PNG to disk, and this is working fine, but all tutorials or examples of this say to Destroy the created Texture2D to prevent memory leaking. In my case, however, I need to keep a reference to the created Texture every time I "save" a render texture, and destroying it breaks that functionality.
Would there be any problems if I don't destroy the tex
variable and use it elsewhere? This is NOT being done every frame, only when a button is pressed periodically.
Here's my code:
RenderTexture.active = linkedCamera.targetTexture;
var tex = new Texture2D( targetTexture.width, targetTexture.height);
tex.ReadPixels(new Rect(0, 0, targetTexture.width, targetTexture.height), 0, 0);
tex.Apply();
string imageSavePath = string.Format("{0}image_{1}.{2}",
saveLoad.imageOutputPath,
Utils.GetTimeString(),
"png");
File.WriteAllBytes(imageSavePath, tex.EncodeToPNG());
Destroy(tex);
Your answer
![](https://koobas.hobune.stream/wayback/20220613043733im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
RenderTexture to PNG on Android 0 Answers
Saving .png from render texture results in much darker image 1 Answer
Need help with spy glass to see through objects. 1 Answer
How to capture screen, render to background, and allow both portrait and landscape on mobile device? 0 Answers
How do I add a renderTexture UI element onto my ARKit screen 0 Answers