Files
Shutong Wu c56337ec03 refactor: drop defensive scaffolding made obsolete by module dep declarations (#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.
2026-05-26 16:13:00 +08:00

97 lines
3.2 KiB
C#

using System;
using System.IO;
using System.Reflection;
using MCPForUnity.Editor.Helpers;
using Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;
using PackageInfo = UnityEditor.PackageManager.PackageInfo;
namespace MCPForUnity.Editor.Resources.Project
{
/// <summary>
/// Provides static project configuration information.
/// </summary>
[McpForUnityResource("get_project_info")]
public static class ProjectInfo
{
public static object HandleCommand(JObject @params)
{
try
{
string assetsPath = Application.dataPath.Replace('\\', '/');
string projectRoot = Directory.GetParent(assetsPath)?.FullName.Replace('\\', '/');
string projectName = Path.GetFileName(projectRoot);
var info = new
{
projectRoot = projectRoot ?? "",
projectName = projectName ?? "",
unityVersion = Application.unityVersion,
platform = EditorUserBuildSettings.activeBuildTarget.ToString(),
assetsPath = assetsPath,
renderPipeline = RenderPipelineUtility.GetActivePipeline().ToString(),
activeInputHandler = GetActiveInputHandler(),
packages = new
{
ugui = IsPackageInstalled("com.unity.ugui"),
textmeshpro = IsPackageInstalled("com.unity.textmeshpro"),
inputsystem = IsPackageInstalled("com.unity.inputsystem"),
uiToolkit = true,
screenCapture = true,
}
};
return new SuccessResponse("Retrieved project info.", info);
}
catch (Exception e)
{
return new ErrorResponse($"Error getting project info: {e.Message}");
}
}
/// <summary>
/// Reads PlayerSettings.activeInputHandler via reflection to avoid
/// compile-time dependency on the Input System package.
/// Returns "Old" (0), "New" (1), or "Both" (2).
/// </summary>
private static string GetActiveInputHandler()
{
try
{
var prop = typeof(PlayerSettings).GetProperty(
"activeInputHandler",
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
if (prop == null)
return "Old";
int value = (int)prop.GetValue(null);
return value switch
{
0 => "Old",
1 => "New",
2 => "Both",
_ => "Old"
};
}
catch
{
return "Old";
}
}
private static bool IsPackageInstalled(string packageName)
{
try
{
return PackageInfo.FindForAssetPath("Packages/" + packageName) != null;
}
catch
{
return false;
}
}
}
}