Fix on Issue #465 (#477)

* Update ScreenshotUtility.cs

Fix issue in issue#465

* Update ScreenshotUtility.cs
main
Shutong Wu 2025-12-22 13:36:55 -05:00 committed by GitHub
parent df906cb747
commit 90758f19f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 9 deletions

View File

@ -42,14 +42,7 @@ namespace MCPForUnity.Runtime.Helpers
// Use only the file name to let Unity decide the final location (per CaptureScreenshot docs). // Use only the file name to let Unity decide the final location (per CaptureScreenshot docs).
string captureName = Path.GetFileName(normalizedFullPath); string captureName = Path.GetFileName(normalizedFullPath);
// Use Asset folder for ScreenCapture.CaptureScreenshot to ensure write to asset rather than project root
#if UNITY_2022_1_OR_NEWER
ScreenCapture.CaptureScreenshot(captureName, size);
#else
Debug.LogWarning("ScreenCapture is supported after Unity 2022.1. Using main camera capture as fallback.");
CaptureFromCameraToAssetsFolder(Camera.main, captureName, size, false);
#endif
string projectRoot = GetProjectRootPath(); string projectRoot = GetProjectRootPath();
string assetsRelativePath = normalizedFullPath; string assetsRelativePath = normalizedFullPath;
if (assetsRelativePath.StartsWith(projectRoot, StringComparison.OrdinalIgnoreCase)) if (assetsRelativePath.StartsWith(projectRoot, StringComparison.OrdinalIgnoreCase))
@ -57,6 +50,13 @@ namespace MCPForUnity.Runtime.Helpers
assetsRelativePath = assetsRelativePath.Substring(projectRoot.Length).TrimStart('/'); assetsRelativePath = assetsRelativePath.Substring(projectRoot.Length).TrimStart('/');
} }
#if UNITY_2022_1_OR_NEWER
ScreenCapture.CaptureScreenshot(assetsRelativePath, size);
#else
Debug.LogWarning("ScreenCapture is supported after Unity 2022.1. Using main camera capture as fallback.");
CaptureFromCameraToAssetsFolder(Camera.main, captureName, size, false);
#endif
return new ScreenshotCaptureResult( return new ScreenshotCaptureResult(
normalizedFullPath, normalizedFullPath,
assetsRelativePath, assetsRelativePath,