You can import many video file formats into Unity. Unity stores imported video files as VideoClip assets.
For Unity to preview video files, the files must be compatible with the platform where you run the Unity Editor. Your files must also be compatible with the target build platforms. Unity provides options for transcoding files to commonly supported formats, but you can also manage compatibility yourself. See Compatibility with target platforms below for more information.
Video source files must use a format that your Editor platform supports. Each platform supports different video file formats.
扩展名 | Windows | macOS | Linux |
---|---|---|---|
.asf | ✓ | ||
.avi | ✓ | ||
.dv | ✓ | ✓ | |
.m4v | ✓ | ✓ | |
.mov | ✓ | ✓ | |
.mp4 | ✓ | ✓ | |
.mpg | ✓ | ✓ | |
.mpeg | ✓ | ✓ | |
.ogv | ✓ | ✓ | ✓ |
.vp8 | ✓ | ✓ | ✓ |
.webm | ✓ | ✓ | ✓ |
.wmv | ✓ |
Encoding for video file tracks must use a supported codec. Each platform supports specific codecs that can change with each version of the platform.
Video files that use unsupported codecs trigger an error message in the Editor and you must convert them to a compatible codec before you can use them.
H.264 is the optimal supported video codec for most platforms. It offers the best cross-platform compatibility, but the Linux Editor doesn’t support this codec.
For Linux, the optimal encoding is a .webm container with the following codecs:
For information about codec support, see the official platform documentation for your Editor. See the links below for codec compatibility for Windows and macOS:
The Video Clip Importer can transcode video files that you import into Unity. This is useful when your videos use a codec that the Editor platform supports, but the files aren’t compatible with your target platform.
To use video files that are compatible with your target platforms, but not your Editor platform, set them up as streaming assets. For example, you might want to use the VP9 codec for an Android build, even though the Editor only supports VP8.
To set video files up as streaming assets, place them in the StreamingAssets
folder of your project. Use the URL property to point the Video Player component to streaming assets.
You can also use Application.streamingAssetsPath to access platform-specific paths to streaming assets via scripts. You can’t preview these paths in the Editor.
If you want to have placeholder versions that are compatible with your Editor but use a different version for your target platform, you can include both versions in your project, use Editor-compatible versions as placeholders, and decide which version to use at run time.
以下示例演示了如何针对不同平台使用不同的视频 URL。如需了解更多信息,请参阅平台相关的编译的文档。
void SetupMovieFile(VideoPlayer vp)
{
# if UNITY_EDITOR || UNITY_LINUX
vp.url = pathToMyVp8File;
# elif UNITY_ANDROID
vp.url = pathToMyVp9File;
# elif UNITY_STANDALONE_WIN
vp.url = pathToMyWmvFile;
# else
vp.url = pathToMyMp4File;
# endif
}
The Video Player component uses the native audio and video decoding libraries of your Editor platform to play video files in the Editor. You must confirm that the files also meet the requirements for the target platform.
可用于硬件加速并且本机支持的最优视频编解码器是 H.264
When cross-platform support is a high priority, VP8 is a good choice. It’s widely supported and has a comprehensive feature set, but it consumes more resources than hardware-accelerated codecs such as H.264.
Android supports VP8 using native libraries, so VP8 might also be hardware-assisted on some Android devices.
The Unity Editor supports the .ogv format, but it isn’t widely supported on other platforms. Transcode .ogv files into .mp4 (H.264) or .webm (VP8) depending on the target platform.
The table below lists key values to look for in your encoding parameters: |参数 | 描述 | |:—|:—| |Video Codec| H.264, H.265 or VP8.| |Resolution|The resolution of your display. For example: 1280 × 720.| |Profile| The profile is a set of capabilities and constraints, often specified by the vendor, such as Baseline or Main. Applies to H.264/H.265.See H.264 or H.265. |Profile Level| Applies for H.264/H.265. Within a given profile, the level specifies performance requirements, for example, Baseline 3.1.| |Audio Codec| AAC (for mp4 videos using H.264/H.265) or Vorbis (for webm videos using VP8).| |Audio Channels| Dependent on the platform. Refer to the developer guide for your platform. For example, the article on Supported media formats for Android.
视频剪辑导入器提供了将视频剪辑资源转码为以下视频编解码器之一的选项:
转码后的视频剪辑自动使用适当的音频编解码器:
注意: |
---|
The Video Clip Importer provides only basic transcoding options. Depending on how your source files are encoded, you might not get optimal performance from clips transcoded with the importer options. In that case, you might get better results with an external transcoding program. |
If you use videos that the target system definitely supports, you can leave the Video Clip Importer’s transcoding options disabled. If disabled, Unity doesn’t modify the files. You can manage encoding with an external program, which allows for finer control.
This section provides useful information about video compatibility, and links to external resources.
Follow vendor recommendations for codec support. On older mobile platforms, codec choices are limited . You might need to inspect and convert or re-encode videos that you intend to include in a game running on multiple devices.
The table below provides platform-specific requirements and information for the H.265 codec.
平台 | Requirements | Encoding/Decoding | 注意 |
---|---|---|---|
macOS | SDK 10.13+ |
硬件编码:第 6 代 Intel Core 处理器 软件编码:所有 Mac 机型 硬件解码:第 6 代 Intel Core 处理器 软件解码:所有 Mac 机型 |
|
Windows | Windows 10 + HEVC 扩展 |
编码器 解码器 |
HEVC 扩展(仅硬件) HEVC 扩展(硬件 + 软件支持) |
iOS | SDK 11.0+ |
硬件解码:A9 芯片 软件解码:所有 iOS 设备 |
|
tvOS | SDK 11.0+ | ||
UWP | Windows 10+ | If a device lists support for H.265, that might not apply to all devices within the device family. | |
Android | 5.0+ |
2019–05–07 页面已修订
Unity 5.6 中的新功能