Skip to content

Commit 7aa4315

Browse files
committed
fix: address screenshot PR feedback
1 parent 56a14af commit 7aa4315

2 files changed

Lines changed: 38 additions & 52 deletions

File tree

MCPForUnity/Editor/Tools/ManageScene.cs

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -602,23 +602,7 @@ private static object CaptureScreenshot(SceneCommand cmd)
602602

603603
AssetDatabase.ImportAsset(result.AssetsRelativePath, ImportAssetOptions.ForceSynchronousImport);
604604
string message = $"Screenshot captured to '{result.AssetsRelativePath}' (camera: {targetCamera.name}).";
605-
606-
var data = new Dictionary<string, object>
607-
{
608-
{ "path", result.AssetsRelativePath },
609-
{ "fullPath", result.FullPath },
610-
{ "superSize", result.SuperSize },
611-
{ "isAsync", false },
612-
{ "camera", targetCamera.name },
613-
{ "captureSource", "game_view" },
614-
};
615-
if (includeImage && result.ImageBase64 != null)
616-
{
617-
data["imageBase64"] = result.ImageBase64;
618-
data["imageWidth"] = result.ImageWidth;
619-
data["imageHeight"] = result.ImageHeight;
620-
}
621-
return new SuccessResponse(message, data);
605+
return new SuccessResponse(message, BuildScreenshotResponseData(result, targetCamera.name, includeImage));
622606
}
623607

624608
if (includeImage && Application.isPlaying)
@@ -632,23 +616,7 @@ private static object CaptureScreenshot(SceneCommand cmd)
632616
AssetDatabase.ImportAsset(result.AssetsRelativePath, ImportAssetOptions.ForceSynchronousImport);
633617
string cameraName = Camera.main != null ? Camera.main.name : "composited";
634618
string message = $"Screenshot captured to '{result.AssetsRelativePath}' (camera: {cameraName}).";
635-
636-
var data = new Dictionary<string, object>
637-
{
638-
{ "path", result.AssetsRelativePath },
639-
{ "fullPath", result.FullPath },
640-
{ "superSize", result.SuperSize },
641-
{ "isAsync", false },
642-
{ "camera", cameraName },
643-
{ "captureSource", "game_view" },
644-
};
645-
if (result.ImageBase64 != null)
646-
{
647-
data["imageBase64"] = result.ImageBase64;
648-
data["imageWidth"] = result.ImageWidth;
649-
data["imageHeight"] = result.ImageHeight;
650-
}
651-
return new SuccessResponse(message, data);
619+
return new SuccessResponse(message, BuildScreenshotResponseData(result, cameraName, includeImage: true));
652620
}
653621

654622
if (includeImage)
@@ -673,23 +641,7 @@ private static object CaptureScreenshot(SceneCommand cmd)
673641

674642
AssetDatabase.ImportAsset(result.AssetsRelativePath, ImportAssetOptions.ForceSynchronousImport);
675643
string message = $"Screenshot captured to '{result.AssetsRelativePath}' (camera: {targetCamera.name}).";
676-
677-
var data = new Dictionary<string, object>
678-
{
679-
{ "path", result.AssetsRelativePath },
680-
{ "fullPath", result.FullPath },
681-
{ "superSize", result.SuperSize },
682-
{ "isAsync", false },
683-
{ "camera", targetCamera.name },
684-
{ "captureSource", "game_view" },
685-
};
686-
if (includeImage && result.ImageBase64 != null)
687-
{
688-
data["imageBase64"] = result.ImageBase64;
689-
data["imageWidth"] = result.ImageWidth;
690-
data["imageHeight"] = result.ImageHeight;
691-
}
692-
return new SuccessResponse(message, data);
644+
return new SuccessResponse(message, BuildScreenshotResponseData(result, targetCamera.name, includeImage));
693645
}
694646

695647
// Default path: use ScreenCapture API if available, camera fallback otherwise
@@ -746,6 +698,31 @@ private static object CaptureScreenshot(SceneCommand cmd)
746698
}
747699
}
748700

701+
private static Dictionary<string, object> BuildScreenshotResponseData(
702+
ScreenshotCaptureResult result,
703+
string cameraName,
704+
bool includeImage)
705+
{
706+
var data = new Dictionary<string, object>
707+
{
708+
{ "path", result.AssetsRelativePath },
709+
{ "fullPath", result.FullPath },
710+
{ "superSize", result.SuperSize },
711+
{ "isAsync", false },
712+
{ "camera", cameraName },
713+
{ "captureSource", "game_view" },
714+
};
715+
716+
if (includeImage && result.ImageBase64 != null)
717+
{
718+
data["imageBase64"] = result.ImageBase64;
719+
data["imageWidth"] = result.ImageWidth;
720+
data["imageHeight"] = result.ImageHeight;
721+
}
722+
723+
return data;
724+
}
725+
749726
private static object CaptureSceneViewScreenshot(
750727
SceneCommand cmd,
751728
string fileName,

MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ public static ScreenshotCaptureResult CaptureComposited(
236236
bool includeImage = false,
237237
int maxResolution = 0)
238238
{
239+
if (!IsScreenCaptureModuleAvailable)
240+
{
241+
var fallbackCamera = FindAvailableCamera();
242+
if (fallbackCamera != null)
243+
return CaptureFromCameraToAssetsFolder(fallbackCamera, fileName, superSize, ensureUniqueFileName, includeImage, maxResolution);
244+
245+
throw new InvalidOperationException("ScreenCapture module is unavailable and no fallback camera found.");
246+
}
247+
239248
ScreenshotCaptureResult result = PrepareCaptureResult(fileName, superSize, ensureUniqueFileName, isAsync: false);
240249
Texture2D tex = null;
241250
Texture2D downscaled = null;
@@ -247,7 +256,7 @@ public static ScreenshotCaptureResult CaptureComposited(
247256
if (tex == null)
248257
{
249258
// Fallback to camera-based if ScreenCapture fails
250-
var cam = Camera.main;
259+
var cam = FindAvailableCamera();
251260
if (cam != null)
252261
return CaptureFromCameraToAssetsFolder(cam, fileName, superSize, ensureUniqueFileName, includeImage, maxResolution);
253262
throw new InvalidOperationException("ScreenCapture.CaptureScreenshotAsTexture returned null and no fallback camera available.");

0 commit comments

Comments
 (0)