- Home /
Is there any way to execute ReadPixels (for capturing a screenshot) but on a camera that is currently not displaying anything on screen?
That might sound like a weird question but here's what I'm trying to do. Basically I want to take a screen shot of the current pose of a character and use that for an icon in a list of saved presets. But I can't simply take a screen shot of what's actually displaying on screen because the lighting isn't very good and there's menu prompts in the way.
I'm looking for a sneaky way to take the character, warp it away outside of the view temporarily into a better lit environment and take the screenshot there. Any ideas how to do this? ReadPixels appears to only work with what is being displayed on screen.
Answer by skullthug · Dec 11, 2012 at 04:35 AM
The answer is basically no, not without UnityPro and RenderToTexture. Thanks REDDIT http://www.reddit.com/r/Unity3D/comments/14gxyf/is_there_any_way_to_execute_readpixels_for/
Answer by hardsetting_ · Sep 20, 2013 at 08:00 PM
Actually yes, I've been able to do so. The solution is a bit long but it's actually pretty simple.
First of all you have to place on the scene a new Camera ( let's call it GrabCamera ) with the desired aspect ratio and such; what is shown in this camera is what will be shown in the screenshot.
To do this, you have to create a new Unity Layer: On the top bar click Edit->Project Settings->Tags and then double click on the blank space to the right of an unused Layer to name it ( let's call it GrabLayer ); objects you want to see will be in this layer.
Then you'll have to specify the layer culling mask in the GrabCamera, setting it to GrabLayer.
Now you have to add the GrabLayer layer to the objects you want to see in the screenshot, by selecting them and pressing the layer drop-down menu on the upper right.
The last step is to create a specific script to attach to the GrabCamera so that it does the grabbing stuff at the right moment. Here's what I used for an ortographic camera:
public class CameraGrabScript : MonoBehaviour
{
public bool grab = true;
public Texture2D grabTexture;
public Vector2 size = new Vector2( 400.0f, 400.0f );
private void Start()
{
this.camera.pixelRect = new Rect( 0.0f, 0.0f, this.size.x, this.size.y );
this.grabTexture = new Texture2D( (int)this.size.x, (int)this.size.y );
}
private void OnPostRender()
{
if( this.grab )
{
this.grabTexture.ReadPixels( new Rect( 0, 0, this.size.x, this.size.y ), 0, 0 );
this.grabTexture.Apply();
this.grab = false;
}
}
}
Basically in the Start function the camera values are set depending on the part of screen I wanted to capture ( not necessary if you've already set the position correctly ). In the OnPostRender function, right after the GrabCamera as finished rendering, if the grab flag is set to true the camera does the grabbing.
The trick here is that the grabbing occurs right after the GrabCamera has finished rendering and the screen hasn't been overwritten by the MainCamera yet.
For this to work correctly is important that the MainCamera's depth is larger that the GrabCamera's so that the former is rendered last and overwrites completely the latter's rendering.
If instead of wanting just some stuff to get screenshot, you want something to be excluded from the grab you have to set the GrabCamera's culling mask accordingly ( Everything but the Excluding layer ).
Your answer
Follow this Question
Related Questions
capture a screen shot and show in the album folder 1 Answer
Can you change ScreenShotCapture Camera? 1 Answer
What is the bottleneck in this iphone code? 3 Answers
Texture2D.ReadPixels and not lose image quality 1 Answer
Using Texture2D.GetPixels() to take a screenshot and then show it on an Image - iOS problems. 1 Answer