You can import many different formats of video file into Unity. Unity stores imported video files as VideoClip assets.
For Unity to preview video files properly, they must be compatible with the Editor platform (the platform you run Unity on). You must also ensure that your files are compatible with the target platforms that your project runs on when you build it. 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 your Editor platform supports. Each platform supports a different subset of video file formats.
扩展名 | Windows | OSX | Linux |
---|---|---|---|
.asf | ✓ | ||
.avi | ✓ | ||
.dv | ✓ | ✓ | |
.m4v | ✓ | ✓ | |
.mov | ✓ | ✓ | |
.mp4 | ✓ | ✓ | |
.mpg | ✓ | ✓ | |
.mpeg | ✓ | ✓ | |
.ogv | ✓ | ✓ | ✓ |
.vp8 | ✓ | ✓ | ✓ |
.webm | ✓ | ✓ | ✓ |
.wmv | ✓ |
Video file tracks must also be encoded using a supported codec. Each platform supports a specific subset of codecs, which can vary from one version of the platform to the next.
Tracks encoded using unsupported codecs trigger an error message in the Editor, and you have to convert/re-encode them using a compatible codec before you can use them.
H.264 (typically in a .mp4, .m4v, or .mov format) is the optimal supported video codec because it offers the best compatibility across platforms.
For information about codec support, consult the official documentation for your Editor platform. For example, Windows and macOS both provide codec compatibility documentation:
The Video Clip Importer can transcode video files that you import into Unity. This is useful when your video sources use a codec that your Editor platform supports, but your target platform does not (see Compatibility with target platforms, below).
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 your Project’s StreamingAssets
folder. To point the Video Player component to Streaming Assets, use the URL property. You can also use Application.streamingAssetsPath to access platform-specific paths to Streaming Assets via scripts.
When you use video files this way, you cannot preview them in the Editor. However, you can create Editor-compatible versions to use as placeholders, include both the compatible- and incompatible version in your project, and decide which one to use at run time.
The example below demonstrates how to use different video URLs for different platforms. For more information, see the documentation on Platform-dependent compilation.
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 VideoPlayer component uses your Editor platform’s native audio and video decoding libraries (see Compatibility on Editor platforms, above) to play video files in the Editor. It’s up to you to ensure that those files meet the requirements for the target platform.
The best natively supported video codec for hardware acceleration is H.264
VP8 is a good choice when cross-platform support is a high priority. It is widely supported and has a comprehensive feature set, but consumes more resources than hardware-accelerated codecs such as H.264.
H.265 is available on devices that support it. See H.265 Compatibility below, for more information.
Android supports VP8 using native libraries, so VP8 may also be hardware-assisted on some Android devices.
Below are some key values to look for in your encoding parameters:
Video Codec: H.264, H.265 or VP8.
Resolution: For example: 1280 x 720.
Profile: Applies to H.264/H.265. The profile is a set of capabilities and constraints, often specified by the vendor, such as Baseline or Main. 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. See H.264 or H.265.
Audio Codec: Typically AAC (for mp4 videos using H.264/H.265) or Vorbis (for webm videos using VP8).
Audio Channels: depends on platform. For example, the Android recommendation is for stereo files, but many devices will accept 5.1.
The Video Clip Importer provides the option to transcode VideoClip assets into one of the following video codecs:
Transcoded VideoClips use the appropriate audio codec automatically:
Note: |
---|
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 using the importer options. In that case, you might get better results using an external transcoding program. |
If you use videos that you know the target system supports, you can leave the Video Clip Importer’s transcoding options disabled. In that case, Unity does not modify the files at all, and uses them as-is. You can manage encoding using an external program, which allows for finer control.
This section provides useful information about video compatibility, as well as links to external resources.
It is important to follow vendor recommendations for codec support. Codec choices are especially constrained on older mobile platforms. You may need to carefully inspect and convert/re-encode videos that you intend to include in a game running on multiple devices.
This section covers some known issues on specific platforms.
Not all devices support resolutions greater than 640 x 360. Runtime checks are performed to verify this and failures will cause the movie to not be played.
For Android Lollipop (5.0 and 5.1.1) and above, you can use any resolution or number of audio channels, provided the target device supports them.
Format compatibility issues are reported in the adb logcat
output and are always prefixed with AndroidVideoMedia
.
Pay attention to device-specific error messages located near Unity’s error messages: they are not available to the engine, but often explain what the compatibility issue is.
Playback from asset bundles is only supported for uncompressed bundles, read directly from disk.
Native webM/VP8 transparency is not supported. You must transcode VP8-encoded webM clips for transparency to function on Android.
The table below provides platform-specific requirements and encoding/decoding information for the H.265 codec.
Platform: | Requirements: | Encoding/Decoding: | Notes |
---|---|---|---|
OSX | SDK 10.13+ |
Hardware encoding: 6th Generation Intel Core processor Software encoding: All Macs Hardware decoding: 6th Generation Intel Core processor Software decoding: All Macs |
|
Windows | Windows 10 + HEVC extensions |
Encoder Decoder |
HEVC extension (Hardware only) HEVC extension (Hardware + software support) |
iOS | SDK 11.0+ |
Hardware decoding: A9 Chip Software decoding: All iOS Devices |
|
tvOS | SDK 11.0+ | ||
XBox | See here | ||
UWP | Windows 10 + See here | Where H.265 support is indicated, it is not necessarily supported by all devices within the device family. | |
Android | 5.0+ See here |
2019–05–07 Page amended with editorial review
Unity 5.6 中的新功能