Skip to content

Instantly share code, notes, and snippets.

@v21
Created February 7, 2016 18:28
Show Gist options
  • Save v21/e7c6eed7665d9072a03a to your computer and use it in GitHub Desktop.
Save v21/e7c6eed7665d9072a03a to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
public class Screenshots : EditorWindow {
bool groupEnabled;
int lastCapturedFrame = -1;
bool recording;
public string folder = "Screenshots/Sequence";
public int frameRate = 25;
public int sizeMultiplier = 1;
private string realFolder = "";
// Add menu named "My Window" to the Window menu
[MenuItem ("Window/Screenshot")]
static void Init () {
// Get existing open window or if none, make a new one:
Screenshots window = (Screenshots)EditorWindow.GetWindow (typeof (Screenshots));
window.Show();
}
void OnGUI () {
if (GUILayout.Button("Single Screenshot"))
{
SingleScreenshot();
}
if(!EditorApplication.isPlaying && recording)
{
StopRecording();
}
if (recording)
{
if (GUILayout.Button("Stop Recording"))
{
StopRecording();
}
}
else
{
if (GUILayout.Button("Start Recording"))
{
StartRecording();
}
}
groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
folder = EditorGUILayout.TextField ("Folder name", folder);
frameRate = EditorGUILayout.IntField ("Capture frame rate", frameRate);
sizeMultiplier = EditorGUILayout.IntField ("Size Multiplier", sizeMultiplier);
EditorGUILayout.EndToggleGroup ();
if (realFolder != "")
{
if (GUILayout.Button("Open Last Folder"))
{
OpenInFileBrowser.Open(realFolder);
}
}
}
static void SingleScreenshot()
{
Application.CaptureScreenshot("Screenshots/" + System.DateTime.Now.ToString("s") + ".png", 2);
}
void StopRecording()
{
recording = false;
}
void StartRecording()
{
// Set the playback framerate!
// (real time doesn't influence time anymore)
Time.captureFramerate = frameRate;
// Find a folder that doesn't exist yet by appending numbers!
realFolder = folder;
int count = 1;
while (System.IO.Directory.Exists(realFolder))
{
realFolder = folder + count;
count++;
}
// Create the folder
System.IO.Directory.CreateDirectory(realFolder);
recording = true;
lastCapturedFrame = Time.frameCount;
}
void Update()
{
if (recording && Time.frameCount > lastCapturedFrame && EditorApplication.isPlaying && !EditorApplication.isPaused)
{
lastCapturedFrame = Time.frameCount;
// name is "realFolder/shot 000005.png"
var name = string.Format("{0}/shot {1:D06}.png", realFolder, Time.frameCount);
// Capture the screenshot
Application.CaptureScreenshot(name, sizeMultiplier);
}
}
}
public static class OpenInFileBrowser
{
public static bool IsInMacOS
{
get
{
return UnityEngine.SystemInfo.operatingSystem.IndexOf("Mac OS") != -1;
}
}
public static bool IsInWinOS
{
get
{
return UnityEngine.SystemInfo.operatingSystem.IndexOf("Windows") != -1;
}
}
public static void Test()
{
Open(UnityEngine.Application.dataPath);
}
public static void OpenInMac(string path)
{
bool openInsidesOfFolder = false;
// try mac
string macPath = path.Replace("\\", "/"); // mac finder doesn't like backward slashes
if ( System.IO.Directory.Exists(macPath) ) // if path requested is a folder, automatically open insides of that folder
{
openInsidesOfFolder = true;
}
if ( !macPath.StartsWith("\"") )
{
macPath = "\"" + macPath;
}
if ( !macPath.EndsWith("\"") )
{
macPath = macPath + "\"";
}
string arguments = (openInsidesOfFolder ? "" : "-R ") + macPath;
try
{
System.Diagnostics.Process.Start("open", arguments);
}
catch ( System.ComponentModel.Win32Exception e )
{
// tried to open mac finder in windows
// just silently skip error
// we currently have no platform define for the current OS we are in, so we resort to this
e.HelpLink = ""; // do anything with this variable to silence warning about not using it
}
}
public static void OpenInWin(string path)
{
bool openInsidesOfFolder = false;
// try windows
string winPath = path.Replace("/", "\\"); // windows explorer doesn't like forward slashes
if ( System.IO.Directory.Exists(winPath) ) // if path requested is a folder, automatically open insides of that folder
{
openInsidesOfFolder = true;
}
try
{
System.Diagnostics.Process.Start("explorer.exe", (openInsidesOfFolder ? "/root," : "/select,") + winPath);
}
catch ( System.ComponentModel.Win32Exception e )
{
// tried to open win explorer in mac
// just silently skip error
// we currently have no platform define for the current OS we are in, so we resort to this
e.HelpLink = ""; // do anything with this variable to silence warning about not using it
}
}
public static void Open(string path)
{
if ( IsInWinOS )
{
OpenInWin(path);
}
else if ( IsInMacOS )
{
OpenInMac(path);
}
else // couldn't determine OS
{
OpenInWin(path);
OpenInMac(path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment