- Home /
Question by
kyozdemirr · Aug 25, 2020 at 08:05 AM ·
texture2dscreenshotblack
Screenshots are black
Hi everyone! I found a script that takes screenshot of a given rect but all of my screenshots are black. Why is that?
public class ScreenshotTaker : MonoBehaviour
{
private int scrShotCount = 0;
private Texture2D texture1, texture2;
private int mWidth, mHeight;
private Camera mCamera;
// Start is called before the first frame update
void Start()
{
mCamera = GetComponent<Camera>();
mWidth = mCamera.pixelWidth;
mHeight = mCamera.pixelHeight;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("ScreenshotTaker");
if (scrShotCount == 0)
{
texture1 = RTImage();
scrShotCount++;
}
else if (scrShotCount == 1)
{
texture2 = RTImage();
scrShotCount = 0;
}
}
}
private Texture2D RTImage()
{
Rect rect = new Rect(0, 0, mWidth, mHeight);
RenderTexture renderTexture = new RenderTexture(mWidth, mHeight, 24);
Texture2D screenShot = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGBA32, false);
mCamera.targetTexture = renderTexture;
//mCamera.Render();
RenderTexture.active = renderTexture;
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
byte[] byteArray = screenShot.EncodeToPNG();
System.IO.File.WriteAllBytes(Application.dataPath + "/CameraScreenshot.png", byteArray);
mCamera.targetTexture = null;
RenderTexture.active = null;
Destroy(renderTexture);
return screenShot;
}
I have a second camera on my scene and that script is attached to that. Is it about resolution or anything else?
Comment
Your answer

Follow this Question
Related Questions
Texture2D.EncodeToPng() saves gray PNG, all texture data missing 1 Answer
Texture2D saves as PNG correctly but doesn't display properly in RawImage 1 Answer
Texture.Apply() Crashes at Resolutions under 1920x1080 1 Answer
Using Screenshot as Texture 4 Answers
[CLOSED]How to take a screenshot and apply it as a texture to a sprite? 1 Answer