#if UNITY_EDITOR #define USE_REFLECTION #endif using System; using System.Collections.Generic; #if UNITY_EDITOR #if USE_REFLECTION using System.Reflection; #else using UnityEditor.Recorder; #endif #endif namespace UnityEngine.Rendering { public static class CameraCaptureBridge { #if USE_REFLECTION private static FieldInfo m_Enabled; private static MethodInfo m_GetActions; #endif private static Dictionary>> actionDict = new Dictionary>>(); private static bool _enabled; static CameraCaptureBridge() { #if USE_REFLECTION const string optionsClassName = "UnityEditor.Recorder.Options"; const string editorDllName = "Unity.Recorder.Editor"; var optionsType = Type.GetType(optionsClassName + ", " + editorDllName); if (optionsType == null) return; const string useCameraCaptureCallbacksFieldName = "useCameraCaptureCallbacks"; var useCameraCaptureCallbacksField = optionsType.GetField( useCameraCaptureCallbacksFieldName, BindingFlags.Public | BindingFlags.Static); if (useCameraCaptureCallbacksField == null) return; const string captureClassName = "UnityEditor.Recorder.Input.CameraCapture"; var captureType = Type.GetType(captureClassName + ", " + editorDllName); if (captureType == null) return; const string getActionsMethodName = "GetActions"; var getActionsMethod = captureType.GetMethod( getActionsMethodName, BindingFlags.Public | BindingFlags.Static); if (getActionsMethod == null) return; m_Enabled = useCameraCaptureCallbacksField; m_GetActions = getActionsMethod; #endif } public static bool enabled { get { return #if USE_REFLECTION m_Enabled == null ? _enabled : (bool)m_Enabled.GetValue(null) #elif UNITY_EDITOR UnityEditor.Recorder.Options.useCameraCaptureCallbacks #else _enabled #endif ; } set { #if USE_REFLECTION m_Enabled?.SetValue(null, value); #elif UNITY_EDITOR UnityEditor.Recorder.Options.useCameraCaptureCallbacks = value; #endif _enabled = value; } } /// /// Provides the set actions to the renderer to be triggered at the end of the render loop for camera capture /// /// The camera to get actions for /// Enumeration of actions public static IEnumerator> GetCaptureActions(Camera camera) { #if USE_REFLECTION if (m_GetActions != null) { var recorderActions = (m_GetActions.Invoke(null, new object[] { camera }) as IEnumerator>); if (recorderActions != null) return recorderActions; } #elif UNITY_EDITOR var recorderActions = UnityEditor.Recorder.Input.CameraCapture.GetActions(camera); if (recorderActions != null) return recorderActions; #endif if (!actionDict.TryGetValue(camera, out var actions)) return null; return actions.GetEnumerator(); } /// /// Adds actions for camera capture /// /// The camera to add actions for /// The action to add public static void AddCaptureAction(Camera camera, Action action) { actionDict.TryGetValue(camera, out var actions); if (actions == null) { actions = new HashSet>(); actionDict.Add(camera, actions); } actions.Add(action); } /// /// Removes actions for camera capture /// /// The camera to remove actions for /// The action to remove public static void RemoveCaptureAction(Camera camera, Action action) { if (camera == null) return; if (actionDict.TryGetValue(camera, out var actions)) actions.Remove(action); } } }