CoplayDev/unity-mcp · error · InvalidOperationException

Failed to resolve Scene view host view.

Error message

Failed to resolve Scene view host view.

What it means

CaptureViewRect() needs the SceneView's internal HostView to call GrabPixels via reflection. GetHostView() tries the m_Parent field and hostView property. If both return null, it throws InvalidOperationException because pixel capture is impossible without the host view's GUIView. This indicates the SceneView window is not properly docked or hosted in the Editor's view hierarchy.

Source

Thrown at MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs:184

            if (camera == null)
                throw new InvalidOperationException("Active Scene View has no camera to derive viewport size from.");

            float viewportWidth = camera.pixelWidth / Mathf.Max(0.0001f, pixelsPerPoint);
            float viewportHeight = camera.pixelHeight / Mathf.Max(0.0001f, pixelsPerPoint);
            Rect windowRect = sceneView.position;

            return new Rect(
                0f,
                Mathf.Max(0f, windowRect.height - viewportHeight),
                Mathf.Min(windowRect.width, viewportWidth),
                Mathf.Min(windowRect.height, viewportHeight));
        }

        private static Texture2D CaptureViewRect(SceneView sceneView, Rect viewportRectPixels)
        {
            object hostView = GetHostView(sceneView);
            if (hostView == null)
                throw new InvalidOperationException("Failed to resolve Scene view host view.");

            // GrabPixels is an internal extern on GUIView (parent of HostView), present since at least Unity 2021.1.
            // See: UnityCsReference/Editor/Mono/GUIView.bindings.cs — `internal extern void GrabPixels(RenderTexture, Rect)`
            // If Unity removes this, the MissingMethodException below keeps the failure explicit.
            MethodInfo grabPixels = hostView.GetType().GetMethod(
                "GrabPixels",
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null,
                new[] { typeof(RenderTexture), typeof(Rect) },
                null);

            if (grabPixels == null)
                throw new MissingMethodException($"{hostView.GetType().FullName}.GrabPixels(RenderTexture, Rect)");

            int width = Mathf.RoundToInt(viewportRectPixels.width);
            int height = Mathf.RoundToInt(viewportRectPixels.height);

            RenderTexture rt = null;

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Ensure the Scene View is open, docked, and visible before capturing — not a floating/undocked window in a transitional state.
  2. Retry after EditorApplication.delayCall to give the view time to attach to its host.
  3. If the internal API changed across Unity versions, verify that m_Parent/hostView still exist using reflection inspection.
  4. Fall back to Camera.Render-based capture if the host view approach is unavailable.
Defensive patterns

Strategy: validation

Validate before calling

object hostView = GetHostView(sceneView);
if (hostView == null)
{ Debug.LogWarning("Scene View host view not available; deferring capture."); return; }

Type guard

public static bool HasHostView(SceneView sv) => GetHostView(sv) != null;

Try / catch

try { CaptureViewRect(sceneView, rect); }
catch (InvalidOperationException ex) when (ex.Message.Contains("host view"))
{ Debug.LogWarning("Scene View not docked; cannot capture via GrabPixels."); }

Prevention

When it happens

Trigger: The SceneView exists as an object but its m_Parent field and hostView property are both null — the window is detached, closing, or not yet fully embedded in the Editor's container hierarchy.

Common situations: Capturing during window creation before the view is docked. The Scene View was programmatically created but never shown. Capturing during Editor shutdown when the view is being torn down. A Unity version changed the internal field/property names for the parent reference.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/ce69946a28ed2961. Report an issue: GitHub.