Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VideoPlayableAsset.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. using UnityEngine.Video;
  6. namespace Timeline.Samples
  7. {
  8. // Editor representation of a Clip to play video in Timeline.
  9. [Serializable]
  10. public class VideoPlayableAsset : PlayableAsset, ITimelineClipAsset
  11. {
  12. public enum RenderMode
  13. {
  14. CameraFarPlane,
  15. CameraNearPlane
  16. };
  17. [Tooltip("The video clip to play.")]
  18. public VideoClip videoClip;
  19. [Tooltip("Mutes the audio from the video")]
  20. public bool mute;
  21. [Tooltip("Loops the video.")]
  22. public bool loop = true;
  23. [Tooltip("The amount of time before the video begins to start preloading the video stream.")]
  24. public double preloadTime = 0.3;
  25. [Tooltip("The aspect ratio of the video to playback.")]
  26. public VideoAspectRatio aspectRatio = VideoAspectRatio.FitHorizontally;
  27. [Tooltip("Where the video content will be drawn.")]
  28. public RenderMode renderMode = RenderMode.CameraFarPlane;
  29. [Tooltip("Specifies which camera to render to. If unassigned, the main camera will be used.")]
  30. public ExposedReference<Camera> targetCamera;
  31. [Tooltip("Specifies an optional audio source to output to.")]
  32. public ExposedReference<AudioSource> audioSource;
  33. // These are set by the track prior to CreatePlayable being called and are used by the VideoSchedulePlayableBehaviour
  34. // to schedule preloading of the video clip
  35. public double clipInTime { get; set; }
  36. public double startTime { get; set; }
  37. // Creates the playable that represents the instance that plays this clip.
  38. // Here a hidden VideoPlayer is being created for the PlayableBehaviour to use
  39. // to control playback. The PlayableBehaviour is responsible for deleting the player.
  40. public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
  41. {
  42. Camera camera = targetCamera.Resolve(graph.GetResolver());
  43. if (camera == null)
  44. camera = Camera.main;
  45. // If we are unable to create a player, return a playable with no behaviour attached.
  46. VideoPlayer player = CreateVideoPlayer(camera, audioSource.Resolve(graph.GetResolver()));
  47. if (player == null)
  48. return Playable.Create(graph);
  49. ScriptPlayable<VideoPlayableBehaviour> playable =
  50. ScriptPlayable<VideoPlayableBehaviour>.Create(graph);
  51. VideoPlayableBehaviour playableBehaviour = playable.GetBehaviour();
  52. playableBehaviour.videoPlayer = player;
  53. playableBehaviour.preloadTime = preloadTime;
  54. playableBehaviour.clipInTime = clipInTime;
  55. playableBehaviour.startTime = startTime;
  56. return playable;
  57. }
  58. // The playable assets duration is used to specify the initial or default duration of the clip in Timeline.
  59. public override double duration
  60. {
  61. get
  62. {
  63. if (videoClip == null)
  64. return base.duration;
  65. return videoClip.length;
  66. }
  67. }
  68. // Implementation of ITimelineClipAsset. This specifies the capabilities of this timeline clip inside the editor.
  69. // For video clips, we are using built-in support for clip-in, speed, blending and looping.
  70. public ClipCaps clipCaps
  71. {
  72. get
  73. {
  74. var caps = ClipCaps.Blending | ClipCaps.ClipIn | ClipCaps.SpeedMultiplier;
  75. if (loop)
  76. caps |= ClipCaps.Looping;
  77. return caps;
  78. }
  79. }
  80. VideoPlayer CreateVideoPlayer(Camera camera, AudioSource targetAudioSource)
  81. {
  82. if (videoClip == null)
  83. return null;
  84. GameObject gameObject = new GameObject(videoClip.name) { hideFlags = HideFlags.HideAndDontSave };
  85. VideoPlayer videoPlayer = gameObject.AddComponent<VideoPlayer>();
  86. videoPlayer.playOnAwake = false;
  87. videoPlayer.source = VideoSource.VideoClip;
  88. videoPlayer.clip = videoClip;
  89. videoPlayer.waitForFirstFrame = false;
  90. videoPlayer.skipOnDrop = true;
  91. videoPlayer.targetCamera = camera;
  92. videoPlayer.renderMode = renderMode == RenderMode.CameraFarPlane ? VideoRenderMode.CameraFarPlane : VideoRenderMode.CameraNearPlane;
  93. videoPlayer.aspectRatio = aspectRatio;
  94. videoPlayer.isLooping = loop;
  95. videoPlayer.audioOutputMode = VideoAudioOutputMode.Direct;
  96. if (mute)
  97. {
  98. videoPlayer.audioOutputMode = VideoAudioOutputMode.None;
  99. }
  100. else if (targetAudioSource != null)
  101. {
  102. videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
  103. for (ushort i = 0; i < videoPlayer.clip.audioTrackCount; ++i)
  104. videoPlayer.SetTargetAudioSource(i, targetAudioSource);
  105. }
  106. return videoPlayer;
  107. }
  108. }
  109. }