Skip to content

ScreenshotUtility.cs fails to compile when target platform is WebGL #574

@SabadashDenis

Description

@SabadashDenis

Environment:

  • Unity 2022.3.x (or newer)
  • WebGL target platform

Error:
Assets\Plugins\UnityMCP\Runtime\Helpers\ScreenshotUtility.cs(54,13):
error CS0103: The name 'ScreenCapture' does not exist in the current context

Root Cause:

On WebGL platform, Unity completely strips the ScreenCapture class from UnityEngine assembly. The class doesn't exist at compile time, so preprocessor directives like #if !UNITY_WEBGL or #if UNITY_EDITOR
cannot help — the compiler fails before evaluating runtime conditions.

Current location Runtime/Helpers/ causes the file to be compiled for all platforms including WebGL.

Analysis:

ScreenshotUtility is only called from ManageScene.cs located in Editor/Tools/. MCP functionality is Editor-only, so this utility has no runtime use case.

The comment in the file states:
"The reason for having another Runtime Utilities in additional to Editor Utilities is to avoid Editor-only dependencies in this runtime code."

This intent doesn't apply here since ScreenCapture.CaptureScreenshot() is unavailable in WebGL runtime anyway.

Solution:

Move ScreenshotUtility.cs from Runtime/Helpers/ to Editor/Helpers/.

The Editor assembly (MCPForUnity.Editor.asmdef) has "includePlatforms": ["Editor"], which excludes it from platform-specific compilation entirely. ScreenCapture is always available in Editor context.

Required changes:

  1. Move Runtime/Helpers/ScreenshotUtility.cs → Editor/Helpers/ScreenshotUtility.cs
  2. Update namespace: MCPForUnity.Runtime.Helpers → MCPForUnity.Editor.Helpers
  3. Update import in ManageScene.cs: using MCPForUnity.Editor.Helpers;
  4. Remove obsolete #if guards around ScreenCapture call (now unnecessary)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions