MediaPlayer_EditorPlayPause.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2015-2022 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProVideo
  7. {
  8. public partial class MediaPlayer : 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. MediaPlayer[] players = Resources.FindObjectsOfTypeAll<MediaPlayer>();
  34. foreach (MediaPlayer player in players)
  35. {
  36. if (isPaused)
  37. {
  38. player.EditorPause();
  39. }
  40. else
  41. {
  42. player.EditorUnpause();
  43. }
  44. }
  45. }
  46. }
  47. private void EditorPause()
  48. {
  49. if (this.isActiveAndEnabled)
  50. {
  51. if (_controlInterface != null && _controlInterface.IsPlaying())
  52. {
  53. _wasPlayingOnPause = true;
  54. _controlInterface.Pause();
  55. }
  56. StopRenderCoroutine();
  57. }
  58. }
  59. private void EditorUnpause()
  60. {
  61. if (this.isActiveAndEnabled)
  62. {
  63. if (_controlInterface != null && _wasPlayingOnPause)
  64. {
  65. _autoPlayOnStart = true;
  66. _wasPlayingOnPause = false;
  67. _autoPlayOnStartTriggered = false;
  68. }
  69. StartRenderCoroutine();
  70. }
  71. }
  72. #endregion // Play/Pause Support for Unity Editor
  73. }
  74. }
  75. #endif