Quickly capture a screenshot in Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using UnityEngine; | |
public static class ScreenshotCapture | |
{ | |
// Saves the screenshot to desktop | |
public static void Capture() | |
{ | |
string saveDirectory = Environment.GetFolderPath( Environment.SpecialFolder.DesktopDirectory ); | |
int fileIndex = 0; | |
string path; | |
do | |
{ | |
path = Path.Combine( saveDirectory, string.Format( "Screenshot {0}.jpeg", ++fileIndex ) ); | |
} while( File.Exists( path ) ); | |
Capture( path ); | |
} | |
public static void Capture( string path ) | |
{ | |
Camera camera = Camera.main; | |
RenderTexture temp = RenderTexture.active; | |
RenderTexture temp2 = camera.targetTexture; | |
RenderTexture renderTex = RenderTexture.GetTemporary( Screen.width, Screen.height, 24 ); | |
Texture2D screenshot = null; | |
try | |
{ | |
RenderTexture.active = renderTex; | |
camera.targetTexture = renderTex; | |
camera.Render(); | |
screenshot = new Texture2D( renderTex.width, renderTex.height, TextureFormat.RGB24, false ); | |
screenshot.ReadPixels( new Rect( 0, 0, renderTex.width, renderTex.height ), 0, 0, false ); | |
screenshot.Apply( false, false ); | |
File.WriteAllBytes( path, screenshot.EncodeToJPG() ); | |
} | |
finally | |
{ | |
camera.targetTexture = temp2; | |
RenderTexture.active = temp; | |
RenderTexture.ReleaseTemporary( renderTex ); | |
if( screenshot != null ) | |
UnityEngine.Object.DestroyImmediate( screenshot ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment