{"record":{"id":"ce69946a28ed2961","repo":"CoplayDev/unity-mcp","slug":"failed-to-resolve-scene-view-host-view","errorCode":null,"errorMessage":"Failed to resolve Scene view host view.","messagePattern":"Failed to resolve Scene view host view\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs","lineNumber":184,"sourceCode":"            if (camera == null)\n                throw new InvalidOperationException(\"Active Scene View has no camera to derive viewport size from.\");\n\n            float viewportWidth = camera.pixelWidth / Mathf.Max(0.0001f, pixelsPerPoint);\n            float viewportHeight = camera.pixelHeight / Mathf.Max(0.0001f, pixelsPerPoint);\n            Rect windowRect = sceneView.position;\n\n            return new Rect(\n                0f,\n                Mathf.Max(0f, windowRect.height - viewportHeight),\n                Mathf.Min(windowRect.width, viewportWidth),\n                Mathf.Min(windowRect.height, viewportHeight));\n        }\n\n        private static Texture2D CaptureViewRect(SceneView sceneView, Rect viewportRectPixels)\n        {\n            object hostView = GetHostView(sceneView);\n            if (hostView == null)\n                throw new InvalidOperationException(\"Failed to resolve Scene view host view.\");\n\n            // GrabPixels is an internal extern on GUIView (parent of HostView), present since at least Unity 2021.1.\n            // See: UnityCsReference/Editor/Mono/GUIView.bindings.cs — `internal extern void GrabPixels(RenderTexture, Rect)`\n            // If Unity removes this, the MissingMethodException below keeps the failure explicit.\n            MethodInfo grabPixels = hostView.GetType().GetMethod(\n                \"GrabPixels\",\n                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,\n                null,\n                new[] { typeof(RenderTexture), typeof(Rect) },\n                null);\n\n            if (grabPixels == null)\n                throw new MissingMethodException($\"{hostView.GetType().FullName}.GrabPixels(RenderTexture, Rect)\");\n\n            int width = Mathf.RoundToInt(viewportRectPixels.width);\n            int height = Mathf.RoundToInt(viewportRectPixels.height);\n\n            RenderTexture rt = null;","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs#L166-L202","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the Scene View is open, docked, and visible before capturing — not a floating/undocked window in a transitional state.","Retry after EditorApplication.delayCall to give the view time to attach to its host.","If the internal API changed across Unity versions, verify that m_Parent/hostView still exist using reflection inspection.","Fall back to Camera.Render-based capture if the host view approach is unavailable."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"object hostView = GetHostView(sceneView);\nif (hostView == null)\n{ Debug.LogWarning(\"Scene View host view not available; deferring capture.\"); return; }","typeGuard":"public static bool HasHostView(SceneView sv) => GetHostView(sv) != null;","tryCatchPattern":"try { CaptureViewRect(sceneView, rect); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"host view\"))\n{ Debug.LogWarning(\"Scene View not docked; cannot capture via GrabPixels.\"); }","preventionTips":["Ensure the Scene View is docked and visible before capturing.","Defer capture until the view is fully embedded in the Editor hierarchy.","Do not capture during window open/close transitions."],"tags":["screenshot","scene-view","hostview","reflection","internal-api","editor"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}