void StartRecording(bool autoExitPlayMode)
        {
            var settings = (RecorderSettings)m_Editor.target;
            var go       = SceneHook.HookupRecorder();
            var session  = new RecordingSession()
            {
                m_Recorder   = RecordersInventory.GenerateNewRecorder(m_recorderSelector.selectedRecorder, settings),
                m_RecorderGO = go,
            };

            var component = go.AddComponent <RecorderComponent>();

            component.session          = session;
            component.autoExitPlayMode = autoExitPlayMode;

            if (session.SessionCreated() && session.BeginRecording())
            {
                m_State = EState.Recording;
            }
            else
            {
                m_State = EState.Idle;
                StopRecording();
            }
        }
        /// <summary>
        /// Prepares the recording context.
        /// To start recording once you've called this method, you must call <see cref="StartRecording"/>.
        /// </summary>
        /// <remarks>
        /// Sets up the internal data for the recording session and pauses the simulation to ensure a proper synchronization between the Recorder and the Unity Editor.
        /// </remarks>
        public void PrepareRecording()
        {
            if (!Application.isPlaying)
            {
                throw new Exception("You can only call the PrepareRecording method in Play mode.");
            }

            if (RecorderOptions.VerboseMode)
            {
                Debug.Log("Prepare Recording.");
            }

            if (m_Settings == null)
            {
                throw new NullReferenceException("Can start recording without prefs");
            }

            SceneHook.PrepareSessionRoot();
            m_RecordingSessions = new List <RecordingSession>();

            foreach (var recorderSetting in m_Settings.RecorderSettings)
            {
                if (recorderSetting == null)
                {
                    if (RecorderOptions.VerboseMode)
                    {
                        Debug.Log("Ignoring unknown recorder.");
                    }

                    continue;
                }

                m_Settings.ApplyGlobalSetting(recorderSetting);

                if (recorderSetting.HasErrors())
                {
                    if (RecorderOptions.VerboseMode)
                    {
                        Debug.Log("Ignoring invalid recorder '" + recorderSetting.name + "'");
                    }

                    continue;
                }

                if (!recorderSetting.Enabled)
                {
                    if (RecorderOptions.VerboseMode)
                    {
                        Debug.Log("Ignoring disabled recorder '" + recorderSetting.name + "'");
                    }

                    continue;
                }

                var session = m_SceneHook.CreateRecorderSessionWithRecorderComponent(recorderSetting);

                m_RecordingSessions.Add(session);
            }
        }
        void RecordButtonOnGui()
        {
            if (m_Editor == null || m_Editor.target == null)
            {
                return;
            }

            switch (m_State)
            {
            case EState.Idle:
            {
                var errors = new List <string>();
                using (new EditorGUI.DisabledScope(!m_Editor.ValidityCheck(errors)))
                {
                    if (GUILayout.Button("Start Recording"))
                    {
                        StartRecording();
                    }
                }
                break;
            }

            case EState.WaitingForPlayModeToStartRecording:
            {
                using (new EditorGUI.DisabledScope(Time.frameCount - m_FrameCount < 5))
                {
                    if (GUILayout.Button("Stop Recording"))
                    {
                        StopRecording();
                    }
                }
                break;
            }

            case EState.Recording:
            {
                var recorderGO = SceneHook.FindRecorder((RecorderSettings)m_Editor.target);
                if (recorderGO == null)
                {
                    GUILayout.Button("Start Recording");     // just to keep the ui system happy.
                    m_State      = EState.Idle;
                    m_FrameCount = 0;
                }
                else
                {
                    if (GUILayout.Button("Stop Recording"))
                    {
                        StopRecording();
                    }
                    UpdateRecordingProgress(recorderGO);
                }
                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        void RecordButtonOnGui()
        {
            if (m_Editor == null || m_Editor.target == null)
            {
                return;
            }

            switch (m_State)
            {
            case EState.Idle:
            {
                using (new EditorGUI.DisabledScope(!m_Editor.isValid))
                {
                    if (GUILayout.Button("Start Recording"))
                    {
                        StartRecording();
                    }
                }
                break;
            }

            case EState.WaitingForPlayModeToStartRecording:
            {
                using (new EditorGUI.DisabledScope(true))
                    GUILayout.Button("Stop Recording");     // passive
                break;
            }

            case EState.Recording:
            {
                var recorderGO = SceneHook.FindRecorder((RecorderSettings)m_Editor.target);
                if (recorderGO == null)
                {
                    GUILayout.Button("Start Recording");     // just to keep the ui system happy.
                    m_State = EState.Idle;
                }
                else
                {
                    if (GUILayout.Button("Stop Recording"))
                    {
                        StopRecording();
                    }
                    UpdateRecordingProgress(recorderGO);
                }
                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
 void StopRecording()
 {
     if (m_Editor != null)
     {
         var settings = (RecorderSettings)m_Editor.target;
         if (settings != null)
         {
             var recorderGO = SceneHook.FindRecorder(settings);
             if (recorderGO != null)
             {
                 UnityHelpers.Destroy(recorderGO);
             }
         }
     }
 }
 void StopRecording()
 {
     if (m_Editor != null)
     {
         var settings = (RecorderSettings)m_Editor.target;
         if (settings != null)
         {
             var recorderGO = SceneHook.FindRecorder(settings);
             if (recorderGO != null)
             {
                 UnityHelpers.Destroy(recorderGO);
             }
         }
     }
     m_FrameCount = 0;
     m_State      = EState.Idle;
 }
 /// <summary>
 /// The constructor of the RecorderController.
 /// </summary>
 /// <param name="settings">The settings to be used by this RecorderController.</param>
 public RecorderController(RecorderControllerSettings settings)
 {
     m_Settings  = settings;
     m_SceneHook = new SceneHook(Guid.NewGuid().ToString());
 }
        /// <summary>
        /// Start recording. Works only in Playmode.
        /// </summary>
        /// <returns>false if an error occured. The console will usually contains logs about the errors.</returns>
        /// <exception cref="Exception">If not in Playmode.</exception>
        /// <exception cref="NullReferenceException">If settings is null.</exception>
        public bool StartRecording()
        {
            if (!Application.isPlaying)
            {
                throw new Exception("Start Recording can only be called in Playmode.");
            }

            if (m_Settings == null)
            {
                throw new NullReferenceException("Can start recording without prefs");
            }

            if (IsRecording())
            {
                if (Options.verboseMode)
                {
                    Debug.Log("Recording was already started.");
                }

                return(false);
            }

            if (Options.verboseMode)
            {
                Debug.Log("Start Recording.");
            }

            SceneHook.PrepareSessionRoot();

            m_RecordingSessions = new List <RecordingSession>();

            foreach (var recorderSetting in m_Settings.recorderSettings)
            {
                if (recorderSetting == null)
                {
                    if (Options.verboseMode)
                    {
                        Debug.Log("Ignoring unknown recorder.");
                    }

                    continue;
                }

                m_Settings.ApplyGlobalSetting(recorderSetting);

                if (recorderSetting.HasErrors())
                {
                    if (Options.verboseMode)
                    {
                        Debug.Log("Ignoring invalid recorder '" + recorderSetting.name + "'");
                    }

                    continue;
                }

                var errors = new List <string>();

                if (recorderSetting.ValidityCheck(errors))
                {
                    foreach (var error in errors)
                    {
                        Debug.LogWarning(recorderSetting.name + ": " + error);
                    }
                }

                if (errors.Count > 0)
                {
                    if (Options.verboseMode)
                    {
                        Debug.LogWarning("Recorder '" + recorderSetting.name +
                                         "' has warnings and may not record properly.");
                    }
                }

                if (!recorderSetting.enabled)
                {
                    if (Options.verboseMode)
                    {
                        Debug.Log("Ignoring disabled recorder '" + recorderSetting.name + "'");
                    }

                    continue;
                }

                var session = m_SceneHook.CreateRecorderSessionWithRecorderComponent(recorderSetting);

                m_RecordingSessions.Add(session);
            }

            var success = m_RecordingSessions.Any() && m_RecordingSessions.All(r => r.SessionCreated() && r.BeginRecording());

            return(success);
        }
        /// <summary>
        /// Prepares the recording context.
        /// To start recording once you've called this method, you must call <see cref="StartRecording"/>.
        /// </summary>
        /// <remarks>
        /// Sets up the internal data for the recording session and pauses the simulation to ensure a proper synchronization between the Recorder and the Unity Editor.
        /// </remarks>
        public void PrepareRecording()
        {
            if (!Application.isPlaying)
            {
                throw new Exception("You can only call the PrepareRecording method in Play mode.");
            }

            if (RecorderOptions.VerboseMode)
            {
                Debug.Log("Prepare Recording.");
            }

            if (m_Settings == null)
            {
                throw new NullReferenceException("Can start recording without prefs");
            }

            SceneHook.PrepareSessionRoot();
            m_RecordingSessions = new List <RecordingSession>();

            int numberOfSubframeRecorder = 0;
            int numberOfRecorderEnabled  = 0;

            foreach (var recorderSetting in m_Settings.RecorderSettings)
            {
                if (recorderSetting == null)
                {
                    if (RecorderOptions.VerboseMode)
                    {
                        Debug.Log("Ignoring unknown recorder.");
                    }

                    continue;
                }

                m_Settings.ApplyGlobalSetting(recorderSetting);

                if (recorderSetting.HasErrors())
                {
                    if (RecorderOptions.VerboseMode)
                    {
                        Debug.Log("Ignoring invalid recorder '" + recorderSetting.name + "'");
                    }

                    continue;
                }

                if (!recorderSetting.Enabled)
                {
                    if (RecorderOptions.VerboseMode)
                    {
                        Debug.Log("Ignoring disabled recorder '" + recorderSetting.name + "'");
                    }

                    continue;
                }

                if (recorderSetting.Enabled)
                {
                    numberOfRecorderEnabled++;
                }

                // Validate that only one recorder support enable capture SubFrames
                if (UnityHelpers.CaptureAccumulation(recorderSetting))
                {
                    numberOfSubframeRecorder++;

                    if (numberOfSubframeRecorder >= 1 && numberOfRecorderEnabled > 1)
                    {
                        Debug.LogError("You can only use one active Recorder at a time when you capture accumulation.");
                        continue;
                    }
                }

                var session = m_SceneHook.CreateRecorderSessionWithRecorderComponent(recorderSetting);

                m_RecordingSessions.Add(session);
            }
        }