Capture synthetic images
Request an asynchronous GPU readback from a camera and encode the result to an image file.
Use this task when you need to record what a camera sees, for example to generate training data or debug a sensor. You can capture a full Camera's channels or a single render target, correct for platforms that flip images, encode the readback to a common image format, and batch captures together for higher throughput.
For a full list of the classes, channels, and formats referenced in this page, refer to the Image capture reference.
Request a capture
Call CaptureCamera.Capture with the Camera you want to read back from and a completion callback. The callback runs on the main thread once every requested channel has finished readback:
using Unity.Simulation;
using UnityEngine;
CaptureCamera.Capture(
camera,
request =>
{
// Called on the main thread when all readback is complete
var colorBytes = (byte[])request.data.colorBuffer;
// persist or process colorBytes...
return AsyncRequest.Result.Completed;
});
If you only need a single render target rather than a Camera's channels, use CaptureRenderTexture.Capture instead:
using Unity.Simulation;
using UnityEngine;
var rt = new RenderTexture(1920, 1080, 24);
CaptureRenderTexture.Capture(rt, request =>
{
byte[] data = request.data as byte[];
// process data...
return AsyncRequest.Result.Completed;
});
CaptureRenderTexture falls back to synchronous readback on platforms that don't support asynchronous GPU readback.
Correct flipped output
Some platforms or render pipelines produce vertically flipped images. If your captured images appear upside down, set the ForceFlip flags on the request for the affected channels to override the automatic flip detection.
Encode the captured bytes
Pass the raw readback bytes to CaptureImageEncoder.Encode to convert them to a common image format, then write the result to disk:
byte[] png = CaptureImageEncoder.Encode(rawBytes, width, height, format, ImageFormat.Png);
Batch captures for throughput
If you're capturing many frames in a row, for example during headless or cloud simulation, set CaptureOptions.useBatchReadback = true to group requests into a BatchReadback flush instead of issuing them individually.