Skip to main content

Making YouTube videos with Unity

Unity is a game engine for creating 2D and 3D games. It can also be used for simulations or machine learning, or even as a general cross-platform app framework. But, did you know it works really well as an all-in-one video editor? And I actually prefer it over other professional video editors. Let me explain why.

In some places I may assume you are familiar with Unity. Also, Unity should be an obvious choice for 3D rendered films/video (just see the work produced by Oats Studio). But from now on I will focus on producing 2D videos and treat it as an alternative to the likes of Final Cut, Premier or free alternatives like HitFilm Express and VSDC.

To showcase what can be achieved with Unity, watch this video. (To get your hands on the source project, keep reading)

The Obvious Benefits

I'll go into technical details in the next section, but first I want to explain why this is a good idea. Maybe these points will convince you:

  • Unity has a clean, easy to use interface. There are loads of beginner friendly tutorials available and a huge community.
  • It has a great asset workflow. Think of assets as your library of content to place in your video. You can use fonts, images, videos, animations, and so much more (scripts, custom shaders, 3D content, ...)
  • You can create prefabs to reuse the same effects multiple times without recreating them.
  • It has exceptional animation features with the new Timeline window which supports scrubbing (providing you set things up properly).
  • Amazing UI and 2d engines, you can even add 2D physics or particle systems.
  • The post processing stack works well. It won't have all the features of Final Cut Pro or Adobe Premier, but it is fairly complete.
  • Scripting engine through which you can achieve literally anything. You probably won't need it if you use Timeline, but it is still there for the advanced users.
  • The Unity Asset Store has alternatives and extensions for everything. Don't like Unity's UI engine ... try installing NGUI. Want nice particle effects ... just search for them. Need more post processing filters ... you can bet someone has made what you are looking for.
  • Unity is free* and works on Windows, Mac and even Linux.

GitHub Repository - Project Template

Using Unity as a video editor out-of-the-box isn't great. It can take a long time to organise your project, install all the right packages, and along the way you will probably find bugs or incompatibilites. But, I've gone ahead and done all the groundwork for you. So rather than starting from an emtpy project you should the following project which includes the sample video source:

Unity Video Editor Template GitHub Repository

It includes a Welcome Window to help organise your window layout for video editing and set the output video resolution:

Now I'll go over some of the tools and packages you'll need to be familiar with and explain how they all fit together in the template.

How can Unity export video?

Unity can export video through the 'Unity Recorder' which is available through the Unity Package Manager. It works from Within the Unity Editor, you don't need to do any builds!

Note: At the time of writing the 'Unity Recorder' package is in alpha. It is working perfectly but you might wish to update it later on.

To set the resolution of the output video, simply set the resolution of the 'Game View'.

Unity Recorder can be used through it's editor window, but for Video Editing it is more convenient to use it through Timeline. In the template project, you can find an example by looking at the 'Global Timeline' object through the Timeline window. The recorder can be added as a clip on a track:

To actually record the video, you will need to enter Playmode and wait until the end of the recorder clip is reached. The output video will be in a 'Recordings' folder in your project directory.

Tip: Leave a few seconds before the recording clip starts. This gives video tracks enough time to initialize properly.

To adjust the other properties of the output video (such as frame rate), select the recorder clip and edit the settings in the Inspector window.

For more information on Unity Recorder, read the documentation here. For use with Timeline read this.

What if Unity's framerate fluctuates or goes below the required video framerate?

This is a valid question. Unity is a game engine so attempts to push out as many frames as possible, as fast as possible. This means the framerate depends on your system's capabilities. Low end machines might only get 10 frames a second when high end could be over 200fps. For a YouTube video you might want to record at 30 or 60fps so you may think your Unity app needs to run at this framerate or higher to avoid dropping frames. But you are wrong!

The Unity Recorder package has got you covered. It can force Unity to render at a constant framerate you need for your video. If you computer is less powerful, the frames just take longer to render but the output it still perfect. You don't lose any quality either way.

Combining videos

Ok ... so you can export videos from Unity very easily. But one important feature of video editors is that you can use existing videos and combine them to make your final video. Can Unity do that?

Yes. Unity has their VideoPlayer component which you can use to play videos you already have. You can even render videos onto 3d objects if you want to (using RenderTextures). However, Unity's VideoPlayer is not compatible out-of-the-box with Unity Recorder. In the sample project I have added fixes for this. (These fixes assume you are using Timeline and playing videos on UI RawImage components, but you can edit the scripts yourself to change this).

The scripts for Timeline support come from the Default Playables package on the Unity Asset Store. But they have been heavily modified so don't attempt to update the package!

To add a video clip first add the video file to the 'Clips' folder. In the import settings make sure 'Transcode' is checked, to avoid any delays during recording. Then create a UI RawImage in your scene where you want the video to be.

Next add a 'Video Script Playable Track' to your Timeline. And right click on the track to add a 'Video Script Playable Asset':

Then in the Inspector window you can select the Video Clip to be played, the RawImage to play it on, looping and audio settings as well as cropping some seconds off the start and end of the Clip if you want to:

You do not need to create a VideoPlayer component or RenderTexture. This is handled for you behind the scenes.

Note: Outside of the Video Clip's time interval in Timeline, the first frame of the video will be shown in its place. If looping is disabled then the last frame will be shown when the video finishes (through to the clip end time).

Tip: If you wish to display the video on a 2D or 3D surface, simply edit the files in 'Assets/VideoEditorAssets/ModifiedDefaultPlayables/Video' and replace the 'RawImage' with a 'Renderer'.

Post Processing

To add Post Processing the template project includes Unity's Post Processing Stack (v2) from the Package Manager. For this you simply add a 'Post-process Layer' component to the Main Camera, and add 'Post-process Volume' components to an object near the camera.

If you use different post processing profiles on different volumes, then you can enable and disable the volumes to change the active post processing effect. In the sample video I did this by making different profiles child objects of the camera:


Then you can use an 'Activation Track' in timeline to enable and disable the different effects:


For more information on Unity's Post Processing Stack, read the documenation here.

Animation and Audio

You can easily animate UI elements using an animation track in timeline. See the sample scene for lots of examples. For more details about animation in Unity see this documentation page.

Similarily for audio you can use the 'Audio Track'. For the sample video I used music from YouTube's free music library (but check the liscense if you want to use it somewhere other than YouTube).

Fonts

Tip: Always use TextMeshPro (included in project template) for UI Text rather than Unity's standard UI Text elements, as it makes your text look crystal clear and allows you to use a larger range of effects.

The template project includes some popular fonts in the 'Fonts' folder. These have come from Google Fonts where you can easily find your own if you need to. To be used with TextMeshPro by creating a Font Asset using the 'Window > Text Mesh Pro > Font Asset Creator' window.

Material Animations

You can get some great text effects by modifying TextMeshPro's material properties. My personal favorite is playing with dilation. While Unity's animation window does lets you animate most material properties, this doesn't work with TextMeshPro.

To solve this I created a special 'Material Animation Track' you can use in Timeline:


To use this you'll need to know the name of the property in the material's shader which you want to change. To obtain this select you material and press the small settings icon in the top right of the Inspector Window. Select 'Edit Script' and scroll through the code/list to find the property name. Then in timeline you can add an animation curve for it's value.

UI Gradients

Finally to make your video backgrounds a bit more interesting, consider adding colour gradients. The template project includes scripts for this. The scripts came from this GitHub repo and I would encourage you to take a look.

Asset Store

Don't forget to check the Asset Store for anything you may (or might not think) you need. There are some amazing free packages for UI effects, animations, audio, particle systems and post processing to put you on par with proffessional video editing software!

... so how fast is it? Is there anything wrong?

Recording a video is fast. On my PC (mid/high end) it takes the same amount of time to export a Full HD video as it does to watch it. Which is great for short animations! It might take a little longer for 4K, but not enough to slow down your workflow. And it also works well on low-end or older hardware. I tried it on a 6 year old laptop (i5-4200U + integrated graphics) and it only took an additional 45 seconds for a 2 minute video.

You might think all of the above is too good to be true ... and to an extent you are right. Unity was not made for making videos so you might find some bugs or incompatible assets now and again. But if you are making animated videos, you are familiar with Unity, and its asset workflow appeals to you, then I believe it is a better option than spending hours trying to learn new software.

Thanks for Reading

Please checkout the YouTube video and GitHub repo if you haven't already. From now on I'll be using Unity for the majority of my video editing needs and I hope I've convinced you to try it.

I would love to hear your opinions in the comments. If you use the project template please 'star' it so others can find it. Also feel free to contribute by opening Issues or PRs.

Comments

Popular posts from this blog

What is Homomorphic Encryption?

Homomorphic Encryption is a promising cryptographic technique for keeping data private. In this post I give a short a simple summary of Homomorphic Encryption including a clear definition with an example use case in cloud computing.

Best Packages for Sublime Text 3 (Excluding Themes)

Sublime Text 3 is pretty good out-of-the-box but reaches a whole new level when you install some of the great packages on offer.

Below I'll list my favourite packages for Sublime Text. These are all packages which will enhance your productivity across all languages, so no themes or language-specific packages will be listed here.

Google Apps Script - Introduction With Some Tips and Tricks

Google Apps Script allows you to write and deploy code scripts through Google Drive. These scripts (written in JavaScript) run server-side and have full access to Google's APIs. This means you can use them to automate all sorts of things, from sending emails to editing spreadsheets.

I've added some examples I use regularly to this repository on GitHub.