CaptureBaser_EditorPlayPause.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProMovieCapture
  7. {
  8. public partial class CaptureBase : MonoBehaviour
  9. {
  10. #region Play/Pause Support for Unity Editor
  11. // This code handles the pause/play buttons in the editor
  12. private static void SetupEditorPlayPauseSupport()
  13. {
  14. #if UNITY_2017_2_OR_NEWER
  15. UnityEditor.EditorApplication.pauseStateChanged -= OnUnityPauseModeChanged;
  16. UnityEditor.EditorApplication.pauseStateChanged += OnUnityPauseModeChanged;
  17. #else
  18. UnityEditor.EditorApplication.playmodeStateChanged -= OnUnityPlayModeChanged;
  19. UnityEditor.EditorApplication.playmodeStateChanged += OnUnityPlayModeChanged;
  20. #endif
  21. }
  22. #if UNITY_2017_2_OR_NEWER
  23. private static void OnUnityPauseModeChanged(UnityEditor.PauseState state)
  24. {
  25. OnUnityPlayModeChanged();
  26. }
  27. #endif
  28. private static void OnUnityPlayModeChanged()
  29. {
  30. if (UnityEditor.EditorApplication.isPlaying)
  31. {
  32. bool isPaused = UnityEditor.EditorApplication.isPaused;
  33. CaptureBase[] captures = Resources.FindObjectsOfTypeAll<CaptureBase>();
  34. foreach (CaptureBase capture in captures)
  35. {
  36. if (isPaused)
  37. {
  38. capture.EditorPause();
  39. }
  40. else
  41. {
  42. capture.EditorUnpause();
  43. }
  44. }
  45. }
  46. }
  47. private void EditorPause()
  48. {
  49. if (this.isActiveAndEnabled)
  50. {
  51. _capturePrePauseTotalTime += (Time.realtimeSinceStartup - _captureStartTime);
  52. PauseCapture();
  53. }
  54. }
  55. private void EditorUnpause()
  56. {
  57. if (this.isActiveAndEnabled)
  58. {
  59. ResumeCapture();
  60. _captureStartTime = Time.realtimeSinceStartup;
  61. }
  62. }
  63. #endregion // Play/Pause Support for Unity Editor
  64. }
  65. }
  66. #endif