Unity Features 101: Video Player
Continuing our series (List: Unity Features), today we will touch a feature that some of you may have wanted to do but do not know how to in Unity.
In my occasions during your development process you want to play a video during runtime/gameplay. In able to do this, you will want to use Unity’s Video Player. There are many use cases for the video player; maybe a cutscene, or a tutorial, or even an in-game tv running a video.
Importing Your Video Clip and Video Settings
Unity supports almost all kinds of video formats. So firstly, drop your video clip into your Assets folder.
The most important setting is “Transcode”. When you check this setting, Unity will be able to re-encode the video clip depending your target platform. If for example, your making a game for the Playstation but your video clip format is not supported on the Playstation, so with Transcode on, Unity will re-encode your video clip to work on Playstation. The remaining settings are self explanatory.
Video Player Component
Create an empty game object in your scene and add the video player component to it.
First off start by adding your video clip to the empty Video Clip in the component. You can next check the settings and add Loop if you want.
The most important setting here is the Render Mode; how will the component render and play your video clip during gameplay.
Render Mode: Camera Far Plane / Near Plane
These two render modes are almost the default for playing full screen cutscenes. Choosing between near or far plane allow you to display anything in-front of the cutscene.
If you choose these render modes then the component will ask you to choose a camera. Drag your main camera into it and set the transparency alpha value if you want.
Press play to start gameplay and there you have it, a full screen cutscene.
Render Mode: Material Override
In this mode, the video player component will override the material of an object that has a renderer (mesh renderer) in order to display the video clip during gameplay.
- Add a Mesh Filter and a Mesh Renderer component to the video player game object. This will automatically add this renderer to the video player’s renderer mode.
2. Create a new material in your assets, call it VideoPlayerMaterial and drag to the material of the added Mesh Renderer.
3. After adding the material, the most important setting to change if needed is the Material Property in the video player component.
This will allow you to choose which property of the material you want to use to render the video clip. In my case, it was a Standard Material so keeping it to _mainTex should do.
4. Finally, in the mesh filter choose a Mesh. I will choose a Quad to display the video clip on. Now press Play, and yes, the video clip is now playing directly on the Quad.
A quick tip: If the video clip seems disproportional, then it has to do with the video clip’s aspect ration. Simply adjust the video player game object scale to match the aspect ratio of the clip and then the video will look nice and smooth.
Render Mode: Render Texture
We have covered render textures in Unity in a previous (Unity features 101: Render Textures), so let’s use them here to display a video clip.
- Create a Render Texture in your Assets and drag it into your video player component.
2. Leave the mesh filter and the mesh renderer on your video player game object and now drag the newly created render texture into the video player material. In my case, it was the albedo of the video player material.
That is basically it, now press play and it should player normally on the quad rendering on a render texture instead.
Note here that the aspect ratio of the video being rendered can be adjust directly from the render texture size setting. Using the Render Texture to render a video clip is very powerful since it is a texture. So you can use it to play it on an in-game tv or create a shader for it or even manipulate it through code.
As we saw in this article, it is fairly easy to play any video clip you want in Unity, to display cutscenes or even a tutorial.