将视频内容播放到目标上。
内容可以是导入的 VideoClip 资源或 URL,例如 file:// 或 http://。视频内容将投射到受支持的目标之一,例如摄像机背景或 RenderTexture。如果视频内容包含透明度,则此透明度将存在于目标中,允许视频目标后面的对象可见。当数据 VideoPlayer.source 设置为 URL 时,播放内容的音频和视频描述仅在 VideoPlayer 准备完成后初始化。您可以使用 VideoPlayer.isPrepared 测试这一点。
有关支持的视频文件格式的更多信息,请参阅 视频文件兼容性。
以下演示了 VideoPlayer 的一些功能
// Examples of VideoPlayer function
using UnityEngine; using UnityEngine.Video;
public class Example : MonoBehaviour { void Start() { // Will attach a VideoPlayer to the main camera. GameObject camera = GameObject.Find("Main Camera");
// VideoPlayer automatically targets the camera backplane when it is added // to a camera object, no need to change videoPlayer.targetCamera. var videoPlayer = camera.AddComponent<UnityEngine.Video.VideoPlayer>();
// Play on awake defaults to true. Set it to false to avoid the url set // below to auto-start playback since we're in Start(). videoPlayer.playOnAwake = false;
// By default, VideoPlayers added to a camera will use the far plane. // Let's target the near plane instead. videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
// This will cause our Scene to be visible through the video being played. videoPlayer.targetCameraAlpha = 0.5F;
// Set the video to play. URL supports local absolute or relative paths. // Here, using absolute. videoPlayer.url = "/Users/graham/movie.mov";
// Skip the first 100 frames. videoPlayer.frame = 100;
// Restart from beginning when done. videoPlayer.isLooping = true;
// Each time we reach the end, we slow down the playback by a factor of 10. videoPlayer.loopPointReached += EndReached;
// Start playback. This means the VideoPlayer may have to prepare (reserve // resources, pre-load a few frames, etc.). To better control the delays // associated with this preparation one can use videoPlayer.Prepare() along with // its prepareCompleted event. videoPlayer.Play(); }
void EndReached(UnityEngine.Video.VideoPlayer vp) { vp.playbackSpeed = vp.playbackSpeed / 10.0F; } }
| controlledAudioTrackMaxCount | 可以控制的音频轨道的最大数量。(只读) |
| aspectRatio | 定义如何拉伸视频内容以填充目标区域。 |
| audioOutputMode | 视频中嵌入的音频的目标位置。 |
| audioTrackCount | 当前配置的数据源中找到的音频轨道的数量。(只读) |
| canSetDirectAudioVolume | 当前平台和视频格式是否支持直接输出音量控制。(只读) |
| canSetPlaybackSpeed | 是否可以更改播放速度。(只读) |
| canSetSkipOnDrop | 是否可以控制跳帧以保持同步。(只读) |
| canSetTime | 是否可以使用 VideoPlayer.time 或 VideoPlayer.frame 属性更改当前时间。(只读) |
| canSetTimeUpdateMode | 是否可以更改 VideoPlayer 跟踪的时间源。(只读) |
| canStep | 如果 VideoPlayer 可以向前逐步浏览视频内容,则返回 true。(只读) |
| clip | VideoPlayer 正在播放的剪辑。 |
| clockTime | VideoPlayer 用于安排其样本的时钟时间。时钟时间以秒为单位。(只读) |
| controlledAudioTrackCount | 此 VideoPlayer 将控制的音频轨道的数量。 |
| externalReferenceTime | VideoPlayer 用于校正其漂移的外部时钟的参考时间。 |
| frame | VideoPlayer.texture 中当前可用帧的帧索引。 |
| frameCount | 当前视频内容中的帧数。(只读) |
| frameRate | 剪辑或 URL 的帧率,以帧/秒为单位。(只读) |
| height | VideoClip 或 URL 中图像的高度(以像素为单位)。(只读) |
| isLooping | 确定当 VideoPlayer 达到剪辑末尾时是否从开头重新开始。 |
| isPaused | 播放是否已暂停。(只读) |
| isPlaying | 内容是否正在播放。(只读) |
| isPrepared | VideoPlayer 是否已成功准备播放内容。(只读) |
| length | VideoClip 或 URL 的长度(以秒为单位)。(只读) |
| pixelAspectRatioDenominator | VideoClip 或 URL 的像素纵横比 (num:den) 的分母。(只读) |
| pixelAspectRatioNumerator | VideoClip 或 URL 的像素纵横比 (num:den) 的分子。(只读) |
| playbackSpeed | 基本播放速率将乘以的因子。 |
| playOnAwake | 组件唤醒后内容是否立即开始播放。 |
| renderMode | 视频内容将在哪里绘制。 |
| sendFrameReadyEvents | 启用 frameReady 事件。 |
| skipOnDrop | VideoPlayer 是否允许跳过帧以赶上当前时间。 |
| source | VideoPlayer 用于播放的源。 |
| targetCamera | 当 VideoPlayer.renderMode 设置为 VideoRenderMode.CameraFarPlane 或 VideoRenderMode.CameraNearPlane 时要绘制到的摄像机组件。 |
| targetCamera3DLayout | 源视频媒体中包含的 3D 内容的类型。 |
| targetCameraAlpha | 目标摄像机平面视频的整体透明度级别。 |
| targetMaterialProperty | 当 VideoPlayer.renderMode 设置为 Video.VideoTarget.MaterialOverride 时,目标材质纹理属性。 |
| targetMaterialRenderer | 当 VideoPlayer.renderMode 设置为 Video.VideoTarget.MaterialOverride 时,目标渲染器。 |
| targetTexture | 当 VideoPlayer.renderMode 设置为 Video.VideoTarget.RenderTexture 时,要绘制到的 RenderTexture。 |
| texture | 放置视频内容的内部纹理。(只读) |
| time | VideoPlayer.texture 中当前可用帧的呈现时间。 |
| timeReference | VideoPlayer 观察以检测和校正漂移的时钟。 |
| timeUpdateMode | VideoPlayer 用于派生其当前时间的时钟源。 |
| url | VideoPlayer 从中读取内容的文件或 HTTP URL。 |
| waitForFirstFrame | 确定当 VideoPlayer.playOnAwake 为开启状态时,VideoPlayer 是否会在第一帧加载到纹理中之前等待。 |
| width | VideoClip 或 URL 中图像的宽度(以像素为单位)。(只读) |
| EnableAudioTrack | 启用/禁用音频轨道解码。仅在 VideoPlayer 当前未播放时有效。 |
| GetAudioChannelCount | 指定音频轨道中的音频通道数。 |
| GetAudioLanguageCode | 返回指定轨道的语言代码(如果存在)。 |
| GetAudioSampleRate | 获取音频轨道的采样率(以赫兹为单位)。 |
| GetDirectAudioMute | 获取指定轨道的直接输出音频静音状态。 |
| GetDirectAudioVolume | 返回指定轨道的直接输出音量。 |
| GetTargetAudioSource | 获取将接收指定轨道音频样本的 AudioSource,如果 VideoPlayer.audioOutputMode 设置为 VideoAudioOutputMode.AudioSource。 |
| IsAudioTrackEnabled | 指定音频轨道的解码是否已启用。请参阅 VideoPlayer.EnableAudioTrack 以区分静音。 |
| Pause | 暂停播放并保持当前时间不变。 |
| Play | 开始播放。 |
| Prepare | 启动播放引擎准备。 |
| SetDirectAudioMute | 设置指定轨道的直接输出音频静音状态。 |
| SetDirectAudioVolume | 设置指定轨道的直接输出音频音量。 |
| SetTargetAudioSource | 设置将接收指定轨道音频样本的 AudioSource,如果使用 VideoPlayer.audioOutputMode 选择此音频目标。 |
| StepForward | 立即将当前时间提前一帧。 |
| Stop | 停止播放并将当前时间设置为 0。 |
| clockResyncOccurred | 当 VideoPlayer 时钟同步回其 VideoTimeReference 时调用。 |
| errorReceived | HTTP 连接问题等错误通过此回调报告。 |
| frameDropped | [尚未实现] 当视频解码器在播放期间未根据时间源生成帧时调用。 |
| frameReady | 准备就绪新帧时调用。 |
| loopPointReached | 当 VideoPlayer 达到要播放内容的末尾时调用。 |
| prepareCompleted | VideoPlayer 准备完成后调用。 |
| seekCompleted | 查找操作完成后调用。 |
| started | 调用 Play 后立即调用。 |
| ErrorEventHandler | 包含错误消息的 VideoPlayer 事件的委托类型。 |
| EventHandler | VideoPlayer 发出的所有无参数事件的委托类型。 |
| FrameReadyEventHandler | 承载帧号的 VideoPlayer 事件的委托类型。 |
| TimeEventHandler | 承载时间位置的 VideoPlayer 事件的委托类型。 |
| enabled | 启用的行为将更新,禁用的行为将不会更新。 |
| isActiveAndEnabled | 报告 GameObject 及其关联的行为是否处于活动状态并已启用。 |
| gameObject | 此组件所附加到的游戏对象。组件始终附加到游戏对象。 |
| tag | 此游戏对象的标签。 |
| transform | 附加到此 GameObject 的 Transform。 |
| hideFlags | 对象是否应隐藏、与场景一起保存或用户可修改? |
| name | 对象的名称。 |
| BroadcastMessage | 在此游戏对象或其任何子对象中的每个 MonoBehaviour 上调用名为 methodName 的方法。 |
| CompareTag | 将 GameObject 的标签与定义的标签进行比较。 |
| GetComponent | 获取与指定组件相同的 GameObject 上类型为 T 的组件的引用。 |
| GetComponentInChildren | 获取与指定组件相同的 GameObject 或任何子对象上类型为 T 的组件的引用。 |
| GetComponentIndex | 获取组件在其父 GameObject 上的索引。 |
| GetComponentInParent | 获取与指定组件相同的 GameObject 或任何父对象上类型为 T 的组件的引用。 |
| GetComponents | 获取与指定组件相同的 GameObject 上所有类型为 T 的组件的引用。 |
| GetComponentsInChildren | 获取与指定组件相同的 GameObject 及其任何子对象上所有类型为 T 的组件的引用。 |
| GetComponentsInParent | 获取与指定组件相同的 GameObject 及其任何父对象上所有类型为 T 的组件的引用。 |
| SendMessage | 在此游戏对象中的每个 MonoBehaviour 上调用名为 methodName 的方法。 |
| SendMessageUpwards | 在此游戏对象和行为的每个祖先上调用名为 methodName 的方法。 |
| TryGetComponent | 获取指定类型的组件(如果存在)。 |
| GetInstanceID | 获取对象的实例 ID。 |
| ToString | 返回对象的名称。 |
| Destroy | 移除 GameObject、组件或资源。 |
| DestroyImmediate | 立即销毁对象 obj。强烈建议使用 Destroy 代替。 |
| DontDestroyOnLoad | 加载新场景时不要销毁目标对象。 |
| FindAnyObjectByType | 检索类型为 type 的任何活动加载对象。 |
| FindFirstObjectByType | 检索类型为 type 的第一个活动加载对象。 |
| FindObjectsByType | 检索所有已加载的 Type 类型对象列表。 |
| Instantiate | 克隆对象 original 并返回克隆体。 |
| InstantiateAsync | 捕获原始对象(必须与某个 GameObject 相关联)的快照,并返回 AsyncInstantiateOperation。 |
| bool | 对象是否存在? |
| operator != | 比较两个对象是否引用不同的对象。 |
| operator == | 比较两个对象引用是否指向同一个对象。 |