XpertLab - Website Designing Company Junagadh,Website Development Company Junagadh, Software Development Company Junagadh, Professional IT Project Training Junagadh

How to Play a Video in Unity

7-Tips-to-Develop-a-Successful-Ecommerce-Store-XpertLab
7 Tips to Develop a Successful Ecommerce Store
25th September 2017
react-native-XpertLab
An Introduction to React Native
21st April 2018
Show all

How to Play a Video in Unity

XpertLab-Unity-Video-Tutorial-Blog-Banner

Sometimes, being able to play a video file in your game is something that needs to be done. Fortunately, Unity provides a component and some libraries to help you do just that. In this post, I will talk about the Video Player component and some of the requirements you need to meet so that Unity can play your video in-game.

What is a Unity Video Player?

unity-video-tutorial

unity-video-tutorialUnity has a component called a Video Player. This component can be attached to any Game Object in your scene. It allows you to assign a video file to it for playback within your game. The Video player can be rendered to a camera’s near or far planes. Additionally, it can render itself to a RenderTexture, which can be attached to Game Objects in your scene as a material. This allows you to create an object in your scene that will play a movie on it. The video player also gives you playback options, such as the ability to play a clip on awake, or to loop the clip.

Unity Tutorial Video

Supported Codecs

The Unity Video Player supports many common video file types. However, since Unity supports a variety of build platforms, it can be a task to figure out what codec you should use for your target build platform.

First, it’s important to know that the Video Player will also output the video’s sound to a sound mixer in your game. This means that your video has both audio and video codecs that your target platform must be able to read. Of course, if your video clip has no sound, then the sound codec doesn’t really matter.

By far, the most widely supported video codec is going to be H.264. This codec is supported by many older phones and most, if not all, modern phones. It is also compatible with Windows and Mac OS X. The VP8 video codec is also widely supported by most devices.

The most widely supported audio codec seems to be AAC. Vorbis is also usable, but AAC seems to have the most support.

Video Resolution

In my short experience using the Video Player in Unity, video resolution was my biggest issue. I often make all my videos in 1920 x 1080 (1080p) resolution. When using a 1080p video on my desktop, I have no issues seeing the content. However, when I try building to an old Galaxy S2 Android phone, the video will not show up.

After reading Unity’s documentation, they make it clear that Android 4.1/4.2 cannot play anything above 720p. Once I changed my 1080p video down to a 480p resolution, I had no issues with the video playing on my Galaxy S2.

This highlights a tedious problem; with the large variety of devices that are on the market, it can be difficult to create video assets that will run on every possible device. Of course, this issue isn’t exclusive to video files. One way to work with the variety of hardware on the market is to use preprocessor directives to execute code depending on the platform that your game is being run on. For systems like Android, where you may have devices running any number of OS versions, you can use Unity’s SystemInfo.operatingSystem function to get the device’s OS version.

The power of these approaches is you can identify what your platform is and the version of the OS that it’s running. This would allow you to make some simple logic like

If target device version <= 4.1.2 then load 720p video clip

else if target device version >= 6.0 then load 1080p video clip.

If you want more information about supported video and audio codecs and resolutions for Android, look at their supported media formats page.

For the modern iPhone hardware, you can look at their specs page here.

Conclusion

Hopefully, this post has given you some valuable information for implementing videos in your game. If you follow the guidelines for your target platform, you shouldn’t have any issue implementing your videos in your game. Good luck!