Skip to content

Commit c641c8c

Browse files
dsarnoclaude
andcommitted
fix(screenshot): propagate folderOverride to composited and specific-camera paths
Two pre-existing inconsistencies surfaced by CodeRabbit on #1103: 1. CaptureComposited dropped the caller's output_folder by hardcoding folderOverride: null in PrepareCaptureResult and the camera fallbacks. Adds the parameter to CaptureComposited's signature and plumbs it through both fallback paths. 2. The targetCamera and includeImage-in-play paths in ManageScene's game_view screenshot did not resolve cmd.outputFolder, so a request that selected a specific camera would always write to the default folder. Resolve via ScreenshotPreferences.Resolve as the other paths already do, and gate AssetDatabase.ImportAsset on IsUnderAssets so non-Assets folders don't trigger a futile import. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 642687f commit c641c8c

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

MCPForUnity/Editor/Tools/ManageScene.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,14 @@ private static object CaptureScreenshot(SceneCommand cmd)
598598
{
599599
if (!Application.isBatchMode) EnsureGameView();
600600

601+
string folderOverride = ScreenshotPreferences.Resolve(cmd.outputFolder);
601602
ScreenshotCaptureResult result = ScreenshotUtility.CaptureFromCameraToProjectFolder(
602603
targetCamera, fileName, resolvedSuperSize, ensureUniqueFileName: true,
603-
includeImage: includeImage, maxResolution: maxResolution);
604+
includeImage: includeImage, maxResolution: maxResolution,
605+
folderOverride: folderOverride);
604606

605-
AssetDatabase.ImportAsset(result.ProjectRelativePath, ImportAssetOptions.ForceSynchronousImport);
607+
if (ScreenshotUtility.IsUnderAssets(result.ProjectRelativePath))
608+
AssetDatabase.ImportAsset(result.ProjectRelativePath, ImportAssetOptions.ForceSynchronousImport);
606609
string message = $"Screenshot captured to '{result.ProjectRelativePath}' (camera: {targetCamera.name}).";
607610
return new SuccessResponse(message, BuildScreenshotResponseData(result, targetCamera.name, includeImage));
608611
}
@@ -611,11 +614,14 @@ private static object CaptureScreenshot(SceneCommand cmd)
611614
{
612615
if (!Application.isBatchMode) EnsureGameView();
613616

617+
string folderOverride = ScreenshotPreferences.Resolve(cmd.outputFolder);
614618
ScreenshotCaptureResult result = ScreenshotUtility.CaptureComposited(
615619
fileName, resolvedSuperSize, ensureUniqueFileName: true,
616-
includeImage: true, maxResolution: maxResolution);
620+
includeImage: true, maxResolution: maxResolution,
621+
folderOverride: folderOverride);
617622

618-
AssetDatabase.ImportAsset(result.ProjectRelativePath, ImportAssetOptions.ForceSynchronousImport);
623+
if (ScreenshotUtility.IsUnderAssets(result.ProjectRelativePath))
624+
AssetDatabase.ImportAsset(result.ProjectRelativePath, ImportAssetOptions.ForceSynchronousImport);
619625
string cameraName = Camera.main != null ? Camera.main.name : "composited";
620626
string message = $"Screenshot captured to '{result.ProjectRelativePath}' (camera: {cameraName}).";
621627
return new SuccessResponse(message, BuildScreenshotResponseData(result, cameraName, includeImage: true));

MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,20 @@ public static ScreenshotCaptureResult CaptureComposited(
246246
int superSize = 1,
247247
bool ensureUniqueFileName = true,
248248
bool includeImage = false,
249-
int maxResolution = 0)
249+
int maxResolution = 0,
250+
string folderOverride = null)
250251
{
251252
if (!IsScreenCaptureModuleAvailable)
252253
{
253254
var fallbackCamera = FindAvailableCamera();
254255
if (fallbackCamera != null)
255-
return CaptureFromCameraToProjectFolder(fallbackCamera, fileName, superSize, ensureUniqueFileName, includeImage, maxResolution);
256+
return CaptureFromCameraToProjectFolder(fallbackCamera, fileName, superSize, ensureUniqueFileName,
257+
includeImage, maxResolution, folderOverride: folderOverride);
256258

257259
throw new InvalidOperationException("ScreenCapture module is unavailable and no fallback camera found.");
258260
}
259261

260-
ScreenshotCaptureResult result = PrepareCaptureResult(fileName, superSize, ensureUniqueFileName, folderOverride: null, isAsync: false);
262+
ScreenshotCaptureResult result = PrepareCaptureResult(fileName, superSize, ensureUniqueFileName, folderOverride: folderOverride, isAsync: false);
261263
Texture2D tex = null;
262264
Texture2D downscaled = null;
263265
string imageBase64 = null;
@@ -270,7 +272,8 @@ public static ScreenshotCaptureResult CaptureComposited(
270272
// Fallback to camera-based if ScreenCapture fails
271273
var cam = FindAvailableCamera();
272274
if (cam != null)
273-
return CaptureFromCameraToProjectFolder(cam, fileName, superSize, ensureUniqueFileName, includeImage, maxResolution);
275+
return CaptureFromCameraToProjectFolder(cam, fileName, superSize, ensureUniqueFileName,
276+
includeImage, maxResolution, folderOverride: folderOverride);
274277
throw new InvalidOperationException("ScreenCapture.CaptureScreenshotAsTexture returned null and no fallback camera available.");
275278
}
276279

0 commit comments

Comments
 (0)