- Home /
When rendering to a texture, how to prevent camera from also rendering to screen?
Hi,
I've been working on a script that generates a thumbnail of a prefab (not used inside the editor) and it's working fine except that for a moment my thumbnail appears on-screen (bottom left). I couldn't find anything online about this so I'm asking here. Here's the code I'm using :
IEnumerator RenderPrefab(GameObject prefab, int width, int height, Action<Texture2D> callback)
{
var cameraContainer = prefab.GetChild("PictureCamera");
var camera = cameraContainer.GetComponent<Camera>();
yield return new WaitForEndOfFrame();
// Camera Container is inactive initially, so we activate it now
cameraContainer.SetActive(true);
// Backup active render texture
var originalActive = RenderTexture.active;
// RenderTexture setup
var renderTexture = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);
RenderTexture.active = renderTexture;
camera.targetTexture = renderTexture;
// Actual rendering
camera.Render();
// Copy RenderTexture to Texture2D
var output = new Texture2D(width, height, TextureFormat.ARGB32, false);
output.ReadPixels(new Rect(0, 0, width, height), 0, 0);
output.Apply(false);
// Revert active render texture
RenderTexture.active = originalActive;
// Cleanup
Object.Destroy(renderTexture);
cameraContainer.SetActive(false);
Destroy(prefab);
// Done
callback(output);
}
As soon as you add a RenderTexture to a camera.TargetTexture, the camera render does not show anymore on to screen, so i guess you should try to add your RenderTexture before you enable your camera. This way you should not see the render on the screen.
Hope it helps.
You don't need to enable your camera at all. Disable the camera. When you call Render manually it doesn't need to be enabled.
Your answer
Follow this Question
Related Questions
Is RenderTexture a Pro only feature? 3 Answers
Multiple camera One rendertexture (panoramic view) Please Help Very Important!! 1 Answer
RenderTexture not working when in Editor 1 Answer
Irregular shape window to a different background world? 0 Answers
[Pro] How do I make programmatic Render-to-Texture work? 1 Answer