Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created September 10, 2024 07:29
Show Gist options
  • Save unitycoder/35051efd864c07a63cc2d0b81873f49a to your computer and use it in GitHub Desktop.
Save unitycoder/35051efd864c07a63cc2d0b81873f49a to your computer and use it in GitHub Desktop.
Take Screenshot from Specific Display1 - n (camera)
// https://discussions.unity.com/t/multiple-camera-screenshots/481847/13
public void Screenshot()
{
foreach (GameObject go in GameObject.FindGameObjectsWithTag("picCam"))
{
Camera cam = go.GetComponent<Camera>();
resWidth = cam.pixelWidth;
resHeight = cam.pixelHeight;
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
cam.targetTexture = rt;
cam.Render();
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
RenderTexture.active = rt;
screenShot.ReadPixels(cam.pixelRect, 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidth, resHeight);
System.IO.File.WriteAllBytes(filename, bytes);
cam.targetTexture = null;
RenderTexture.active = null;
rt.Release();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment