fix(compat): keep Runtime helpers compiling when built-in modules are disabled (#1160)#1162
Conversation
…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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR fixes compilation errors in Unity 6000.3.13f1 by replacing compile-time dependencies on ChangesModule availability via reflection
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
Physics2Dtype 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.
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 usedtypeof(Physics2D)directly, contradicting its own XML doc ("reflection rather than direct property access … survive eventual removal without a recompile"). Switched toType.GetType("UnityEngine.Physics2D, UnityEngine.Physics2DModule").ScreenshotUtility.cs:269— the file already invests in a reflectiveScreenCapture.CaptureScreenshotpath (s_captureScreenshotMethod) so it compiles without the module. The newerCaptureCompositedmethod bypassed that pattern with a directScreenCapture.CaptureScreenshotAsTexturecall. Added a sibling cachedMethodInfoand 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:
ScreenshotUtilityshipsIsScreenCaptureModuleAvailable+ScreenCaptureModuleNotAvailableError+ a camera-based fallback, andUnityPhysicsCompat'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
CS1069onUnityPhysicsCompat.cs:49).CS0103onScreenshotUtility.cs:269) andCaptureCompositedfalls back to camera capture when called.tools/check-unity-versions.sh) — these are reflection-only changes with no new#if UNITY_*gating, so no version-specific risk.Summary by CodeRabbit