- Home /
How to capture pixels with .ReadPixels through an editor script?
Hi,
I'm trying to make an editor extension that manipulates screenshots taken via an editor window. Application.CaptureScreenshot isn't really appropriate for me, so I'm trying to use Texture2D.ReadPixels and EncodeToPNG, however when I use the example code from the script reference, it outputs an image of the editor window, not the viewport (as seen below).
The function I'm using:
public void TakeHiResShot() {
EditorWindow.FocusWindowIfItsOpen<SceneView>();
Texture2D tex = new Texture2D (resWidth, resHeight, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels (new Rect(0, 0, resWidth, resHeight), 0, 0);
tex.Apply ();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
}
As you can see I've tried to shift the focus of the window before taking the screenshot but that doesn't appear to affect ReadPixels in any way. The script reference mentions something called /source/ but I can't find any other references to this.
Thanks in advance!
I'm having exactly the same issue. Any resolutions to this yet? $$anonymous$$y readpixels will only grab an image from the current window and not the viewport I want to.
Ins$$anonymous$$d of focusing SceneView, can you close the focusing window?
Your answer
![](https://koobas.hobune.stream/wayback/20220613080817im_/https://answers.unity.com/themes/thub/images/avi.jpg)