Implement screenshot capture for Unity 2022.1+

Add conditional screenshot capture for Unity 2022.1 and newer.
main
Shutong Wu 2025-12-10 12:15:29 -05:00 committed by GitHub
parent 2f2b56a20d
commit a9878622ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 2 deletions

View File

@ -41,12 +41,15 @@ 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);
#if UNITY_2022_1_OR_NEWER #if UNITY_2022_1_OR_NEWER
ScreenCapture.CaptureScreenshot(captureName, size); 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 #endif
Debug.Log($"Screenshot requested: file='{captureName}' intendedFullPath='{normalizedFullPath}' persistentDataPath='{Application.persistentDataPath}'");
string projectRoot = GetProjectRootPath(); string projectRoot = GetProjectRootPath();
string assetsRelativePath = normalizedFullPath; string assetsRelativePath = normalizedFullPath;
if (assetsRelativePath.StartsWith(projectRoot, StringComparison.OrdinalIgnoreCase)) if (assetsRelativePath.StartsWith(projectRoot, StringComparison.OrdinalIgnoreCase))