If you do motion graphics work and you use Unity3D you might find yourself wanting to render out sequences of images from your Unity3D projects to import into compositing applications like Adobe After Effects. I needed to do that.
It’s relatively easy to do if you own Unity3D Pro thanks to support for Render Textures, here’s a script that uses that approach. With a clever workaround you can get transparent background images out of the the free version of Unity3D too.
I first saw the technique described by user Twinfox in this thread. It involves rendering each frame twice, once with a black background and once with a white one, then looking at the differences in pixel colours between the two to derive a final output image with a transparent background.
At the bottom of this post I’ve embedded the script I’ve written that uses this approach. Feel free to use/modify it without restriction.
Here’s how to get it working:
- Save the script in your project as: TransparentBackgroundScreenshotRecorder.cs.
- In your scene, attach the script to the Game Object that carries the camera whose viewpoint you’d like to use.
- In the inspector, set the frameRate value for the attached script. This determines how many screenshots will be captured for each second of ‘game time’.
- In the inspector set the framesToCapture to determine how many screenshots will be made.
- Choose the desired resolution in Unity’s Game window using the dropdown at the top left. You’ll need to use a resolution that’s less than or equal to your screen’s resolution to get the size of image you specify. I usually use 1920 x 1080 for my video projects.
- Turn on the ‘Maximise on Play’ switch in the Game window.
- Play your scene from the editor.
The scene will appear to play very slowly, with a white background, meanwhile screenshots are being saved to [PathToYourUnityProject]/Screenshots by default. A number will be appended to the folder name if there’s a naming collision.
The script will exit back to the editor once the desired number of frames have been captured.
Since playback while capturing is very slow and jerky, it isn’t suited to sequences that respond in a time-sensitive way to player input, but works well for recording anything that runs automatically.