Update warning message for Camera Capture (#661)

main
Shutong Wu 2026-01-30 21:09:06 -05:00 committed by GitHub
parent 140a7e5c55
commit 424df879d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 91 additions and 6 deletions

View File

@ -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<Camera>().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)
{

View File

@ -31,6 +31,34 @@ namespace MCPForUnity.Runtime.Helpers
{
private const string ScreenshotsFolderName = "Screenshots";
private static bool s_loggedLegacyScreenCaptureFallback;
private static bool? s_screenCaptureModuleAvailable;
/// <summary>
/// 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.
/// </summary>
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;
}
}
/// <summary>
/// Error message to display when Screen Capture module is not available.
/// </summary>
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
}
/// <summary>