The ExitPlayMode Prefab in the scene hierarchy.
The ExitPlayMode Prefab in the scene hierarchy.

ExitPlayMode: My favorite script for Unity Rendering

I get a lot of work doing rendering in game engines. Since 2018 Unity became the easiest place to be doing that kind of offline rendering, especially if you are working with a small team and need quick iterations.

They created 3 tools that really solidified Unity’s utility as a animation and rendering tool:

  • Timeline: A enhanced sequencer that let’s you control everything from animations, activations, audio, and effects in one reliable and intuitive control track.
  • Cinemachine: tool for using ‘Virtual Cameras’ to set and control shots, drive physics based properties, and drive post processing.
  • Unity Recorder: A tool for exporting images and video captured in engine, with consistent render quality. Recorder can slow down frame rendering to make sure your lighting and resolution are always exactly what you asked for.
  • URP or HDRP: The dedicated Render Pipelines and tight integration with Cinemachine and Timeline really boosted Unity’s overall usefulness for rendering.

But these tools are all missing an essential component, once the render stops, the game engine keeps going. This isn’t a huge problem on a short render, or a simple scene. Anything that renders quick enough that you could wait at your desk is totally fine.

But if your render is going for a few hours, and if your lighting is power intensive (HDRP Path Tracing), you need a guaranteed way to stop the engine when the render is done.

So this is my personal end render script, deployed for clients across the continent, and to render of hundreds of commercial renders:

using UnityEditor;
using UnityEngine;

public class ExitPlayMode : MonoBehaviour
{
    void OnEnable ()
    {
        Debug.Log("Render Done!!!");
        EditorApplication.Beep();
        EditorApplication.ExitPlaymode();
        Application.Quit();
    }
}

To deploy this:

  1. Add to an empty in unity (make this a prefab if you want to reuse it)
  2. Drag it into your timeline to create an “Activation Track”
  3. Move the “Activation Clip” to the end of your recording

It’s doing a few simple but ‘hard to find on google’ things:

  1. Let you know the render is done in the console. It’s always good to spell out exactly what’s going on.
  2. An audio cue, using a system beep means we don’t need to hook up any audio files, it just works. This beep lets you go live your life out of the room. Now you can go work on your laptop, watch something, make some food, do some exercise. Just make sure to tune the volume on your speakers to make sure you hear it (Or get a baby monitor?) for however far away you need to hear the beep.
  3. Stops Unity’s play mode. This is the big one, you don’t want the render loop, and physics loop and update loop running all night. This stop all of that right away and returns your editor to it’s normal – more power efficient – editor mode.
  4. Quit the build. In case made a build for the rendering, this will kill the program. It’s the build version of the last step. The last step can’t effect builds, and this step can’t effect the editor. Come to think of it’ step three might prevent a build, if so, let’s call fixing that homework for you.

Nothing in there is radical or overly fancy, it’s simple and functional.
I tried to re-write it from scratch this week and found that google results for “Play beep from code in Unity” all kind of suck now, so I dug up my original and put it here so no one has to face that minor inconvenience again.

Matt Murch

Technical & 3D artist. Working on games in Unity and Blender. CEO/Founder @ Double M Digital Inc. Making the new game Chicken Nuggets! Always open to neat 3D/VR/Real-time contracts and opportunities!