Guest User

ScreenCaptureImageSequence.cs v2 by Hyshinara

a guest
Aug 18th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.20 KB | None | 0 0
  1.  
  2. // based on TakeScreenshot.cs by Aarku (http://wiki.unity3d.com/index.php?title=TakeScreenshot), heavily editted by Hyshinara (Jan Tuts)
  3. // You can capture image sequences at a maximum of 999 takes, each 4.5 hours long at 60fps, per custom nameable scene.
  4.  
  5. // Recordings are placed in the "Captured Image Sequences" folder in the project's base folder / build's executable folder
  6.  
  7. /* TODO (from TakeScreenshot.cs by Aarku):
  8.  * By default, screenshot files are placed next to the executable bundle -- we don't want this in a
  9.  * shipping game, as it will fail if the user doesn't have write access to the Applications folder.
  10.  * Instead we should place the screenshots on the user's desktop. However, the ~/ notation doesn't
  11.  * work, and Unity doesn't have a mechanism to return special paths. Therefore, the correct way to
  12.  * solve this is probably with a plug-in to return OS specific special paths.
  13.  *
  14.  * Mono/.NET has functions to get special paths... see discussion page. --Aarku
  15. */
  16.  
  17. using UnityEngine;
  18. using System.Collections;
  19. using System.Text.RegularExpressions;
  20.  
  21. public class ScreenCaptureImageSequence : MonoBehaviour {
  22.  
  23.     [Space(10)]
  24.     [Range (0, 60)]
  25.     public int frameRate = 30; // when set to 0, ScreenCapture happens at whatever fps Unity runs at (max 60)
  26.     [Range (1, 4)]
  27.     public int captureSizeMultiplier = 1; // increases screen capture size (eg: 2 = 2x2 of original size => HD screen captures in 4K)
  28.     [Space(10)]
  29.     public string sceneTitle = null; // add a custom text in front of the file names
  30.     private string tempSceneTitle = null; // will take the content of sceneTitle, so you can't change it while recording
  31.     public string customFileSuffix = null; // add a custom text after the file names
  32.     private string tempCustomFileSuffix = null; // will take the content of sceneTitle, so you can't change it while recording
  33.  
  34.     private string previousSceneTitle = null;
  35.     private int frameCount = 0; // frameCount instead of screenshotCount
  36.     private int takeCount = 0; // to keep things organised, each time you stop and start recording again, you start a new take
  37.     private bool isRecording = false; // are we recording?
  38.     private string folderName = null;
  39.  
  40.  
  41.     void Start() {
  42.         takeCount = PlayerPrefs.GetInt("TakeCount"); // maintain take count between plays
  43.     }
  44.  
  45.  
  46.     void Update() {
  47.         if (Input.GetButtonDown ("Fire2")) { // Alt key or Right Mouse Button
  48.             StartStopRecording ();
  49.         }
  50.  
  51.         RecordImageSequence ();
  52.     }
  53.  
  54.  
  55.     public void StartStopRecording(){
  56.         if (!isRecording) {
  57.             // setting the frame rate to record at
  58.                 Time.captureFramerate = frameRate;
  59.  
  60.             // check if a new scene is being recorded, and thus Takes should reset
  61.                 previousSceneTitle = PlayerPrefs.GetString("PreviousSceneTitle");
  62.  
  63.                 if (sceneTitle != previousSceneTitle){
  64.                     takeCount = 0;
  65.                     PlayerPrefs.SetString("PreviousSceneTitle", sceneTitle);
  66.                 }
  67.  
  68.             // update and save Take count and reset Frame count
  69.                 takeCount++;
  70.                 PlayerPrefs.SetInt("TakeCount", takeCount);
  71.  
  72.                 frameCount = 0;
  73.  
  74.             // making sure you can't accidentally change the scene title and file suffix while recording
  75.                 tempSceneTitle = sceneTitle;
  76.                 tempCustomFileSuffix = customFileSuffix;
  77.  
  78.             // setting base folderName, styled like "Take 001 (recording started on 2015-08-17 at 15h54m11s)"
  79.                 folderName = "Take " + string.Format("{0:000}", takeCount) + " (recording started on " + string.Format("{0:yyyy-MM-dd}",System.DateTime.Now) + " at " + string.Format("{0:HH.mm.ss}",System.DateTime.Now) + ")";
  80.  
  81.             // make sure the scene title is alphanumerical and add it to the folder name, if there is one
  82.                 if (tempSceneTitle != ""){
  83.                     tempSceneTitle = Regex.Replace(tempSceneTitle, "[^A-Za-z0-9 _]", "");  // removes all non-alphanumerical characters, except for underscores
  84.  
  85.                     folderName = tempSceneTitle + ", " + folderName; // add scene title to folder name
  86.                 }
  87.                     /* "[^A-Za-z0-9_] */ // only alphanumerical and underscores
  88.                     /* @"^[\w]+$" */ // should be a shorthand to "only alphanumeriacal and underscores", but doesn't seem to work?
  89.  
  90.             // make sure the file suffix is alphanumerical, if there is one
  91.                 if (tempCustomFileSuffix != ""){
  92.                     tempCustomFileSuffix = Regex.Replace(tempCustomFileSuffix, "[^A-Za-z0-9_]", "");  // removes all non-alphanumerical characters, except for underscores
  93.                 }
  94.  
  95.             Debug.Log (folderName);
  96.  
  97.             // create a new folder for the take in the "Captured Image Sequences" folder
  98.                 folderName = "Captured Image Sequences/" + folderName;
  99.                 System.IO.Directory.CreateDirectory (folderName);
  100.  
  101.  
  102.             // flag to run recording code in Update
  103.                 isRecording = true;
  104.  
  105.  
  106.             // convert scene title and file suffix to CamelCase for further use in the frame file names
  107.                 if (tempSceneTitle != null){
  108.                     tempSceneTitle = ConvertToCamelCase(tempSceneTitle);
  109.                 }
  110.  
  111.                 if (tempCustomFileSuffix != null){
  112.                     tempCustomFileSuffix = ConvertToCamelCase(tempCustomFileSuffix);
  113.                 }
  114.  
  115.  
  116.         } else {
  117.             isRecording = false;
  118.             Time.captureFramerate = 0;
  119.             Debug.Log ("Recording Stopped.");
  120.         }
  121.     }
  122.  
  123.  
  124.     void RecordImageSequence (){
  125.         if (takeCount >= 999) { // ensuring the value will fit the formatting and not throw an error
  126.             Debug.Log ("You've run out of takes ("+ takeCount +"/999) for this scene! Please start a new scene by changing the Scene Title variable.");
  127.         } else if (isRecording){
  128.             string frameFilename;
  129.            
  130.             do {
  131.                 frameCount++;
  132.                
  133.                 if (frameCount >= 999999){
  134.                     Debug.Log ("You've run of Frames for this Take ("+ frameCount +"/999999). A new Take has started automatically.");
  135.                    
  136.                     takeCount++;
  137.                 }
  138.  
  139.                 // format string to include lead zeros, to insure that the image sequence frames will always maintain the correct order
  140.                 frameFilename = tempSceneTitle /*+ "ImageSequence" */+ "Take" + string.Format("{0:000}", takeCount) + "Frame" + string.Format("{0:000000}", frameCount) + tempCustomFileSuffix + ".png";
  141.  
  142.             } while (System.IO.File.Exists(frameFilename));
  143.            
  144.             Application.CaptureScreenshot(folderName + "/" +  frameFilename, captureSizeMultiplier);
  145.         }
  146.     }
  147.  
  148.    
  149.     string ConvertToCamelCase (string stringToConvert) {
  150.         /*  // alt method, didn't seem to work, should check again
  151.                 string resultString;
  152.  
  153.                 // capitalize the first letter of each word
  154.                     string firstChar = stringToConvert[0].ToString();
  155.                     resultString = stringToConvert.Length > 0 ? firstChar.ToUpper() + stringToConvert.Substring(1) : stringToConvert;
  156.  
  157.                 // remove spaces between words
  158.                     resultString = Regex.Replace(resultString, "[^ ]", "");
  159.  
  160.                 // return the result
  161.                     return (resultString);
  162.         */
  163.  
  164.         // If the provided string is empty, return null
  165.         if (stringToConvert == null || stringToConvert == "") {
  166.                 return null;
  167.             }
  168.  
  169.         // If there is only 1 character, just convert and return that
  170.             if (stringToConvert.Length < 2) {
  171.                 return stringToConvert.ToUpper ();
  172.             }
  173.        
  174.         // Split the string into words.
  175.             string[] words = stringToConvert.Split(new char[] { }, System.StringSplitOptions.RemoveEmptyEntries);
  176.        
  177.         // Combine the words.
  178.             string resultString = "";
  179.             foreach(string word in words)
  180.             {
  181.                 resultString += char.ToUpper(word[0]);
  182.                 resultString += word.Substring(1);
  183.             }
  184.  
  185.         // return the result
  186.             return resultString;
  187.        
  188.     }
  189. }
Add Comment
Please, Sign In to add comment