Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aholkner
Created October 17, 2019 12:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aholkner/1853cae91bdf4542b81afc1f393d5166 to your computer and use it in GitHub Desktop.
Save aholkner/1853cae91bdf4542b81afc1f393d5166 to your computer and use it in GitHub Desktop.
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