Snapshots

From ETC Public Wiki
Jump to: navigation, search

Take a Snapshot or screen capture and save it to disk

Found this code at: http://answers.unity3d.com/questions/22954/how-to-save-a-picture-take-screenshot-from-a-camer.html

   using UnityEngine;
   using System.Collections;
    
   public class HiResScreenShots : MonoBehaviour {
   public int resWidth = 2550;
   public int resHeight = 3300;
    
   private bool takeHiResShot = false;
    
   public static string ScreenShotName(int width, int height) {
   return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png",
   Application.dataPath,
   width, height,
   System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
   }
    
   public void TakeHiResShot() {
   takeHiResShot = true;
   }
    
   void LateUpdate() {
   takeHiResShot |= Input.GetKeyDown("k");
   if (takeHiResShot) {
   RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
   camera.targetTexture = rt;
   Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
   camera.Render();
   RenderTexture.active = rt;
   screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
   camera.targetTexture = null;
   RenderTexture.active = null; // JC: added to avoid errors
   Destroy(rt);
   byte[] bytes = screenShot.EncodeToPNG();
   string filename = ScreenShotName(resWidth, resHeight);
   System.IO.File.WriteAllBytes(filename, bytes);
   Debug.Log(string.Format("Took screenshot to: {0}", filename));
   takeHiResShot = false;
   }
   }
   }


Take a webcam snapshot and save to disk

Found this code here: http://answers.unity3d.com/questions/337530/how-to-save-a-snapshot-of-the-webcamtexture.html

   WebCamTexture _CamTex;
   private string _SavePath = "C:/WebcamSnaps/";
   int _CaptureCounter = 0;
    
   void TakeSnapshot()
   {
   Texture2D snap = new Texture2D(_CamTex.width, _CamTex.height);
   snap.SetPixels(_CamTex.GetPixels());
   snap.Apply();
    
   System.IO.File.WriteAllBytes(_SavePath + _CaptureCounter.ToString() + ".png", snap.EncodeToPNG());
   ++_CaptureCounter;
   }


Also check out the docs on WebCamTexture https://docs.unity3d.com/Documentation/ScriptReference/WebCamTexture.html