Skip to content

Instantly share code, notes, and snippets.

@HassakuTb
Last active June 28, 2016 02:42
Show Gist options
  • Save HassakuTb/db52f771d616de1bbc8e8be3375f430b to your computer and use it in GitHub Desktop.
Save HassakuTb/db52f771d616de1bbc8e8be3375f430b to your computer and use it in GitHub Desktop.
/**
SequenceScreenshot.cs
Copyright (c) 2016 Hassaku
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
using UnityEngine;
using UnityEditor;
/// <summary>
/// 連続でスクリーンショットを撮りまくるエディタ拡張
/// </summary>
public class SequenceScreenshot : EditorWindow {
/// <summary>
/// スクリーンショットの区別方法
/// </summary>
private enum IdentifyMethod {
Timestamp, // タイムスタンプで識別
SerialNumber, // 連番で識別
}
private const string directoryPathKey = "SequenceScreenshot_DirectoryPath";
private const string filenamePrefixKey = "SequenceScreenshot_FilenamePrefix";
private const string filenameSuffixKey = "SequenceScreenshot_FilenameSuffix";
private const string intervalKey = "SequenceScreenshot_Interval";
private const string maxCaptureCountKey = "SequenceScreenshot_MaxCaptureCount";
private const string identifyMethodKey = "SequenceScreenshot_IdentifyMethod";
private const string timestampFormatKey = "SequenceScreenshot_TimestampFormat";
private const string serialNumberKey = "SequenceScreenshot_SerialNumber";
private const string serialNumberOffsetKey = "SequenceScreenshot_SerialNumberOffset";
private const string windowTitle = "Screenshot";
private readonly Vector2 bigButtonSize = new Vector2(200, 40);
private const float smallButtonHeight = 30;
private const string startButtonLabel = "Capture Start";
private const string stopButtonLabel = "Capture Stop";
private bool isStarted = false;
private string directoryPath;
private string filenamePrefix;
private string filenameSuffix;
private float interval;
private int maxCaptureCount;
private IdentifyMethod identifyMethod;
private string timestampFormat;
private int serialNumber;
private int serialNumberOffset;
private string filenameSample;
private bool captureStarted = false;
private double previousTime;
private double accumeTime = 0.0;
private int captureCount;
/// <summary>
/// メニューを選択した時ウィンドウを開く
/// </summary>
[MenuItem("Extension/Sequence Screenshot")]
static void OpenWindow() {
// ウィンドウを開く
GetWindow<SequenceScreenshot>(windowTitle);
}
void OnGUI() {
if (isStarted) {
// ストップボタン
if (GUILayout.Button(stopButtonLabel, GUILayout.Width(bigButtonSize.x), GUILayout.Height(bigButtonSize.y))) {
isStarted = false;
}
}
else {
// スタートボタン
if (GUILayout.Button(startButtonLabel, GUILayout.Width(bigButtonSize.x), GUILayout.Height(bigButtonSize.y))) {
isStarted = true;
captureStarted = false;
}
}
EditorGUILayout.Space();
EditorGUILayout.LabelField("Output Folder", directoryPath);
if (GUILayout.Button("Select Folder", GUILayout.Height(smallButtonHeight))) {
directoryPath = EditorUtility.OpenFolderPanel("Select Folder", Application.dataPath, "");
}
filenamePrefix = EditorGUILayout.TextField("Filename Prefix", filenamePrefix);
filenameSuffix = EditorGUILayout.TextField("Filename Suffix", filenameSuffix);
EditorGUILayout.Space();
interval = EditorGUILayout.FloatField("Capture Interval", interval);
maxCaptureCount = EditorGUILayout.IntField("Max Capture Count", maxCaptureCount);
EditorGUILayout.Space();
identifyMethod = (IdentifyMethod)EditorGUILayout.EnumPopup("Identify Method", identifyMethod);
switch (identifyMethod) {
case IdentifyMethod.Timestamp:
timestampFormat = EditorGUILayout.TextField("Format", timestampFormat);
filenameSample = filenamePrefix + timestampFormat + filenameSuffix;
break;
case IdentifyMethod.SerialNumber:
serialNumberOffset = EditorGUILayout.IntField("Offset", serialNumberOffset);
if (GUILayout.Button("Reset Serial Number", GUILayout.Height(smallButtonHeight))) {
serialNumber = serialNumberOffset;
}
EditorGUILayout.LabelField("Current Serial Number", serialNumber.ToString());
filenameSample = filenamePrefix + serialNumber.ToString() + filenameSuffix;
break;
}
EditorGUILayout.Space();
EditorGUILayout.LabelField("Filename Sample", filenameSample);
if (!System.IO.Directory.Exists(directoryPath)) {
EditorGUILayout.HelpBox("Folder \"" + directoryPath + "\" is not exist.", MessageType.Error);
}
}
/// <summary>
/// 表示している間毎フレーム呼び出される
/// </summary>
void Update() {
if (!isStarted || !EditorApplication.isPlaying) return;
if (!System.IO.Directory.Exists(directoryPath)) return;
if (!captureStarted) {
previousTime = EditorApplication.timeSinceStartup;
accumeTime = 0.0;
captureCount = 0;
captureStarted = true;
}
double time = EditorApplication.timeSinceStartup;
accumeTime += time - previousTime;
previousTime = time;
if(accumeTime >= interval) {
string filename = string.Empty;
switch (identifyMethod) {
case IdentifyMethod.Timestamp:
string timestamp = System.DateTime.Now.ToString(timestampFormat);
filename = directoryPath + "/" + filenamePrefix + timestamp + filenameSuffix;
break;
case IdentifyMethod.SerialNumber:
filename = directoryPath + "/" + filenamePrefix + serialNumber + filenameSuffix;
++serialNumber;
break;
}
Application.CaptureScreenshot(filename);
++captureCount;
EditorPrefs.SetInt(serialNumberKey, serialNumber);
if (maxCaptureCount > 0 && captureCount >= maxCaptureCount) isStarted = false;
accumeTime -= interval;
Repaint();
}
}
void OnFocus() {
directoryPath = EditorPrefs.GetString(directoryPathKey, EditorApplication.applicationContentsPath);
filenamePrefix = EditorPrefs.GetString(filenamePrefixKey, "capture_");
filenameSuffix = EditorPrefs.GetString(filenameSuffixKey, ".png");
interval = EditorPrefs.GetFloat(intervalKey, 5.0f);
maxCaptureCount = EditorPrefs.GetInt(maxCaptureCountKey, 0);
identifyMethod = (IdentifyMethod)System.Enum.Parse(typeof(IdentifyMethod), EditorPrefs.GetString(identifyMethodKey, IdentifyMethod.Timestamp.ToString()));
timestampFormat = EditorPrefs.GetString(timestampFormatKey, "yyyy-MM-dd_hh-mm-ss");
serialNumberOffset = EditorPrefs.GetInt(serialNumberOffsetKey, 0);
serialNumber = EditorPrefs.GetInt(serialNumberKey, serialNumberOffset);
}
/// <summary>
/// フォーカスを失った時
/// </summary>
void OnLostFocus() {
EditorPrefs.SetString(directoryPathKey, directoryPath);
EditorPrefs.SetString(filenamePrefixKey, filenamePrefix);
EditorPrefs.SetString(filenameSuffixKey, filenameSuffix);
EditorPrefs.SetFloat(intervalKey, interval);
EditorPrefs.SetInt(maxCaptureCountKey, maxCaptureCount);
EditorPrefs.SetString(identifyMethodKey, identifyMethod.ToString());
EditorPrefs.SetString(timestampFormatKey, timestampFormat);
EditorPrefs.SetInt(serialNumberKey, serialNumber);
EditorPrefs.SetInt(serialNumberOffsetKey, serialNumberOffset);
}
/// <summary>
/// ウィンドウが閉じられるとき呼び出される
/// </summary>
void OnDestroy() {
EditorPrefs.SetString(directoryPathKey, directoryPath);
EditorPrefs.SetString(filenamePrefixKey, filenamePrefix);
EditorPrefs.SetString(filenameSuffixKey, filenameSuffix);
EditorPrefs.SetFloat(intervalKey, interval);
EditorPrefs.SetInt(maxCaptureCountKey, maxCaptureCount);
EditorPrefs.SetString(identifyMethodKey, identifyMethod.ToString());
EditorPrefs.SetString(timestampFormatKey, timestampFormat);
EditorPrefs.SetInt(serialNumberKey, serialNumber);
EditorPrefs.SetInt(serialNumberOffsetKey, serialNumberOffset);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment