Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

VideoPlayableBehaviour.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using UnityEngine.Playables;
  3. using UnityEngine.Video;
  4. namespace Timeline.Samples
  5. {
  6. // The runtime instance of a video clip player in Timeline.
  7. public sealed class VideoPlayableBehaviour : PlayableBehaviour
  8. {
  9. public VideoPlayer videoPlayer;
  10. public double preloadTime;
  11. public double clipInTime;
  12. public double startTime;
  13. private bool preparing;
  14. // Called by the mixer (VideoSchedulerPlayableBehaviour) when this is nearly active to
  15. // give the video time to load.
  16. public void PrepareVideo()
  17. {
  18. if (videoPlayer == null || videoPlayer.isPrepared || preparing)
  19. return;
  20. videoPlayer.targetCameraAlpha = 0.0f;
  21. videoPlayer.time = clipInTime;
  22. videoPlayer.Prepare();
  23. preparing = true;
  24. }
  25. // Called each frame the clip is active.
  26. //
  27. public override void PrepareFrame(Playable playable, FrameData info)
  28. {
  29. if (videoPlayer == null)
  30. return;
  31. // Pause or Play the video to match whether the graph is being scrubbed or playing
  32. // If we need to hold the last frame, this will treat the last frame as a pause
  33. bool shouldBePlaying = info.evaluationType == FrameData.EvaluationType.Playback;
  34. if (!videoPlayer.isLooping && playable.GetTime() >= videoPlayer.clip.length)
  35. shouldBePlaying = false;
  36. if (shouldBePlaying)
  37. {
  38. // this will use the timeline time to prevent drift
  39. videoPlayer.timeReference = VideoTimeReference.ExternalTime;
  40. if (!videoPlayer.isPlaying)
  41. videoPlayer.Play();
  42. videoPlayer.externalReferenceTime = playable.GetTime() / videoPlayer.playbackSpeed;
  43. }
  44. else
  45. {
  46. videoPlayer.timeReference = VideoTimeReference.Freerun;
  47. if (!videoPlayer.isPaused)
  48. videoPlayer.Pause();
  49. SyncVideoToPlayable(playable);
  50. }
  51. // use the accumulated blend value to set the alpha and the audio volume
  52. videoPlayer.targetCameraAlpha = info.effectiveWeight;
  53. if (videoPlayer.audioOutputMode == VideoAudioOutputMode.Direct)
  54. {
  55. for (ushort i = 0; i < videoPlayer.clip.audioTrackCount; ++i)
  56. videoPlayer.SetDirectAudioVolume(i, info.effectiveWeight);
  57. }
  58. }
  59. // Called when the clip becomes active.
  60. public override void OnBehaviourPlay(Playable playable, FrameData info)
  61. {
  62. if (videoPlayer == null)
  63. return;
  64. SyncVideoToPlayable(playable);
  65. videoPlayer.playbackSpeed = Mathf.Clamp(info.effectiveSpeed, 1 / 10f, 10f);
  66. videoPlayer.Play();
  67. preparing = false;
  68. }
  69. // Called when the clip becomes inactive OR the timeline is 'paused'
  70. public override void OnBehaviourPause(Playable playable, FrameData info)
  71. {
  72. if (videoPlayer == null)
  73. return;
  74. preparing = false;
  75. // The effective weight will be greater than 0 if the graph is paused and the playhead is still on this clip.
  76. if (info.effectiveWeight <= 0)
  77. videoPlayer.Stop();
  78. else
  79. videoPlayer.Pause();
  80. }
  81. // Called when the playable is destroyed.
  82. public override void OnPlayableDestroy(Playable playable)
  83. {
  84. if (videoPlayer != null)
  85. {
  86. videoPlayer.Stop();
  87. if (Application.isPlaying)
  88. Object.Destroy(videoPlayer.gameObject);
  89. else
  90. Object.DestroyImmediate(videoPlayer.gameObject);
  91. }
  92. }
  93. // Syncs the video player time to playable time
  94. private void SyncVideoToPlayable(Playable playable)
  95. {
  96. if (videoPlayer == null || videoPlayer.clip == null)
  97. return;
  98. if (videoPlayer.isLooping)
  99. videoPlayer.time = playable.GetTime() % videoPlayer.clip.length;
  100. else
  101. videoPlayer.time = System.Math.Min(playable.GetTime(), videoPlayer.clip.length);
  102. }
  103. }
  104. }