@@ -50,57 +50,6 @@ public static class ScreenshotUtility
5050 /// or globally via <c>ScreenshotPreferences</c> in the Editor assembly.
5151 /// </summary>
5252 public const string DefaultFolder = "Assets/Screenshots" ;
53- private static bool s_loggedLegacyScreenCaptureFallback ;
54- private static bool ? s_screenCaptureModuleAvailable ;
55- private static System . Reflection . MethodInfo s_captureScreenshotMethod ;
56- private static System . Reflection . MethodInfo s_captureScreenshotAsTextureMethod ;
57-
58- /// <summary>
59- /// Checks if the Screen Capture module (com.unity.modules.screencapture) is enabled.
60- /// This module can be disabled in Package Manager > Built-in, which removes the ScreenCapture class.
61- /// </summary>
62- public static bool IsScreenCaptureModuleAvailable
63- {
64- get
65- {
66- if ( ! s_screenCaptureModuleAvailable . HasValue )
67- {
68- // Check if ScreenCapture type exists (module might be disabled in Package Manager > Built-in)
69- var screenCaptureType = Type . GetType ( "UnityEngine.ScreenCapture, UnityEngine.ScreenCaptureModule" )
70- ?? Type . GetType ( "UnityEngine.ScreenCapture, UnityEngine.CoreModule" ) ;
71- s_screenCaptureModuleAvailable = screenCaptureType != null ;
72- if ( screenCaptureType != null )
73- {
74- s_captureScreenshotMethod = screenCaptureType . GetMethod ( "CaptureScreenshot" ,
75- new Type [ ] { typeof ( string ) , typeof ( int ) } ) ;
76- s_captureScreenshotAsTextureMethod = screenCaptureType . GetMethod ( "CaptureScreenshotAsTexture" ,
77- new Type [ ] { typeof ( int ) } ) ;
78- }
79- }
80- return s_screenCaptureModuleAvailable . Value ;
81- }
82- }
83-
84- /// <summary>
85- /// Reflective invocation of ScreenCapture.CaptureScreenshotAsTexture(int). Returns
86- /// null when the Screen Capture module is disabled. Centralised so the only direct
87- /// reference to ScreenCapture lives here.
88- /// </summary>
89- internal static Texture2D InvokeCaptureScreenshotAsTexture ( int superSize )
90- {
91- if ( ! IsScreenCaptureModuleAvailable || s_captureScreenshotAsTextureMethod == null )
92- return null ;
93- return s_captureScreenshotAsTextureMethod . Invoke ( null , new object [ ] { superSize } ) as Texture2D ;
94- }
95-
96- /// <summary>
97- /// Error message to display when Screen Capture module is not available.
98- /// </summary>
99- public const string ScreenCaptureModuleNotAvailableError =
100- "The Screen Capture module (com.unity.modules.screencapture) is not enabled. " +
101- "To use screenshot capture with ScreenCapture API, please enable it in Unity: " +
102- "Window > Package Manager > Built-in > Screen Capture > Enable. " +
103- "Alternatively, MCP for Unity will use camera-based capture as a fallback if a Camera exists in the scene." ;
10453
10554 private static Camera FindAvailableCamera ( )
10655 {
@@ -127,44 +76,10 @@ public static ScreenshotCaptureResult CaptureToProjectFolder(
12776 bool ensureUniqueFileName = true ,
12877 string folderOverride = null )
12978 {
130- // Use reflection to call ScreenCapture.CaptureScreenshot so the code compiles
131- // even when the Screen Capture module (com.unity.modules.screencapture) is disabled.
132- if ( IsScreenCaptureModuleAvailable && s_captureScreenshotMethod != null )
133- {
134- ScreenshotCaptureResult result = PrepareCaptureResult ( fileName , superSize , ensureUniqueFileName , folderOverride , isAsync : true ) ;
135- // ScreenCapture.CaptureScreenshot accepts paths relative to the project root.
136- s_captureScreenshotMethod . Invoke ( null , new object [ ] { result . ProjectRelativePath , result . SuperSize } ) ;
137- return result ;
138- }
139- else
140- {
141- // Module disabled or unavailable - try camera fallback
142- Debug . LogWarning ( "[MCP for Unity] " + ScreenCaptureModuleNotAvailableError ) ;
143- return CaptureWithCameraFallback ( fileName , superSize , ensureUniqueFileName , folderOverride ) ;
144- }
145- }
146-
147- private static ScreenshotCaptureResult CaptureWithCameraFallback ( string fileName , int superSize , bool ensureUniqueFileName , string folderOverride )
148- {
149- if ( ! s_loggedLegacyScreenCaptureFallback )
150- {
151- Debug . Log ( "[MCP for Unity] Using camera-based screenshot capture. " +
152- "This requires a Camera in the scene. For best results on Unity 2022.1+, ensure the Screen Capture module is enabled: " +
153- "Window > Package Manager > Built-in > Screen Capture > Enable." ) ;
154- s_loggedLegacyScreenCaptureFallback = true ;
155- }
156-
157- var cam = FindAvailableCamera ( ) ;
158- if ( cam == null )
159- {
160- throw new InvalidOperationException (
161- "No camera found to capture screenshot. Camera-based capture requires a Camera in the scene. " +
162- "Either add a Camera to your scene, or enable the Screen Capture module: " +
163- "Window > Package Manager > Built-in > Screen Capture > Enable."
164- ) ;
165- }
166-
167- return CaptureFromCameraToProjectFolder ( cam , fileName , superSize , ensureUniqueFileName , folderOverride : folderOverride ) ;
79+ ScreenshotCaptureResult result = PrepareCaptureResult ( fileName , superSize , ensureUniqueFileName , folderOverride , isAsync : true ) ;
80+ // ScreenCapture.CaptureScreenshot accepts paths relative to the project root.
81+ ScreenCapture . CaptureScreenshot ( result . ProjectRelativePath , result . SuperSize ) ;
82+ return result ;
16883 }
16984
17085 /// <summary>
@@ -283,7 +198,7 @@ private static Texture2D CaptureCompositedAfterFrame(int superSize, int timeoutS
283198 /// <summary>
284199 /// Captures a screenshot using ScreenCapture.CaptureScreenshotAsTexture, which captures the
285200 /// final composited frame including UI Toolkit overlays, post-processing, etc.
286- /// Falls back to camera-based capture if ScreenCapture is unavailable .
201+ /// Falls back to camera-based capture if ScreenCapture returns null at runtime .
287202 /// </summary>
288203 public static ScreenshotCaptureResult CaptureComposited (
289204 string fileName = null ,
@@ -293,16 +208,6 @@ public static ScreenshotCaptureResult CaptureComposited(
293208 int maxResolution = 0 ,
294209 string folderOverride = null )
295210 {
296- if ( ! IsScreenCaptureModuleAvailable || s_captureScreenshotAsTextureMethod == null )
297- {
298- var fallbackCamera = FindAvailableCamera ( ) ;
299- if ( fallbackCamera != null )
300- return CaptureFromCameraToProjectFolder ( fallbackCamera , fileName , superSize , ensureUniqueFileName ,
301- includeImage , maxResolution , folderOverride : folderOverride ) ;
302-
303- throw new InvalidOperationException ( "ScreenCapture module is unavailable and no fallback camera found." ) ;
304- }
305-
306211 ScreenshotCaptureResult result = PrepareCaptureResult ( fileName , superSize , ensureUniqueFileName , folderOverride : folderOverride , isAsync : false ) ;
307212 Texture2D tex = null ;
308213 Texture2D downscaled = null ;
@@ -315,9 +220,9 @@ public static ScreenshotCaptureResult CaptureComposited(
315220 // composited; route through WaitForEndOfFrame instead.
316221 tex = Application . isPlaying
317222 ? CaptureCompositedAfterFrame ( result . SuperSize )
318- : InvokeCaptureScreenshotAsTexture ( result . SuperSize ) ;
223+ : ScreenCapture . CaptureScreenshotAsTexture ( result . SuperSize ) ;
319224#else
320- tex = InvokeCaptureScreenshotAsTexture ( result . SuperSize ) ;
225+ tex = ScreenCapture . CaptureScreenshotAsTexture ( result . SuperSize ) ;
321226#endif
322227 if ( tex == null )
323228 {
@@ -857,7 +762,7 @@ private System.Collections.IEnumerator Start()
857762 {
858763 yield return new WaitForEndOfFrame ( ) ;
859764 Texture2D tex = null ;
860- try { tex = ScreenshotUtility . InvokeCaptureScreenshotAsTexture ( _superSize ) ; }
765+ try { tex = ScreenCapture . CaptureScreenshotAsTexture ( _superSize ) ; }
861766 catch ( Exception ex ) { Debug . LogError ( $ "[MCP for Unity] CaptureScreenshotAsTexture failed: { ex . Message } ") ; }
862767 _onComplete ? . Invoke ( tex ) ;
863768 Destroy ( gameObject ) ;
0 commit comments