Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Last active April 21, 2020 11:44
Show Gist options
  • Save openroomxyz/92202fe0fb4a2a444a0122470ad56b3a to your computer and use it in GitHub Desktop.
Save openroomxyz/92202fe0fb4a2a444a0122470ad56b3a to your computer and use it in GitHub Desktop.
Unity : Take A picture at Run-time?
public static void TakeSnapshot(Camera cam, int resolution, string filename)
{
if (cam.targetTexture == null)
{
cam.targetTexture = new RenderTexture(resolution, resolution, 24);
}
Texture2D snapshoot = new Texture2D(resolution, resolution, TextureFormat.RGB24, false);
cam.Render();
RenderTexture.active = cam.targetTexture;
snapshoot.ReadPixels(new Rect(0, 0, resolution, resolution), 0, 0);
byte[] bytes = snapshoot.EncodeToPNG();
string path = @"C:\Users\x\Desktop\x\" + filename;
System.IO.File.WriteAllBytes(path, bytes);
}
//NOTES
//Don’t use the main camera, create additional one and remove audio source component!
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment