VideoPlayableBehaviour.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using UnityEngine;
  2. using UnityEngine.Playables;
  3. using UnityEngine.Video;
  4. namespace UnityEngine.Timeline
  5. {
  6. public class VideoPlayableBehaviour : PlayableBehaviour
  7. {
  8. public VideoPlayer videoPlayer;
  9. public VideoClip videoClip;
  10. public bool mute = false;
  11. public bool loop = true;
  12. public double preloadTime = 0.3;
  13. public double clipInTime = 0.0;
  14. private bool playedOnce = false;
  15. private bool preparing = false;
  16. public void PrepareVideo()
  17. {
  18. if (videoPlayer == null || videoClip == null)
  19. return;
  20. videoPlayer.targetCameraAlpha = 0.0f;
  21. if (videoPlayer.clip != videoClip)
  22. StopVideo();
  23. if (videoPlayer.isPrepared || preparing)
  24. return;
  25. videoPlayer.source = VideoSource.VideoClip;
  26. videoPlayer.clip = videoClip;
  27. videoPlayer.playOnAwake = false;
  28. videoPlayer.waitForFirstFrame = true;
  29. videoPlayer.isLooping = loop;
  30. for (ushort i = 0; i < videoClip.audioTrackCount; ++i)
  31. {
  32. if (videoPlayer.audioOutputMode == VideoAudioOutputMode.Direct)
  33. videoPlayer.SetDirectAudioMute(i, mute || !Application.isPlaying);
  34. else if (videoPlayer.audioOutputMode == VideoAudioOutputMode.AudioSource)
  35. {
  36. AudioSource audioSource = videoPlayer.GetTargetAudioSource(i);
  37. if (audioSource != null)
  38. audioSource.mute = mute || !Application.isPlaying;
  39. }
  40. }
  41. videoPlayer.loopPointReached += LoopPointReached;
  42. videoPlayer.time = clipInTime;
  43. videoPlayer.Prepare();
  44. preparing = true;
  45. }
  46. void LoopPointReached(VideoPlayer vp)
  47. {
  48. playedOnce = !loop;
  49. }
  50. public override void PrepareFrame(Playable playable, FrameData info)
  51. {
  52. if (videoPlayer == null || videoClip == null)
  53. return;
  54. videoPlayer.timeReference = Application.isPlaying ? VideoTimeReference.ExternalTime :
  55. VideoTimeReference.Freerun;
  56. if (videoPlayer.isPlaying && Application.isPlaying)
  57. videoPlayer.externalReferenceTime = playable.GetTime();
  58. else if (!Application.isPlaying)
  59. SyncVideoToPlayable(playable);
  60. }
  61. public override void OnBehaviourPlay(Playable playable, FrameData info)
  62. {
  63. if (videoPlayer == null)
  64. return;
  65. if (!playedOnce)
  66. {
  67. PlayVideo();
  68. SyncVideoToPlayable(playable);
  69. }
  70. }
  71. public override void OnBehaviourPause(Playable playable, FrameData info)
  72. {
  73. if (videoPlayer == null)
  74. return;
  75. if (Application.isPlaying)
  76. PauseVideo();
  77. else
  78. StopVideo();
  79. }
  80. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  81. {
  82. if (videoPlayer == null || videoPlayer.clip == null)
  83. return;
  84. videoPlayer.targetCameraAlpha = info.weight;
  85. if (Application.isPlaying)
  86. {
  87. for (ushort i = 0; i < videoPlayer.clip.audioTrackCount; ++i)
  88. {
  89. if (videoPlayer.audioOutputMode == VideoAudioOutputMode.Direct)
  90. videoPlayer.SetDirectAudioVolume(i, info.weight);
  91. else if (videoPlayer.audioOutputMode == VideoAudioOutputMode.AudioSource)
  92. {
  93. AudioSource audioSource = videoPlayer.GetTargetAudioSource(i);
  94. if (audioSource != null)
  95. audioSource.volume = info.weight;
  96. }
  97. }
  98. }
  99. }
  100. public override void OnGraphStart(Playable playable)
  101. {
  102. playedOnce = false;
  103. }
  104. public override void OnGraphStop(Playable playable)
  105. {
  106. if (!Application.isPlaying)
  107. StopVideo();
  108. }
  109. public override void OnPlayableDestroy(Playable playable)
  110. {
  111. StopVideo();
  112. }
  113. public void PlayVideo()
  114. {
  115. if (videoPlayer == null)
  116. return;
  117. videoPlayer.Play();
  118. preparing = false;
  119. if (!Application.isPlaying)
  120. PauseVideo();
  121. }
  122. public void PauseVideo()
  123. {
  124. if (videoPlayer == null)
  125. return;
  126. videoPlayer.Pause();
  127. preparing = false;
  128. }
  129. public void StopVideo()
  130. {
  131. if (videoPlayer == null)
  132. return;
  133. playedOnce = false;
  134. videoPlayer.Stop();
  135. preparing = false;
  136. }
  137. private void SyncVideoToPlayable(Playable playable)
  138. {
  139. if (videoPlayer == null || videoPlayer.clip == null)
  140. return;
  141. videoPlayer.time = (clipInTime + (playable.GetTime() * videoPlayer.playbackSpeed)) % videoPlayer.clip.length;
  142. }
  143. }
  144. }