Skip to content

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

Closed

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)

Activity

  1. Scriptwonder commented on Jan 17, 2026

    @Scriptwonder
    Collaborator

    Can we instead add asmdef for the Runtime folder to resolve the issue?

  2. SabadashDenis commented on Jan 18, 2026

    @SabadashDenis
    Author

    There is already an asmdef for the Runtime folder (MCPForUnity.Runtime.asmdef). I tried adding "excludePlatforms": ["WebGL"] to it, but this doesn't solve the compilation error.

    ExcludePlatforms controls whether the compiled assembly is included in the final build, but Unity still compiles the C# code for the active build target. So when a developer has WebGL selected as their active platform in Build Settings, Unity compiles Runtime scripts using the WebGL scripting backend, where ScreenCapture class doesn't exist. This results in CS0103 error even though the assembly would be excluded from the actual build.

    Using #if !UNITY_WEBGL preprocessor guards also won't work properly — it would fix the compilation error, but screenshots wouldn't work in the Editor when WebGL is the active platform, since UNITY_WEBGL is defined based on the build target, not the runtime environment.

    Moving ScreenshotUtility.cs to the Editor/ folder solves both problems because Editor scripts are always compiled using the desktop scripting backend, regardless of the active build target. Since MCP tools only run in the Editor anyway, this doesn't change any functionality.

  3. Scriptwonder commented on Jan 19, 2026

    @Scriptwonder
    Collaborator

    I see your point.
    The naming might be confusing: runtime actually means PlayMode, as many Editor folder tools rely on APIs that can only run on editor time. I think moving to Editor is fine, but I was thinking of expanding the runtime to include a lot more runtime-runable tools in the future. Will make that change if I cannot find a better solution these two weeks.

  4. SabadashDenis commented on Jan 19, 2026

    @SabadashDenis
    Author

    Ok, thanks. I created pull request yesterday. You can check it.

  5. Fkscorpion commented on Jan 29, 2026

    @Fkscorpion

    Is this also related and/or will it fix this bug? Sounds like it's the same you had just with another platform.

    I've opened a project with unity 6000.3.5f2 (LTS) on the Android Platform and got this:

    Library\PackageCache\com.coplaydev.unity-mcp@9c2049bb148f\Runtime\Helpers\ScreenshotUtility.cs(54,13): error CS0103: The name 'ScreenCapture' does not exist in the current context

    I had to manually enable the ScreenCapture Component in the PackageManager and then the error disappeared:

    Image
  6. Scriptwonder commented on Jan 30, 2026

    @Scriptwonder
    Collaborator

    Is this also related and/or will it fix this bug? Sounds like it's the same you had just with another platform.

    I've opened a project with unity 6000.3.5f2 (LTS) on the Android Platform and got this:

    Library\PackageCache\com.coplaydev.unity-mcp@9c2049bb148f\Runtime\Helpers\ScreenshotUtility.cs(54,13): error CS0103: The name 'ScreenCapture' does not exist in the current context

    I had to manually enable the ScreenCapture Component in the PackageManager and then the error disappeared:

    Image

    Oh I see. @SabadashDenis Can you check if WebGL enable this feature by default? This might be the actual issue.

  7. SabadashDenis commented on Jan 31, 2026

    @SabadashDenis
    Author

    Is this also related and/or will it fix this bug? Sounds like it's the same you had just with another platform.
    I've opened a project with unity 6000.3.5f2 (LTS) on the Android Platform and got this:
    Library\PackageCache\com.coplaydev.unity-mcp@9c2049bb148f\Runtime\Helpers\ScreenshotUtility.cs(54,13): error CS0103: The name 'ScreenCapture' does not exist in the current context
    I had to manually enable the ScreenCapture Component in the PackageManager and then the error disappeared:
    Image

    Oh I see. @SabadashDenis Can you check if WebGL enable this feature by default? This might be the actual issue.

    Yeh, it's the source of problem. Enable package in my project and ScreenCapture error dissapeared. Tried to create new project with WebGL platform, ScreenCapture package enabled by default.

    So, it was my bad. I disabled unused packages in project for optimization. And after Unity-MCP setup i had compile error.

  8. Scriptwonder commented on Jan 31, 2026

    @Scriptwonder
    Collaborator

    Thank you for double checking! Will add notes in the GUI/Reference to let user know that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions