@@ -591,22 +591,48 @@ private static object CaptureScreenshot(SceneCommand cmd)
591591 }
592592 }
593593
594- // When a specific camera is requested or include_image is true, always use camera-based capture
595- // (synchronous, gives us bytes in memory for base64).
596- if ( targetCamera != null || includeImage )
594+ // When include_image is requested but no specific camera, use composited capture
595+ // (ScreenCapture.CaptureScreenshotAsTexture) which captures UI Toolkit overlays.
596+ // When a specific camera IS requested, use camera-based capture.
597+ if ( targetCamera != null )
597598 {
599+ if ( ! Application . isBatchMode ) EnsureGameView ( ) ;
600+
601+ ScreenshotCaptureResult result = ScreenshotUtility . CaptureFromCameraToAssetsFolder (
602+ targetCamera , fileName , resolvedSuperSize , ensureUniqueFileName : true ,
603+ includeImage : includeImage , maxResolution : maxResolution ) ;
604+
605+ AssetDatabase . ImportAsset ( result . AssetsRelativePath , ImportAssetOptions . ForceSynchronousImport ) ;
606+ string message = $ "Screenshot captured to '{ result . AssetsRelativePath } ' (camera: { targetCamera . name } ).";
607+ return new SuccessResponse ( message , BuildScreenshotResponseData ( result , targetCamera . name , includeImage ) ) ;
608+ }
609+
610+ if ( includeImage && Application . isPlaying )
611+ {
612+ if ( ! Application . isBatchMode ) EnsureGameView ( ) ;
613+
614+ ScreenshotCaptureResult result = ScreenshotUtility . CaptureComposited (
615+ fileName , resolvedSuperSize , ensureUniqueFileName : true ,
616+ includeImage : true , maxResolution : maxResolution ) ;
617+
618+ AssetDatabase . ImportAsset ( result . AssetsRelativePath , ImportAssetOptions . ForceSynchronousImport ) ;
619+ string cameraName = Camera . main != null ? Camera . main . name : "composited" ;
620+ string message = $ "Screenshot captured to '{ result . AssetsRelativePath } ' (camera: { cameraName } ).";
621+ return new SuccessResponse ( message , BuildScreenshotResponseData ( result , cameraName , includeImage : true ) ) ;
622+ }
623+
624+ if ( includeImage )
625+ {
626+ // Not in play mode — fall back to camera-based capture
627+ targetCamera = Camera . main ;
598628 if ( targetCamera == null )
599629 {
600- targetCamera = Camera . main ;
601- if ( targetCamera == null )
602- {
603- var allCams = UnityFindObjectsCompat . FindAll < Camera > ( ) ;
604- targetCamera = allCams . Length > 0 ? allCams [ 0 ] : null ;
605- }
630+ var allCams = UnityFindObjectsCompat . FindAll < Camera > ( ) ;
631+ targetCamera = allCams . Length > 0 ? allCams [ 0 ] : null ;
606632 }
607633 if ( targetCamera == null )
608634 {
609- return new ErrorResponse ( "No camera found in the scene. Add a Camera to use screenshot with camera or include_image ." ) ;
635+ return new ErrorResponse ( "No camera found in the scene. Add a Camera to use screenshot with include_image outside of Play mode ." ) ;
610636 }
611637
612638 if ( ! Application . isBatchMode ) EnsureGameView ( ) ;
@@ -624,7 +650,6 @@ private static object CaptureScreenshot(SceneCommand cmd)
624650 {
625651 return new ErrorResponse ( ex . Message ) ;
626652 }
627-
628653 if ( ScreenshotUtility . IsUnderAssets ( result . ProjectRelativePath ) )
629654 AssetDatabase . ImportAsset ( result . ProjectRelativePath , ImportAssetOptions . ForceSynchronousImport ) ;
630655 string message = $ "Screenshot captured to '{ result . ProjectRelativePath } ' (camera: { targetCamera . name } ).";
@@ -644,7 +669,7 @@ private static object CaptureScreenshot(SceneCommand cmd)
644669 data [ "imageWidth" ] = result . ImageWidth ;
645670 data [ "imageHeight" ] = result . ImageHeight ;
646671 }
647- return new SuccessResponse ( message , data ) ;
672+ return new SuccessResponse ( message , BuildScreenshotResponseData ( result , targetCamera . name , includeImage ) ) ;
648673 }
649674
650675 // Default path: use ScreenCapture API if available, camera fallback otherwise
@@ -715,6 +740,31 @@ private static object CaptureScreenshot(SceneCommand cmd)
715740 }
716741 }
717742
743+ private static Dictionary < string , object > BuildScreenshotResponseData (
744+ ScreenshotCaptureResult result ,
745+ string cameraName ,
746+ bool includeImage )
747+ {
748+ var data = new Dictionary < string , object >
749+ {
750+ { "path" , result . AssetsRelativePath } ,
751+ { "fullPath" , result . FullPath } ,
752+ { "superSize" , result . SuperSize } ,
753+ { "isAsync" , false } ,
754+ { "camera" , cameraName } ,
755+ { "captureSource" , "game_view" } ,
756+ } ;
757+
758+ if ( includeImage && result . ImageBase64 != null )
759+ {
760+ data [ "imageBase64" ] = result . ImageBase64 ;
761+ data [ "imageWidth" ] = result . ImageWidth ;
762+ data [ "imageHeight" ] = result . ImageHeight ;
763+ }
764+
765+ return data ;
766+ }
767+
718768 private static object CaptureSceneViewScreenshot (
719769 SceneCommand cmd ,
720770 string fileName ,
0 commit comments