Skip to content

Instantly share code, notes, and snippets.

@ezirmusitua
Created May 17, 2019 02:49
Show Gist options
  • Save ezirmusitua/a05bbe92639f69f060bac2117ca49021 to your computer and use it in GitHub Desktop.
Save ezirmusitua/a05bbe92639f69f060bac2117ca49021 to your computer and use it in GitHub Desktop.
[Take screenshot] Take full/camera screenshot #unity
// 1. Take full screenshot
//// full screen
//// not support single camera
//// bad efficiency
void CaptureFullScreen() {
Application.CaptureScreenshot("Screenshot.png", 0);
}
Texture2D CaptureFullScreen() {
return Application.CaptureScreenshotAsTexture();
}
// 2. Take with texture2D
//// support rect
//// not support camera
Texture2D CaptureScreenshotWithTexture2D(Rect rect) {
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
return screenShot;
}
// 3. Take cameras
//// support rect
//// support camera but screen
Texture2D CaptureCamerasScreenshot(List<Camera> cameras, Rect rect) {
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
cameras.ForEach(camera => {
camera.targetTexture = rt;
camera.Render();
})
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
screenShot.Apply();
cameras.ForEach(camera => {
camera.targetTexture = null;
});
RenderTexture.active = null; // JC: added to avoid errors
GameObject.Destroy(rt);
return screenShot;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment