@@ -390,14 +390,42 @@ static int Capture(CaptureOperation current)
390390 $ "expected { current . Width } x{ current . Height } .") ;
391391 }
392392
393+ // Windows ReadScreenPixel expects point-space origin/size.
394+ // macOS Retina needs pixel-space conversion + a screen-prefix read.
395+ return Application . platform == RuntimePlatform . OSXEditor
396+ ? CaptureMacOS ( current , pointRect )
397+ : CaptureWindows ( current , pointRect ) ;
398+ }
399+
400+ static int CaptureWindows ( CaptureOperation current , Rect pointRect )
401+ {
402+ var width = current . Width ;
403+ var height = current . Height ;
404+ var origin = new Vector2 (
405+ Mathf . Round ( pointRect . x ) ,
406+ Mathf . Round ( pointRect . y ) ) ;
407+ var pixels = UnityEditorInternal . InternalEditorUtility
408+ . ReadScreenPixel ( origin , width , height ) ;
409+ if ( pixels == null || pixels . Length != width * height )
410+ {
411+ throw new InvalidOperationException (
412+ $ "Expected { width * height } pixels but received " +
413+ $ "{ ( pixels == null ? 0 : pixels . Length ) } .") ;
414+ }
415+
416+ return WriteCapturePng ( pixels , width , height , current . OutputPath ) ;
417+ }
418+
419+ static int CaptureMacOS ( CaptureOperation current , Rect pointRect )
420+ {
393421 var pixelRect = EditorGUIUtility . PointsToPixels ( pointRect ) ;
394422 var pixelXMin = Mathf . RoundToInt ( pixelRect . xMin ) ;
395423 var pixelYMin = Mathf . RoundToInt ( pixelRect . yMin ) ;
396424 var pixelXMax = Mathf . RoundToInt ( pixelRect . xMax ) ;
397425 var pixelYMax = Mathf . RoundToInt ( pixelRect . yMax ) ;
398426 var pixelWidth = pixelXMax - pixelXMin ;
399427 var pixelHeight = pixelYMax - pixelYMin ;
400- var pixels = ReadWindowPixels (
428+ var pixels = ReadMacOSWindowPixels (
401429 pixelXMin ,
402430 pixelYMin ,
403431 pixelWidth ,
@@ -423,18 +451,7 @@ static int Capture(CaptureOperation current)
423451 sourceTexture ,
424452 current . Width ,
425453 current . Height ) ;
426- var png = outputTexture . EncodeToPNG ( ) ;
427- var directory = Path . GetDirectoryName ( current . OutputPath ) ;
428- if ( string . IsNullOrWhiteSpace ( directory ) )
429- {
430- throw new InvalidOperationException (
431- $ "Could not determine the output directory for " +
432- $ "'{ current . OutputPath } '.") ;
433- }
434-
435- Directory . CreateDirectory ( directory ) ;
436- File . WriteAllBytes ( current . OutputPath , png ) ;
437- return png . Length ;
454+ return WriteCapturePng ( outputTexture , current . OutputPath ) ;
438455 }
439456 finally
440457 {
@@ -447,21 +464,12 @@ static int Capture(CaptureOperation current)
447464 }
448465 }
449466
450- static Color [ ] ReadWindowPixels (
467+ static Color [ ] ReadMacOSWindowPixels (
451468 int x ,
452469 int y ,
453470 int width ,
454471 int height )
455472 {
456- if ( Application . platform != RuntimePlatform . OSXEditor )
457- {
458- return UnityEditorInternal . InternalEditorUtility
459- . ReadScreenPixel (
460- new Vector2 ( x , y ) ,
461- width ,
462- height ) ;
463- }
464-
465473 if ( x < 0 || y < 0 )
466474 {
467475 throw new InvalidOperationException (
@@ -503,6 +511,45 @@ static Color[] ReadWindowPixels(
503511 return result ;
504512 }
505513
514+ static int WriteCapturePng (
515+ Color [ ] pixels ,
516+ int width ,
517+ int height ,
518+ string outputPath )
519+ {
520+ var texture = new Texture2D (
521+ width ,
522+ height ,
523+ TextureFormat . RGB24 ,
524+ false ) ;
525+ try
526+ {
527+ texture . SetPixels ( pixels ) ;
528+ texture . Apply ( false , false ) ;
529+ return WriteCapturePng ( texture , outputPath ) ;
530+ }
531+ finally
532+ {
533+ Object . DestroyImmediate ( texture ) ;
534+ }
535+ }
536+
537+ static int WriteCapturePng ( Texture2D texture , string outputPath )
538+ {
539+ var png = texture . EncodeToPNG ( ) ;
540+ var directory = Path . GetDirectoryName ( outputPath ) ;
541+ if ( string . IsNullOrWhiteSpace ( directory ) )
542+ {
543+ throw new InvalidOperationException (
544+ $ "Could not determine the output directory for " +
545+ $ "'{ outputPath } '.") ;
546+ }
547+
548+ Directory . CreateDirectory ( directory ) ;
549+ File . WriteAllBytes ( outputPath , png ) ;
550+ return png . Length ;
551+ }
552+
506553 static Texture2D ResizeCapture (
507554 Texture2D source ,
508555 int width ,
0 commit comments