Last active
January 29, 2020 23:49
-
-
Save kankikuchi/188598e01cc8af2fec8e881d03687e84 to your computer and use it in GitHub Desktop.
Unity Recorderを使ってエディタを再生した時に自動で録画を行う【Unity】【アセット】【エディタ拡張】
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AutoRecorder.cs | |
// http://kan-kikuchi.hatenablog.com/entry/AutoRecorder | |
// | |
// Created by kan.kikuchi on 2020.01.17 | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using UnityEditor; | |
using UnityEditor.Recorder; | |
using UnityEngine; | |
/// <summary> | |
/// エディタが再生された時に自動で録画をするクラス | |
/// </summary> | |
[InitializeOnLoad]//エディター起動時にコンストラクタが呼ばれるように | |
public static class AutoRecorder{ | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
/// <summary> | |
/// コンストラクタ(InitializeOnLoad属性によりエディター起動時に呼び出される) | |
/// </summary> | |
static AutoRecorder() { | |
//playModeStateChangedイベントにメソッド登録 | |
EditorApplication.playModeStateChanged += OnChangedPlayMode; | |
} | |
//================================================================================= | |
//プレイモードの変更 | |
//================================================================================= | |
//プレイモードが変更された | |
private static void OnChangedPlayMode(PlayModeStateChange state) { | |
//自動録画設定されてない場合はスルー | |
if (!AutoRecorderSettingsWindow.IsAutoRecord()) { | |
return; | |
} | |
//エディタの再生開始時 | |
if (state == PlayModeStateChange.EnteredPlayMode) { | |
StartRecording(); | |
} | |
//エディタの再生終了時 | |
else if (state == PlayModeStateChange.EnteredEditMode) { | |
DeleteFileIfNeeded(); | |
} | |
} | |
//================================================================================= | |
//録画開始 | |
//================================================================================= | |
//録画開始 | |
private static void StartRecording() { | |
//Recorderのウィンドウ取得 | |
var recorderWindow = (RecorderWindow)EditorWindow.GetWindow(typeof(RecorderWindow)); | |
if (recorderWindow == null) { | |
Debug.LogWarning("RecorderWindowがないので自動再生出来ませんでした"); | |
return; | |
} | |
if (recorderWindow.IsRecording()) { | |
return; | |
} | |
Debug.Log("自動録画開始"); | |
recorderWindow.StartRecording(); | |
} | |
//================================================================================= | |
//ファイルの削除 | |
//================================================================================= | |
//上限数を超えたらファイルを削除する | |
private static void DeleteFileIfNeeded() { | |
//Recorderのウィンドウ取得 | |
var recorderWindow = (RecorderWindow)EditorWindow.GetWindow(typeof(RecorderWindow)); | |
if (recorderWindow == null) { | |
Debug.LogWarning("RecorderWindowがないのでファイルの削除が出来ませんでした"); | |
return; | |
} | |
//設定ファイル(private)を無理やり取得する | |
var windowType = recorderWindow.GetType(); | |
var settingsFieldInfo = windowType.GetField("m_ControllerSettings", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance); | |
var recorderControllerSettings = (RecorderControllerSettings)settingsFieldInfo.GetValue(recorderWindow); | |
//保存先のパスを取得 | |
foreach (var recorderSetting in recorderControllerSettings.recorderSettings) { | |
DeleteFileIfNeeded(Path.GetDirectoryName(recorderSetting.outputFile)); | |
} | |
} | |
//指定したディレクトリ内のファイル数が上限を超えたらファイルを削除する | |
private static void DeleteFileIfNeeded(string directoryPath) { | |
//対象ファイルのパスを取得 | |
var filePaths = Directory | |
.GetFiles (directoryPath, "*", SearchOption.AllDirectories) //ディレクトリ内の全ファイルを取得 | |
.Where(filePath => Path.GetFileName(filePath) != ".DS_Store") //.DS_Storeは除く | |
.OrderBy(filePath => File.GetLastWriteTime(filePath).Date) //日付順に昇順でソート | |
.ThenBy(filePath => File.GetLastWriteTime(filePath).TimeOfDay) //同じ日付内で時刻順に昇順でソート | |
.ToList(); | |
//上限を超えている場合はファイルを古い物から削除していく | |
while (filePaths.Count > AutoRecorderSettingsWindow.GetFileNumMax()) { | |
var filePath = filePaths[0]; | |
filePaths.Remove(filePath); | |
File.Delete(filePath); | |
Debug.Log($"{filePath}を削除しました"); | |
} | |
} | |
} | |
/// <summary> | |
/// AutoRecorderの設定をするためのウィンドウ | |
/// </summary> | |
public class AutoRecorderSettingsWindow : EditorWindow{ | |
//設定の保存Key | |
private static readonly string IS_AUTO_RECORD_SAVE_KEY = "IS_AUTO_RECORD_SAVE_KEY"; | |
private static readonly string FILE_NUM_MAX_SAVE_KEY = "FILE_NUM_MAX_SAVE_KEY"; | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
//メニューからウィンドウを表示 | |
[MenuItem("Tools/Auto Recorder/Settings Window")] | |
public static void Open (){ | |
AutoRecorderSettingsWindow.GetWindow (typeof(AutoRecorderSettingsWindow)); | |
} | |
//================================================================================= | |
//更新 | |
//================================================================================= | |
private void OnGUI(){ | |
EditorGUILayout.BeginVertical(GUI.skin.box); | |
//自動録画をするかを設定するトグルスイッチ | |
EditorGUILayout.BeginVertical(GUI.skin.box); | |
var beforeAutoRecordFlag = IsAutoRecord(); | |
var afterAutoRecordFlag = EditorGUILayout.ToggleLeft("自動録画をするか", beforeAutoRecordFlag); | |
if (afterAutoRecordFlag != beforeAutoRecordFlag) { | |
SetAutoRecordFlag(afterAutoRecordFlag); | |
} | |
EditorGUILayout.EndVertical(); | |
//保存するファイル数の上限を設定するGUI | |
EditorGUILayout.BeginVertical(GUI.skin.box); | |
var beforeFileNumMax = GetFileNumMax(); | |
var afterFileNumMax = Mathf.Max(1, EditorGUILayout.IntField("保存するファイル数の上限", beforeFileNumMax)); | |
if (afterFileNumMax != beforeFileNumMax) { | |
SetFileNumMax(afterFileNumMax); | |
} | |
EditorGUILayout.EndVertical(); | |
EditorGUILayout.EndVertical(); | |
} | |
//================================================================================= | |
//設定値の読み書き | |
//================================================================================= | |
/// <summary> | |
/// 自動録画をするか | |
/// </summary> | |
public static bool IsAutoRecord() { | |
return !string.IsNullOrEmpty(EditorUserSettings.GetConfigValue(IS_AUTO_RECORD_SAVE_KEY)); | |
} | |
//自動録画をするかのフラグを設定 | |
private static void SetAutoRecordFlag(bool isAutoRecord) { | |
EditorUserSettings.SetConfigValue(IS_AUTO_RECORD_SAVE_KEY, isAutoRecord ? IS_AUTO_RECORD_SAVE_KEY : ""); | |
} | |
/// <summary> | |
/// 保存するファイル数の上限を取得 | |
/// </summary> | |
public static int GetFileNumMax() { | |
var fileNumMaxText = EditorUserSettings.GetConfigValue(FILE_NUM_MAX_SAVE_KEY); | |
if (string.IsNullOrEmpty(fileNumMaxText)) { | |
int fileNumMaxDefault = 10; | |
SetFileNumMax(fileNumMaxDefault); | |
return fileNumMaxDefault; | |
} | |
return int.Parse(fileNumMaxText); | |
} | |
//保存するファイル数の上限を設定 | |
private static void SetFileNumMax(int numMax) { | |
EditorUserSettings.SetConfigValue(FILE_NUM_MAX_SAVE_KEY, numMax.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment