fix: declare required Unity module dependencies#1122
Conversation
MCPForUnity.Runtime uses Texture2D.EncodeToPNG, Physics2D, and ScreenCapture APIs. Declaring their built-in modules prevents Unity projects that do not already enable those modules from failing script compilation. Co-authored-by: Codex <noreply@openai.com>
|
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 (1)
📝 WalkthroughWalkthroughSeven Unity module dependencies— ChangesUnity Module Dependencies
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 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 updates the Unity package manifest to explicitly declare built-in Unity module dependencies that the package’s code relies on, preventing script compilation failures in projects where those modules are disabled.
Changes:
- Added
com.unity.modules.imageconversiondependency to ensureTexture2D.EncodeToPNG()APIs are available. - Added
com.unity.modules.physics2ddependency to ensurePhysics2DAPIs are available. - Added
com.unity.modules.screencapturedependency to ensureScreenCaptureAPIs are available.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "com.unity.modules.imageconversion": "1.0.0", | ||
| "com.unity.modules.physics2d": "1.0.0", | ||
| "com.unity.modules.screencapture": "1.0.0", | ||
| "com.unity.nuget.newtonsoft-json": "3.0.2", | ||
| "com.unity.test-framework": "1.1.31" |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@MCPForUnity/package.json`:
- Around line 10-12: Add the missing 3D physics Unity module by adding the
package "com.unity.modules.physics" with the same version string used for other
modules (e.g., "1.0.0") to MCPForUnity/package.json so the runtime can resolve
UnityEngine.Physics used in MCPForUnity/Runtime/Helpers/UnityPhysicsCompat.cs
(see typeof(Physics).GetProperty(...)); ensure the physics and physics2d entries
use the same version format as the other modules to match the test project
manifests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
…arations (#1122) Since #1122 declared the Physics 2D, Screen Capture, Image Conversion, etc. built-in modules as required deps in package.json, Unity Package Manager now keeps them enabled while MCP for Unity is installed. Two defensive layers that protected against the now-impossible "module disabled" case become dead code, plus one sub-floor preprocessor gate that was always-true on our declared `unity: 2021.3` floor. Tier 1A — UnityPhysicsCompat.cs - Revert `Type.GetType("UnityEngine.Physics2D, UnityEngine.Physics2DModule")` to `typeof(Physics2D)`. The property-level reflection (for `autoSyncTransforms` deprecation in Unity 6.x) stays — that's a different concern. Tier 1B — TrailControl.cs - Strip `#if UNITY_2021_1_OR_NEWER` gate. Package floor is 2021.3 per package.json, so the `#else` branch ("AddPosition requires Unity 2021.1+") is unreachable. Tier 2 — ScreenshotUtility.cs + callers - Remove `IsScreenCaptureModuleAvailable` property, `ScreenCaptureModuleNotAvailableError` constant, `InvokeCaptureScreenshotAsTexture` helper, and the `s_captureScreenshotMethod` / `s_captureScreenshotAsTextureMethod` / `s_screenCaptureModuleAvailable` reflection caches. - Replace reflective `MethodInfo.Invoke` calls with direct `ScreenCapture.X` calls in `CaptureToProjectFolder`, `CaptureComposited`, and the `ScreenshotCapturer` MonoBehaviour. - Camera-based fallback path (`FindAvailableCamera` + `CaptureFromCameraToProjectFolder`) is preserved — it still handles transient null returns from `ScreenCapture` inside `CaptureComposited`. - Drop pre-flight `if (!IsScreenCaptureModuleAvailable)` gates in `ManageUI.cs` (UI render) and `ManageScene.cs` (screenshot path). - `ProjectInfo` resource: `screenCapture` field is now an invariant `true` (preserves API shape for `mcpforunity://project/info` consumers). Net: 144 lines removed, 15 added. No new tests — the changes are pure removal of code paths that cannot fire with the current deps declared. Related: #1160, #1122.
Summary
Adds missing built-in Unity module dependencies to the MCP for Unity package manifest:
com.unity.modules.imageconversioncom.unity.modules.physics2dcom.unity.modules.screencaptureRoot Cause
MCPForUnity.Runtimedirectly uses APIs from these modules:Texture2D.EncodeToPNG()from Image ConversionPhysics2Dfrom Physics 2DScreenCapturefrom Screen CaptureProjects that do not already enable these built-in modules can fail script compilation after installing the package.
Validation
Validated in a Unity
6000.3.15f1project that originally failed compilingMCPForUnity.Runtimewith missing module references. After declaring the dependencies, Unity Package Manager resolved the three built-in modules and batchmode script compilation completed successfully.Summary by CodeRabbit