Skip to content

Commit 4805bc1

Browse files
committed
fix(compat): keep Runtime helpers compiling when Physics 2D / Screen Capture modules are disabled (#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.
1 parent 5d0c2bd commit 4805bc1

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static class ScreenshotUtility
5353
private static bool s_loggedLegacyScreenCaptureFallback;
5454
private static bool? s_screenCaptureModuleAvailable;
5555
private static System.Reflection.MethodInfo s_captureScreenshotMethod;
56+
private static System.Reflection.MethodInfo s_captureScreenshotAsTextureMethod;
5657

5758
/// <summary>
5859
/// Checks if the Screen Capture module (com.unity.modules.screencapture) is enabled.
@@ -72,6 +73,8 @@ public static bool IsScreenCaptureModuleAvailable
7273
{
7374
s_captureScreenshotMethod = screenCaptureType.GetMethod("CaptureScreenshot",
7475
new Type[] { typeof(string), typeof(int) });
76+
s_captureScreenshotAsTextureMethod = screenCaptureType.GetMethod("CaptureScreenshotAsTexture",
77+
new Type[] { typeof(int) });
7578
}
7679
}
7780
return s_screenCaptureModuleAvailable.Value;
@@ -249,7 +252,7 @@ public static ScreenshotCaptureResult CaptureComposited(
249252
int maxResolution = 0,
250253
string folderOverride = null)
251254
{
252-
if (!IsScreenCaptureModuleAvailable)
255+
if (!IsScreenCaptureModuleAvailable || s_captureScreenshotAsTextureMethod == null)
253256
{
254257
var fallbackCamera = FindAvailableCamera();
255258
if (fallbackCamera != null)
@@ -266,7 +269,9 @@ public static ScreenshotCaptureResult CaptureComposited(
266269
int imgW = 0, imgH = 0;
267270
try
268271
{
269-
tex = ScreenCapture.CaptureScreenshotAsTexture(result.SuperSize);
272+
// Reflective call to ScreenCapture.CaptureScreenshotAsTexture so the file compiles
273+
// even when the Screen Capture module (com.unity.modules.screencapture) is disabled.
274+
tex = s_captureScreenshotAsTextureMethod.Invoke(null, new object[] { result.SuperSize }) as Texture2D;
270275
if (tex == null)
271276
{
272277
// Fallback to camera-based if ScreenCapture fails

MCPForUnity/Runtime/Helpers/UnityPhysicsCompat.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ namespace MCPForUnity.Runtime.Helpers
1717
///
1818
/// We use reflection rather than direct property access so calls stay clean of
1919
/// CS0618 warnings AND survive eventual removal of the obsolete property without
20-
/// a recompile of this package.
20+
/// a recompile of this package. Type lookups go through <see cref="Type.GetType(string)"/>
21+
/// so this file compiles even when the Physics 2D built-in module is disabled in
22+
/// the Package Manager.
2123
/// </summary>
2224
public static class UnityPhysicsCompat
2325
{
26+
// Assembly-qualified name — resolved at runtime so the file compiles when the
27+
// Physics 2D built-in module is disabled in the Package Manager.
28+
private const string Physics2DTypeName = "UnityEngine.Physics2D, UnityEngine.Physics2DModule";
2429
/// <summary>
2530
/// Cross-version description of the 3D physics simulation mode.
2631
/// On 2022.2+ this maps onto <c>UnityEngine.SimulationMode</c>; on older
@@ -46,9 +51,13 @@ private static PropertyInfo Physics2DAutoSyncProp
4651
if (!_physics2DProbed)
4752
{
4853
_physics2DProbed = true;
49-
_physics2DAutoSync = typeof(Physics2D).GetProperty(
50-
"autoSyncTransforms",
51-
BindingFlags.Public | BindingFlags.Static);
54+
var physics2DType = Type.GetType(Physics2DTypeName);
55+
if (physics2DType != null)
56+
{
57+
_physics2DAutoSync = physics2DType.GetProperty(
58+
"autoSyncTransforms",
59+
BindingFlags.Public | BindingFlags.Static);
60+
}
5261
}
5362
return _physics2DAutoSync;
5463
}

0 commit comments

Comments
 (0)