

Most Unity developers use cameras, lights, and materials without ever touching Command Buffers. But under the hood, Unity’s rendering pipeline is built around commands sent to the GPU. Command Buffers allow you to inject your own rendering instructions directly into that pipeline.
If you want custom rendering effects, advanced post-processing, or fine control over when something is drawn, Command Buffers are a powerful tool.
A Command Buffer is a list of rendering commands that Unity executes at a specific point in the render pipeline.
Think of it as a custom set of GPU instructions you prepare in advance and tell Unity when to run.
You can:
Command Buffers are useful when:
They give you precise control over rendering order.
Here’s a simple example that draws a mesh manually.
using UnityEngine;
using UnityEngine.Rendering;
public class SimpleCommandBufferExample : MonoBehaviour
{
public Camera targetCamera;
public Mesh mesh;
public Material material;
private CommandBuffer commandBuffer;
void Start()
{
commandBuffer = new CommandBuffer();
commandBuffer.name = "Draw Custom Mesh";
commandBuffer.DrawMesh(
mesh,
Matrix4x4.identity,
material
);
targetCamera.AddCommandBuffer(
CameraEvent.AfterForwardOpaque,
commandBuffer
);
}
void OnDisable()
{
if (targetCamera != null && commandBuffer != null)
{
targetCamera.RemoveCommandBuffer(
CameraEvent.AfterForwardOpaque,
commandBuffer
);
}
}
}
This script injects drawing instructions after opaque objects are rendered.
When attaching a Command Buffer to a camera, you must choose when it runs. Some common events include:
Choosing the correct event determines how your effect interacts with other objects.
Blit copies a texture into another render target. This is common in post-processing.
commandBuffer.Blit(
BuiltinRenderTextureType.CameraTarget,
BuiltinRenderTextureType.CameraTarget,
material
);
This applies a material effect to the camera output.
You can render to a separate texture for portals, mirrors, or special effects.
RenderTexture rt = new RenderTexture(512, 512, 16); commandBuffer.SetRenderTarget(rt); commandBuffer.ClearRenderTarget(true, true, Color.black); commandBuffer.DrawMesh(mesh, Matrix4x4.identity, material);
This gives you full control over off-screen rendering.
If you forget to remove Command Buffers, they can stack or persist unexpectedly.
Always remove them in OnDisable or OnDestroy.
In Scriptable Render Pipeline (URP or HDRP), Command Buffers are used internally through ScriptableRenderPass.
Instead of attaching to CameraEvent directly, you extend the render pipeline.
In built-in pipeline, you attach directly to Camera or Light.
Use them when you need rendering control that standard materials and cameras cannot provide.
If a feature can be built using normal shaders and materials, prefer that first. Command Buffers are powerful but add complexity.
Command Buffers give you low-level access to Unity’s rendering pipeline without writing a full custom render pipeline. They are perfect for advanced visual effects, special rendering passes, and performance-optimized custom solutions.
Used carefully, they open the door to professional-grade rendering techniques inside Unity.