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

@ -3,7 +3,7 @@ using System.IO;
using System.Linq;
using UnityEngine;
namespace MCPForUnity.Runtime.Helpers
namespace MCPForUnity.Runtime.Helpers
//The reason for having another Runtime Utilities in additional to Editor Utilities is to avoid Editor-only dependencies in this runtime code.
{
public readonly struct ScreenshotCaptureResult
@ -42,14 +42,7 @@ namespace MCPForUnity.Runtime.Helpers
// Use only the file name to let Unity decide the final location (per CaptureScreenshot docs).
string captureName = Path.GetFileName(normalizedFullPath);
#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
// Use Asset folder for ScreenCapture.CaptureScreenshot to ensure write to asset rather than project root
string projectRoot = GetProjectRootPath();
string assetsRelativePath = normalizedFullPath;
if (assetsRelativePath.StartsWith(projectRoot, StringComparison.OrdinalIgnoreCase))
@ -57,6 +50,13 @@ namespace MCPForUnity.Runtime.Helpers
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(
normalizedFullPath,
assetsRelativePath,