CoplayDev/unity-mcp · error · InvalidOperationException
Failed to resolve Scene view viewport rect.
Error message
Failed to resolve Scene view viewport rect.
What it means
GetSceneViewViewportPixelRect() calls GetViewportLocalRectPoints() to get the viewport in local points, then checks width/height. If the local-points rect has zero or negative dimensions, it throws before the pixel-space conversion. This is an earlier failure than [15]: the viewport geometry itself could not be resolved, before even computing pixel dimensions.
Source
Thrown at MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs:148
sceneView.Repaint();
InvokeMethodIfExists(sceneView, "RepaintImmediately");
SceneView.RepaintAll();
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
EditorApplication.QueuePlayerLoopUpdate();
Thread.Sleep(RepaintSettlingDelayMs);
}
catch (Exception ex)
{
McpLog.Debug($"[EditorWindowScreenshotUtility] SceneView repaint failed: {ex.Message}");
}
}
private static Rect GetSceneViewViewportPixelRect(SceneView sceneView)
{
float pixelsPerPoint = EditorGUIUtility.pixelsPerPoint;
Rect viewportLocalPoints = GetViewportLocalRectPoints(sceneView, pixelsPerPoint);
if (viewportLocalPoints.width <= 0f || viewportLocalPoints.height <= 0f)
throw new InvalidOperationException("Failed to resolve Scene view viewport rect.");
return new Rect(
Mathf.Round(viewportLocalPoints.x * pixelsPerPoint),
Mathf.Round(viewportLocalPoints.y * pixelsPerPoint),
Mathf.Round(viewportLocalPoints.width * pixelsPerPoint),
Mathf.Round(viewportLocalPoints.height * pixelsPerPoint));
}
private static Rect GetViewportLocalRectPoints(SceneView sceneView, float pixelsPerPoint)
{
Rect? cameraViewport = GetRectProperty(sceneView, "cameraViewport");
if (cameraViewport.HasValue && cameraViewport.Value.width > 0f && cameraViewport.Value.height > 0f)
{
return cameraViewport.Value;
}
Camera camera = sceneView.camera;
if (camera == null)View on GitHub (pinned to c21bf496bc)
Solutions
- Wait for the Scene View to finish initialization (EditorApplication.update delay) before capturing.
- Ensure the Scene View is focused and fully rendered before calling capture.
- Retry the capture after a short delay if the viewport is in a transitional state.
- Verify sceneView.camera is non-null and has positive pixelWidth/pixelHeight before calling.
Defensive patterns
Strategy: validation
Validate before calling
Rect? vp = GetRectProperty(sceneView, "cameraViewport");
Camera cam = sceneView.camera;
if ((vp == null || vp.Value.width <= 0) && (cam == null || cam.pixelWidth <= 0))
{ /* viewport not ready; defer capture */ return; } Try / catch
try { CaptureSceneViewViewportToProject(sceneView, ...); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Failed to resolve"))
{ EditorApplication.delayCall += () => RetryCapture(sceneView); } Prevention
- Allow the Scene View to finish initializing before capturing.
- Defer capture to EditorApplication.delayCall if the viewport is not ready.
- Avoid capturing during scene loads or layout transitions.
When it happens
Trigger: GetViewportLocalRectPoints returns a zero-size rect because neither the internal 'cameraViewport' property nor sceneView.camera yields a valid size. This happens when the Scene View is not yet initialized, during scene loading, or in layout transitions.
Common situations: Capturing immediately after opening a project before the Scene View finishes initializing. The Scene View is in a transitional state (docking/undocking). The cameraViewport internal property is unavailable in a specific Unity version and camera.pixelWidth/Height are zero.
Related errors
- Captured Scene view viewport is empty.
- Active Scene View has no camera to derive viewport size from
- Failed to resolve Scene view host view.
- {hostView.GetType().FullName}.GrabPixels(RenderTexture, Rect
- Could not generate a unique screenshot filename for '{fullPa
AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13).
Data as JSON: /api/errors/445437ebfd4819e9.
Report an issue: GitHub.