Skip to content

fix(compat): keep Runtime helpers compiling when built-in modules are disabled (#1160)#1162

Merged
Scriptwonder merged 1 commit into
CoplayDev:betafrom
Scriptwonder:fix/issue-1160-disabled-modules
May 25, 2026
Merged

fix(compat): keep Runtime helpers compiling when built-in modules are disabled (#1160)#1162
Scriptwonder merged 1 commit into
CoplayDev:betafrom
Scriptwonder:fix/issue-1160-disabled-modules

Conversation

@Scriptwonder
Copy link
Copy Markdown
Collaborator

@Scriptwonder Scriptwonder commented May 25, 2026

Summary

Fixes #1160 — v9.7.0 introduced two Runtime compile failures for users who disable the Physics 2D or Screen Capture built-in modules in Package Manager → Built-in.

  • UnityPhysicsCompat.cs:49 — the shim file used typeof(Physics2D) directly, contradicting its own XML doc ("reflection rather than direct property access … survive eventual removal without a recompile"). Switched to Type.GetType("UnityEngine.Physics2D, UnityEngine.Physics2DModule").
  • ScreenshotUtility.cs:269 — the file already invests in a reflective ScreenCapture.CaptureScreenshot path (s_captureScreenshotMethod) so it compiles without the module. The newer CaptureComposited method bypassed that pattern with a direct ScreenCapture.CaptureScreenshotAsTexture call. Added a sibling cached MethodInfo and routed the call through it.

Editor-side direct usages (PhysicsQueryOps.cs, ManageUI.cs) are pre-existing in v9.6.8 and represent intentional feature requirements — out of scope here.

Why this fix vs. "module is required"

The package already advertises graceful degradation: ScreenshotUtility ships IsScreenCaptureModuleAvailable + ScreenCaptureModuleNotAvailableError + a camera-based fallback, and UnityPhysicsCompat's XML doc explicitly promises reflective access. v9.7.0 silently broke both promises in Runtime files — restoring them is the minimal, contract-aligned change.

Test plan

  • In a project with Physics 2D disabled in Package Manager → Built-in, install this branch and confirm Runtime compiles (pre-fix: CS1069 on UnityPhysicsCompat.cs:49).
  • In a project with Screen Capture disabled, confirm Runtime compiles (pre-fix: CS0103 on ScreenshotUtility.cs:269) and CaptureComposited falls back to camera capture when called.
  • CI matrix (tools/check-unity-versions.sh) — these are reflection-only changes with no new #if UNITY_* gating, so no version-specific risk.

Summary by CodeRabbit

  • Refactor
    • Enhanced screenshot capture with improved fallback mechanisms when modules are unavailable.
    • Improved Physics2D compatibility resolution for better resilience across varying module configurations.

Review Change Stack

…Capture modules are disabled (CoplayDev#1160)

v9.7.0 introduced two Runtime regressions that block compilation when users
disable the matching built-in modules in Package Manager:

- UnityPhysicsCompat.cs used `typeof(Physics2D)` directly, contradicting the
  file's own contract of reflection-based access ("survive eventual removal
  without recompile"). Switched to `Type.GetType("UnityEngine.Physics2D,
  UnityEngine.Physics2DModule")` so the shim degrades gracefully when the
  Physics 2D module is off.

- ScreenshotUtility.CaptureComposited bypassed the existing reflective
  ScreenCapture path (s_captureScreenshotMethod) with a direct call to
  ScreenCapture.CaptureScreenshotAsTexture. Cached a second MethodInfo
  (s_captureScreenshotAsTextureMethod) and routed the call through it, so
  the file compiles with the Screen Capture module disabled and falls back
  to camera capture at runtime — matching the behavior the file already
  advertises via IsScreenCaptureModuleAvailable.
Copilot AI review requested due to automatic review settings May 25, 2026 17:28
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e86258da-4de0-43a7-a076-dc5053712530

📥 Commits

Reviewing files that changed from the base of the PR and between 5d0c2bd and 4805bc1.

📒 Files selected for processing (2)
  • MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs
  • MCPForUnity/Runtime/Helpers/UnityPhysicsCompat.cs

📝 Walkthrough

Walkthrough

This PR fixes compilation errors in Unity 6000.3.13f1 by replacing compile-time dependencies on ScreenCapture.CaptureScreenshotAsTexture and Physics2D types with runtime reflection, allowing the code to compile when these optional modules are not enabled while maintaining existing fallback behavior.

Changes

Module availability via reflection

Layer / File(s) Summary
ScreenshotUtility reflected method caching and precondition
MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs
ScreenCapture.CaptureScreenshotAsTexture is cached as a reflected MethodInfo during IsScreenCaptureModuleAvailable initialization. CaptureComposited checks both module availability and successful reflection before invoking the composited path; otherwise falls back to camera-based capture.
UnityPhysicsCompat runtime type resolution
MCPForUnity/Runtime/Helpers/UnityPhysicsCompat.cs
Physics2D type is resolved at runtime via Type.GetType(Physics2DTypeName) instead of compile-time typeof(Physics2D). Property lookup proceeds on the resolved type; XML documentation updated to explain reflection-based lookup and compilation resilience when the Physics2D module is disabled.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • CoplayDev/unity-mcp#1040: Introduces or modifies CaptureComposited and CaptureScreenshotAsTexture usage; this PR adds reflection-based invocation of the same method for module availability handling.

Poem

🐰 Reflection dances, no compile-time fuss,
Physics and Screenshots now work for all of us,
When modules are missing, our fallbacks take flight,
Unity 6000 compiles just right!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically identifies the fix: restoring Runtime helper compilation when built-in modules are disabled, with reference to the corresponding issue.
Description check ✅ Passed The description thoroughly explains the problem, root causes, fixes applied, and test plan, covering all material aspects though some optional template sections are not completed.
Linked Issues check ✅ Passed The PR directly addresses both compile errors from issue #1160 by replacing direct type/module references with reflection-based access in UnityPhysicsCompat.cs and ScreenshotUtility.cs.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing Runtime compilation issues in the two identified files; editor-side files with module dependencies are correctly noted as out of scope.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR restores Runtime compatibility when Unity built-in Physics 2D or Screen Capture modules are disabled, aligning the helper shims with their existing reflective/degraded behavior.

Changes:

  • Replaces direct Physics2D type access with an assembly-qualified reflective lookup.
  • Adds reflective lookup/invocation for ScreenCapture.CaptureScreenshotAsTexture.
  • Preserves camera fallback behavior when Screen Capture APIs are unavailable.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
MCPForUnity/Runtime/Helpers/UnityPhysicsCompat.cs Removes direct Physics 2D module dependency from Runtime helper probing.
MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs Removes direct Screen Capture texture API dependency and routes composited capture through cached reflection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Scriptwonder Scriptwonder merged commit fe200b7 into CoplayDev:beta May 25, 2026
3 checks passed
@Scriptwonder Scriptwonder deleted the fix/issue-1160-disabled-modules branch May 25, 2026 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: v9.7.0 and v9.7.1 break compatibility with 6000.3.13f1

2 participants