Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // based on TakeScreenshot.cs by Aarku (http://wiki.unity3d.com/index.php?title=TakeScreenshot), heavily editted by Hyshinara (Jan Tuts)
- // You can capture image sequences at a maximum of 999 takes, each 4.5 hours long at 60fps, per custom nameable scene.
- // Recordings are placed in the "Captured Image Sequences" folder in the project's base folder / build's executable folder
- /* TODO (from TakeScreenshot.cs by Aarku):
- * By default, screenshot files are placed next to the executable bundle -- we don't want this in a
- * shipping game, as it will fail if the user doesn't have write access to the Applications folder.
- * Instead we should place the screenshots on the user's desktop. However, the ~/ notation doesn't
- * work, and Unity doesn't have a mechanism to return special paths. Therefore, the correct way to
- * solve this is probably with a plug-in to return OS specific special paths.
- *
- * Mono/.NET has functions to get special paths... see discussion page. --Aarku
- */
- using UnityEngine;
- using System.Collections;
- using System.Text.RegularExpressions;
- public class ScreenCaptureImageSequence : MonoBehaviour {
- [Space(10)]
- [Range (0, 60)]
- public int frameRate = 30; // when set to 0, ScreenCapture happens at whatever fps Unity runs at (max 60)
- [Range (1, 4)]
- public int captureSizeMultiplier = 1; // increases screen capture size (eg: 2 = 2x2 of original size => HD screen captures in 4K)
- [Space(10)]
- public string sceneTitle = null; // add a custom text in front of the file names
- private string tempSceneTitle = null; // will take the content of sceneTitle, so you can't change it while recording
- public string customFileSuffix = null; // add a custom text after the file names
- private string tempCustomFileSuffix = null; // will take the content of sceneTitle, so you can't change it while recording
- private string previousSceneTitle = null;
- private int frameCount = 0; // frameCount instead of screenshotCount
- private int takeCount = 0; // to keep things organised, each time you stop and start recording again, you start a new take
- private bool isRecording = false; // are we recording?
- private string folderName = null;
- void Start() {
- takeCount = PlayerPrefs.GetInt("TakeCount"); // maintain take count between plays
- }
- void Update() {
- if (Input.GetButtonDown ("Fire2")) { // Alt key or Right Mouse Button
- StartStopRecording ();
- }
- RecordImageSequence ();
- }
- public void StartStopRecording(){
- if (!isRecording) {
- // setting the frame rate to record at
- Time.captureFramerate = frameRate;
- // check if a new scene is being recorded, and thus Takes should reset
- previousSceneTitle = PlayerPrefs.GetString("PreviousSceneTitle");
- if (sceneTitle != previousSceneTitle){
- takeCount = 0;
- PlayerPrefs.SetString("PreviousSceneTitle", sceneTitle);
- }
- // update and save Take count and reset Frame count
- takeCount++;
- PlayerPrefs.SetInt("TakeCount", takeCount);
- frameCount = 0;
- // making sure you can't accidentally change the scene title and file suffix while recording
- tempSceneTitle = sceneTitle;
- tempCustomFileSuffix = customFileSuffix;
- // setting base folderName, styled like "Take 001 (recording started on 2015-08-17 at 15h54m11s)"
- 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) + ")";
- // make sure the scene title is alphanumerical and add it to the folder name, if there is one
- if (tempSceneTitle != ""){
- tempSceneTitle = Regex.Replace(tempSceneTitle, "[^A-Za-z0-9 _]", ""); // removes all non-alphanumerical characters, except for underscores
- folderName = tempSceneTitle + ", " + folderName; // add scene title to folder name
- }
- /* "[^A-Za-z0-9_] */ // only alphanumerical and underscores
- /* @"^[\w]+$" */ // should be a shorthand to "only alphanumeriacal and underscores", but doesn't seem to work?
- // make sure the file suffix is alphanumerical, if there is one
- if (tempCustomFileSuffix != ""){
- tempCustomFileSuffix = Regex.Replace(tempCustomFileSuffix, "[^A-Za-z0-9_]", ""); // removes all non-alphanumerical characters, except for underscores
- }
- Debug.Log (folderName);
- // create a new folder for the take in the "Captured Image Sequences" folder
- folderName = "Captured Image Sequences/" + folderName;
- System.IO.Directory.CreateDirectory (folderName);
- // flag to run recording code in Update
- isRecording = true;
- // convert scene title and file suffix to CamelCase for further use in the frame file names
- if (tempSceneTitle != null){
- tempSceneTitle = ConvertToCamelCase(tempSceneTitle);
- }
- if (tempCustomFileSuffix != null){
- tempCustomFileSuffix = ConvertToCamelCase(tempCustomFileSuffix);
- }
- } else {
- isRecording = false;
- Time.captureFramerate = 0;
- Debug.Log ("Recording Stopped.");
- }
- }
- void RecordImageSequence (){
- if (takeCount >= 999) { // ensuring the value will fit the formatting and not throw an error
- Debug.Log ("You've run out of takes ("+ takeCount +"/999) for this scene! Please start a new scene by changing the Scene Title variable.");
- } else if (isRecording){
- string frameFilename;
- do {
- frameCount++;
- if (frameCount >= 999999){
- Debug.Log ("You've run of Frames for this Take ("+ frameCount +"/999999). A new Take has started automatically.");
- takeCount++;
- }
- // format string to include lead zeros, to insure that the image sequence frames will always maintain the correct order
- frameFilename = tempSceneTitle /*+ "ImageSequence" */+ "Take" + string.Format("{0:000}", takeCount) + "Frame" + string.Format("{0:000000}", frameCount) + tempCustomFileSuffix + ".png";
- } while (System.IO.File.Exists(frameFilename));
- Application.CaptureScreenshot(folderName + "/" + frameFilename, captureSizeMultiplier);
- }
- }
- string ConvertToCamelCase (string stringToConvert) {
- /* // alt method, didn't seem to work, should check again
- string resultString;
- // capitalize the first letter of each word
- string firstChar = stringToConvert[0].ToString();
- resultString = stringToConvert.Length > 0 ? firstChar.ToUpper() + stringToConvert.Substring(1) : stringToConvert;
- // remove spaces between words
- resultString = Regex.Replace(resultString, "[^ ]", "");
- // return the result
- return (resultString);
- */
- // If the provided string is empty, return null
- if (stringToConvert == null || stringToConvert == "") {
- return null;
- }
- // If there is only 1 character, just convert and return that
- if (stringToConvert.Length < 2) {
- return stringToConvert.ToUpper ();
- }
- // Split the string into words.
- string[] words = stringToConvert.Split(new char[] { }, System.StringSplitOptions.RemoveEmptyEntries);
- // Combine the words.
- string resultString = "";
- foreach(string word in words)
- {
- resultString += char.ToUpper(word[0]);
- resultString += word.Substring(1);
- }
- // return the result
- return resultString;
- }
- }
Add Comment
Please, Sign In to add comment