Last active
April 21, 2020 11:44
-
-
Save openroomxyz/92202fe0fb4a2a444a0122470ad56b3a to your computer and use it in GitHub Desktop.
Unity : Take A picture at Run-time?
This file contains hidden or 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
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