I'm developinng a PC game in Unity 6 URP 3D. I'd like to take a screenshot and encode it to JPG, but I'm worried about the player experiencing performance hitches. Conforming to the Unity 6 API, what's the most performant, best practice way to achieve this?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
For performant screenshot capture in Unity 6 URP, here's the best approach to minimize hitches:
Use AsyncGPUReadback
The key is to avoid blocking the main thread. Unity's AsyncGPUReadback allows you to read texture data from the GPU without stalling:
csharp
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Collections;
using System.Threading.Tasks;
public class ScreenshotCapture : MonoBehaviour
{
private bool isCapturing = false;
public async void CaptureScreenshot()
{
if (isCapturing) return;
isCapturing = true;
// Wait for end of frame to ensure rendering is complete
await Task.Yield();
// Request async readback
AsyncGPUReadback.Request(
src: null, // null captures the screen
mipIndex: 0,
dstFormat: TextureFormat.RGB24,
callback: OnCompleteReadback
);
}
private async void OnCompleteReadback(AsyncGPUReadbackRequest request)
{
if (request.hasError)
{
Debug.LogError("GPU readback error");
isCapturing = false;
return;
}
// Get the raw data
NativeArray<byte> rawData = request.GetData<byte>();
// Encode on a background thread
await Task.Run(() =>
{
// Create texture data array
byte[] textureData = rawData.ToArray();
// Encode to JPG (this happens on background thread)
byte[] jpgData = ImageConversion.EncodeArrayToJPG(
textureData,
GraphicsFormat.R8G8B8_UNorm,
(uint)Screen.width,
(uint)Screen.height,
0, // row bytes (0 = auto calculate)
quality: 75
);
// Save to disk (still on background thread)
string path = System.IO.Path.Combine(
Application.persistentDataPath,
$"screenshot_{System.DateTime.Now:yyyyMMdd_HHmmss}.jpg"
);
System.IO.File.WriteAllBytes(path, jpgData);
});
isCapturing = false;
}
}
Alternative: RenderTexture Approach
If you need more control or want to capture specific cameras:
csharp
public class RenderTextureScreenshot : MonoBehaviour
{
public Camera targetCamera;
private RenderTexture renderTexture;
public async void CaptureFromCamera()
{
// Create temporary render texture
renderTexture = RenderTexture.GetTemporary(
Screen.width,
Screen.height,
24,
RenderTextureFormat.ARGB32
);
// Store original camera target
var originalTarget = targetCamera.targetTexture;
// Render to our texture
targetCamera.targetTexture = renderTexture;
targetCamera.Render();
targetCamera.targetTexture = originalTarget;
// Request async readback
AsyncGPUReadback.Request(
renderTexture,
0,
TextureFormat.RGB24,
OnRenderTextureReadback
);
}
private async void OnRenderTextureReadback(AsyncGPUReadbackRequest request)
{
// Release the render texture immediately
RenderTexture.ReleaseTemporary(renderTexture);
if (request.hasError) return;
// Same encoding process as above...
await Task.Run(() =>
{
var rawData = request.GetData<byte>();
byte[] jpgData = ImageConversion.EncodeArrayToJPG(
rawData.ToArray(),
GraphicsFormat.R8G8B8_UNorm,
(uint)request.width,
(uint)request.height,
0,
quality: 75
);
// Save file...
});
}
}
Performance Tips
Limit frequency: Add cooldown between captures to prevent multiple simultaneous requests
Reduce resolution: Consider capturing at lower resolution if full resolution isn't needed
Adjust JPG quality: Lower quality (60-75) significantly reduces encoding time
Pool resources: If capturing frequently, consider pooling RenderTextures
Profile on target hardware: AsyncGPUReadback performance varies by platform
The async approach typically adds only 1-3ms to the main thread, with the heavy work happening asynchronously. The actual encoding and file I/O happen on a background thread, preventing any noticeable hitches during gameplay.