Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Reflection;
using UnityEngine;
/// Access Unity.Recorder window through reflection
public static class RecordingExtensions
{
/// Start recording video using settings from the Recorder window
public static void StartRecording() => StartStopRecording("StartRecording");
/// Stop the Recorder window recording
public static void StopRecording() => StartStopRecording("StopRecording");
/// Returns true if the Recorder window is recording
public static bool isRecording
{
get
{
if (recorderWindowType == null) // Not running in editor
return false;
var windows = Resources.FindObjectsOfTypeAll(recorderWindowType);
for (int i = 0; i < windows.Length; ++i)
{
var window = windows[i];
if ((bool)isRecordingMethod.Invoke(window, new object[0]))
return true;
}
return false;
}
}
static Type recorderWindowType => _recorderWindowType.Value;
static Lazy<Type> _recorderWindowType = new Lazy<Type>(() =>
Type.GetType("UnityEditor.Recorder.RecorderWindow, Unity.Recorder.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"));
static MethodInfo isRecordingMethod => _isRecordingMethod.Value;
static Lazy<MethodInfo> _isRecordingMethod = new Lazy<MethodInfo>(() =>
recorderWindowType.GetMethod("IsRecording", BindingFlags.Instance | BindingFlags.Public));
static void StartStopRecording(string methodName)
{
Debug.Log($"[RECORDER] {methodName}");
if (recorderWindowType == null)
return;
var getWindowMethod = recorderWindowType.BaseType.GetMethod("GetWindow", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(Type) }, null);
var recorderWindow = getWindowMethod.Invoke(null, new object[] { recorderWindowType });
var startStopMethod = recorderWindowType.GetMethod(methodName);
startStopMethod.Invoke(recorderWindow, new object[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.