From 424df879d4a7708375c6195c6605cbdf49696a1f Mon Sep 17 00:00:00 2001 From: Shutong Wu <51266340+Scriptwonder@users.noreply.github.com> Date: Fri, 30 Jan 2026 21:09:06 -0500 Subject: [PATCH] Update warning message for Camera Capture (#661) --- MCPForUnity/Editor/Tools/ManageScene.cs | 35 +++++++++++ .../Runtime/Helpers/ScreenshotUtility.cs | 62 +++++++++++++++++-- 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/MCPForUnity/Editor/Tools/ManageScene.cs b/MCPForUnity/Editor/Tools/ManageScene.cs index 2c72300..2ceecd9 100644 --- a/MCPForUnity/Editor/Tools/ManageScene.cs +++ b/MCPForUnity/Editor/Tools/ManageScene.cs @@ -361,6 +361,41 @@ namespace MCPForUnity.Editor.Tools { int resolvedSuperSize = (superSize.HasValue && superSize.Value > 0) ? superSize.Value : 1; + // Batch mode warning + if (Application.isBatchMode) + { + McpLog.Warn("[ManageScene] Screenshot capture in batch mode uses camera-based fallback. Results may vary."); + } + + // Check Screen Capture module availability and warn if not available + bool screenCaptureAvailable = ScreenshotUtility.IsScreenCaptureModuleAvailable; + bool hasCameraFallback = Camera.main != null || UnityEngine.Object.FindObjectsOfType().Length > 0; + +#if UNITY_2022_1_OR_NEWER + if (!screenCaptureAvailable && !hasCameraFallback) + { + return new ErrorResponse( + "Cannot capture screenshot. The Screen Capture module is not enabled and no Camera was found in the scene. " + + "Please either: (1) Enable the Screen Capture module: Window > Package Manager > Built-in > Screen Capture > Enable, " + + "or (2) Add a Camera to your scene for camera-based fallback capture." + ); + } + + if (!screenCaptureAvailable) + { + McpLog.Warn("[ManageScene] Screen Capture module not enabled. Using camera-based fallback. " + + "For best results, enable it: Window > Package Manager > Built-in > Screen Capture > Enable."); + } +#else + if (!hasCameraFallback) + { + return new ErrorResponse( + "No camera found in the scene. Screenshot capture on Unity versions before 2022.1 requires a Camera in the scene. " + + "Please add a Camera to your scene or upgrade to Unity 2022.1+ for ScreenCapture API support." + ); + } +#endif + // Best-effort: ensure Game View exists and repaints before capture. if (!Application.isBatchMode) { diff --git a/MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs b/MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs index 4a001b0..3176f0c 100644 --- a/MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs +++ b/MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs @@ -31,6 +31,34 @@ namespace MCPForUnity.Runtime.Helpers { private const string ScreenshotsFolderName = "Screenshots"; private static bool s_loggedLegacyScreenCaptureFallback; + private static bool? s_screenCaptureModuleAvailable; + + /// + /// Checks if the Screen Capture module (com.unity.modules.screencapture) is enabled. + /// This module can be disabled in Package Manager > Built-in, which removes the ScreenCapture class. + /// + public static bool IsScreenCaptureModuleAvailable + { + get + { + if (!s_screenCaptureModuleAvailable.HasValue) + { + // Check if ScreenCapture type exists (module might be disabled) + s_screenCaptureModuleAvailable = Type.GetType("UnityEngine.ScreenCapture, UnityEngine.ScreenCaptureModule") != null + || Type.GetType("UnityEngine.ScreenCapture, UnityEngine.CoreModule") != null; + } + return s_screenCaptureModuleAvailable.Value; + } + } + + /// + /// Error message to display when Screen Capture module is not available. + /// + public const string ScreenCaptureModuleNotAvailableError = + "The Screen Capture module (com.unity.modules.screencapture) is not enabled. " + + "To use screenshot capture with ScreenCapture API, please enable it in Unity: " + + "Window > Package Manager > Built-in > Screen Capture > Enable. " + + "Alternatively, MCP for Unity will use camera-based capture as a fallback if a Camera exists in the scene."; private static Camera FindAvailableCamera() { @@ -55,24 +83,46 @@ namespace MCPForUnity.Runtime.Helpers public static ScreenshotCaptureResult CaptureToAssetsFolder(string fileName = null, int superSize = 1, bool ensureUniqueFileName = true) { #if UNITY_2022_1_OR_NEWER - ScreenshotCaptureResult result = PrepareCaptureResult(fileName, superSize, ensureUniqueFileName, isAsync: true); - ScreenCapture.CaptureScreenshot(result.AssetsRelativePath, result.SuperSize); - return result; + // Check if Screen Capture module is available (can be disabled in Package Manager > Built-in) + if (IsScreenCaptureModuleAvailable) + { + ScreenshotCaptureResult result = PrepareCaptureResult(fileName, superSize, ensureUniqueFileName, isAsync: true); + ScreenCapture.CaptureScreenshot(result.AssetsRelativePath, result.SuperSize); + return result; + } + else + { + // Module disabled - try camera fallback + Debug.LogWarning("[MCP for Unity] " + ScreenCaptureModuleNotAvailableError); + return CaptureWithCameraFallback(fileName, superSize, ensureUniqueFileName); + } #else + // Unity < 2022.1 - always use camera fallback + return CaptureWithCameraFallback(fileName, superSize, ensureUniqueFileName); +#endif + } + + private static ScreenshotCaptureResult CaptureWithCameraFallback(string fileName, int superSize, bool ensureUniqueFileName) + { if (!s_loggedLegacyScreenCaptureFallback) { - Debug.Log("ScreenCapture is supported after Unity 2022.1. Using camera capture as fallback."); + Debug.Log("[MCP for Unity] Using camera-based screenshot capture. " + + "This requires a Camera in the scene. For best results on Unity 2022.1+, ensure the Screen Capture module is enabled: " + + "Window > Package Manager > Built-in > Screen Capture > Enable."); s_loggedLegacyScreenCaptureFallback = true; } var cam = FindAvailableCamera(); if (cam == null) { - throw new InvalidOperationException("No camera found to capture screenshot."); + throw new InvalidOperationException( + "No camera found to capture screenshot. Camera-based capture requires a Camera in the scene. " + + "Either add a Camera to your scene, or enable the Screen Capture module: " + + "Window > Package Manager > Built-in > Screen Capture > Enable." + ); } return CaptureFromCameraToAssetsFolder(cam, fileName, superSize, ensureUniqueFileName); -#endif } ///