Guest User

Untitled

a guest
Oct 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4.  
  5. // Screen Recorder will save individual images of active scene in any resolution and of a specific image format
  6. // including raw, jpg, png, and ppm. Raw and PPM are the fastest image formats for saving.
  7. //
  8. // You can compile these images into a video using ffmpeg:
  9. // ffmpeg -i screen_3840x2160_%d.ppm -y test.avi
  10.  
  11. public class TakeScreenShot : MonoBehaviour
  12. {
  13. // 4k = 3840 x 2160 1080p = 1920 x 1080
  14. public int captureWidth = 1920;
  15. public int captureHeight = 1080;
  16.  
  17. // optional game object to hide during screenshots (usually your scene canvas hud)
  18. public GameObject hideGameObject;
  19.  
  20. // optimize for many screenshots will not destroy any objects so future screenshots will be fast
  21. public bool optimizeForManyScreenshots = true;
  22.  
  23. // configure with raw, jpg, png, or ppm (simple raw format)
  24. public enum Format { RAW, JPG, PNG, PPM };
  25. public Format format = Format.PPM;
  26.  
  27. // folder to write output (defaults to data path)
  28. public string folder;
  29.  
  30. // private vars for screenshot
  31. private Rect rect;
  32. private RenderTexture renderTexture;
  33. private Texture2D screenShot;
  34. private int counter = 0; // image #
  35.  
  36. // commands
  37. private bool captureScreenshot = false;
  38. private bool captureVideo = false;
  39.  
  40. public Camera camera;
  41.  
  42. // create a unique filename using a one-up variable
  43. private string uniqueFilename(int width, int height)
  44. {
  45. // if folder not specified by now use a good default
  46. if (folder == null || folder.Length == 0)
  47. {
  48. folder = Application.dataPath;
  49. if (Application.isEditor)
  50. {
  51. // put screenshots in folder above asset path so unity doesn't index the files
  52. var stringPath = folder + "/..";
  53. folder = Path.GetFullPath(stringPath);
  54. }
  55. folder += "/screenshots";
  56.  
  57. // make sure directoroy exists
  58. System.IO.Directory.CreateDirectory(folder);
  59.  
  60. // count number of files of specified format in folder
  61. string mask = string.Format("screen_{0}x{1}*.{2}", width, height, format.ToString().ToLower());
  62. counter = Directory.GetFiles(folder, mask, SearchOption.TopDirectoryOnly).Length;
  63. }
  64.  
  65. // use width, height, and counter for unique file name
  66. var filename = string.Format("{0}/screen_{1}x{2}_{3}.{4}", folder, width, height, counter, format.ToString().ToLower());
  67.  
  68. // up counter for next call
  69. ++counter;
  70.  
  71. // return unique filename
  72. return filename;
  73. }
  74.  
  75. public void CaptureScreenshot()
  76. {
  77. captureScreenshot = true;
  78. }
  79.  
  80. void Update()
  81. {
  82. // check keyboard 'k' for one time screenshot capture and holding down 'v' for continious screenshots
  83. captureScreenshot |= Input.GetKeyDown("k");
  84. captureVideo = Input.GetKey("v");
  85.  
  86. if (captureScreenshot || captureVideo)
  87. {
  88. captureScreenshot = false;
  89.  
  90. // hide optional game object if set
  91. if (hideGameObject != null) hideGameObject.SetActive(false);
  92.  
  93. // create screenshot objects if needed
  94. if (renderTexture == null)
  95. {
  96. // creates off-screen render texture that can rendered into
  97. rect = new Rect(0, 0, captureWidth, captureHeight);
  98. renderTexture = new RenderTexture(captureWidth, captureHeight, 24);
  99. screenShot = new Texture2D(captureWidth, captureHeight, TextureFormat.RGB24, false);
  100. }
  101.  
  102. // get main camera and manually render scene into rt
  103. // NOTE: added because there was no reference to camera in original script; must add this script to Camera
  104. camera.targetTexture = renderTexture;
  105. camera.Render();
  106.  
  107. // read pixels will read from the currently active render texture so make our offscreen
  108. // render texture active and then read the pixels
  109. RenderTexture.active = renderTexture;
  110. screenShot.ReadPixels(rect, 0, 0);
  111.  
  112. // reset active camera texture and render texture
  113. camera.targetTexture = null;
  114. RenderTexture.active = null;
  115.  
  116. // get our unique filename
  117. string filename = uniqueFilename((int) rect.width, (int) rect.height);
  118.  
  119. // pull in our file header/data bytes for the specified image format (has to be done from main thread)
  120. byte[] fileHeader = null;
  121. byte[] fileData = null;
  122. if (format == Format.RAW)
  123. {
  124. fileData = screenShot.GetRawTextureData();
  125. }
  126. else if (format == Format.PNG)
  127. {
  128. fileData = screenShot.EncodeToPNG();
  129. }
  130. else if (format == Format.JPG)
  131. {
  132. fileData = screenShot.EncodeToJPG();
  133. }
  134. else // ppm
  135. {
  136. // create a file header for ppm formatted file
  137. string headerStr = string.Format("P6\n{0} {1}\n255\n", rect.width, rect.height);
  138. fileHeader = System.Text.Encoding.ASCII.GetBytes(headerStr);
  139. fileData = screenShot.GetRawTextureData();
  140. }
  141.  
  142. // create new thread to save the image to file (only operation that can be done in background)
  143. new System.Threading.Thread(() =>
  144. {
  145. // create file and write optional header with image bytes
  146. var f = System.IO.File.Create(filename);
  147. if (fileHeader != null) f.Write(fileHeader, 0, fileHeader.Length);
  148. f.Write(fileData, 0, fileData.Length);
  149. f.Close();
  150. Debug.Log(string.Format("Wrote screenshot {0} of size {1}", filename, fileData.Length));
  151. }).Start();
  152.  
  153. // unhide optional game object if set
  154. if (hideGameObject != null) hideGameObject.SetActive(true);
  155.  
  156. // cleanup if needed
  157. if (optimizeForManyScreenshots == false)
  158. {
  159. Destroy(renderTexture);
  160. renderTexture = null;
  161. screenShot = null;
  162. }
  163. }
  164. }
  165. }
Add Comment
Please, Sign In to add comment