Firstly, thanks Unity Answers community!
A synopsis of the situation: I'm trying to add content to a scene at runtime, send a secondary camera out to snap a single image of the content, and use that image as a texture (a thumbnail image rendered by the main camera).
I'm using currently using RenderTexture -->ReadPixels -->EncodeToPng --> load WWW (the PNG file) which works, but it creates a new file for each screen grab. Is there a way I can internally use the ReadPixels to create a snapshot Texture2D from the RenderTexture without writing an external PNG?
I apologize for this haggard pastiche of code (it's been pasted together from other Unity Answers):
I'm looking for a way to texture the newThumbnail with the "screenShot" Texture2D without having to EncodeToPNG(). Is this possible? Thank you for any feedback!
Why not just avoid it all together? Can't you just use the RT as the texture of the thumbnail? If not, you could use ReadPixels from the RT and SetPixels to another 2D texture which is your thumbnail, if you needed to do some processing in between or something. (Note ReadPixels32/SetPixels32 much faster).
answered Mar 14 '12 at 11:37 PM
Thanks Dave. The reason I can't use the RenderTexture directly as the mainTexture of the thumbnail plane is that the camera will continuously update the RenderTexture (unless I set it inactive), so I would need a dedicated camera for each thumbnail. I was looking for a way to create ten thumbnails without creating ten cameras, or ten PNG files, or ten of anything except what is absolutely necessary. Your response sent me to the GetPixels32/SetPixels32 documentation, of which there is very little! Below is my script reformulation. I still have a few unanswered questions related to this practice, though. The summary:
That this works seems miraculous to me. How does the thumbShot.ReadPixels know that the reading refers to the thumbnailCamera? Is it because "rt" is the active RenderTexture?
Also, I'm wondering if anyone can point out any glaring problems regarding memory management with the script below, in light of the fact that there may be hundreds of these thumbnails created in a scene.