Beispiel #1
0
        /// <summary>
        /// Computes the inner image size to be used to capture the game screenshot.
        /// It enables the composition canvas at the desired composition resolution, and look for the raw image size and scale.
        /// </summary>
        public virtual IEnumerator ComputeInnerTextureSizeCoroutine(RawImage image, int compositionWidth, int compositionHeight, ScreenshotResolution resolution)
        {
            m_Canvas.gameObject.SetActive(true);

            // Resize the gameview at the composition resolution, and wait for a few frames
            GameViewController.SaveCurrentGameViewSize();
            for (int f = 0; f < resizeframe; ++f)
            {
                GameViewController.SetGameViewSize(compositionWidth, compositionHeight);
                yield return(new WaitForEndOfFrame());
            }

            // Get the raw image size
            Rect r = image.rectTransform.rect;

            resolution.m_Width  = (int)(r.width * image.rectTransform.lossyScale.x * supersampleCoeff);
            resolution.m_Height = (int)(r.height * image.rectTransform.lossyScale.y * supersampleCoeff);
            //			Debug.Log ("Raw image size " + resolution);

            // Restore all
            m_Canvas.gameObject.SetActive(false);
            GameViewController.RestoreGameViewSize();
        }
Beispiel #2
0
        public IEnumerator CaptureAllCoroutine(List <ScreenshotResolution> resolutions,
                                               List <ScreenshotCamera> cameras,
                                               List <ScreenshotOverlay> overlays,
                                               CaptureMode captureMode,
                                               int antiAliasing        = 8,
                                               bool captureGameUI      = true,
                                               ColorFormat colorFormat = ColorFormat.RGB,
                                               bool recomputeAlphaMask = false,
                                               bool stopTime           = false,
                                               bool restore            = true)
        {
            //			Debug.Log ("Capture all");

            if (resolutions == null)
            {
                Debug.LogError("Resolution list is null.");
                yield break;
            }
            if (cameras == null)
            {
                Debug.LogError("Cameras list is null.");
                yield break;
            }
            if (cameras.Count == 0 && captureMode == CaptureMode.RENDER_TO_TEXTURE)
            {
                cameras.Add(new ScreenshotCamera(Camera.main));
            }
            if (overlays == null)
            {
                Debug.LogError("Overlays list is null.");
                yield break;
            }
            if (captureMode == CaptureMode.RENDER_TO_TEXTURE && !UnityVersion.HasPro())
            {
                Debug.LogError("RENDER_TO_TEXTURE requires Unity Pro or Unity 5.0 and later.");
                yield break;
            }

            // If a capture is in progress we wait until we can take the screenshot
            if (m_IsRunning == true)
            {
                Debug.LogWarning("A capture process is already running.");
            }
            while (m_IsRunning == true)
            {
                yield return(null);
            }


#if (!UNITY_EDITOR && !UNITY_STANDALONE_WIN)
            if (captureMode == CaptureMode.GAMEVIEW_RESIZING)
            {
                Debug.LogError("GAMEVIEW_RESIZING capture mode is only available for Editor and Windows Standalone.");
                yield break;
            }
#endif

            // Init
            m_IsRunning = true;

            // Stop the time so all screenshots are exactly the same
            if (Application.isPlaying && stopTime)
            {
                StopTime();
            }


            // Apply settings: enable and disable the cameras and canvas
            ApplySettings(cameras, overlays, captureMode, captureGameUI);


            // Save the screen config to be restored after the capture process
            if (captureMode == CaptureMode.GAMEVIEW_RESIZING)
            {
                GameViewController.SaveCurrentGameViewSize();

#if !UNITY_EDITOR && UNITY_STANDALONE_WIN
                yield return(null);

                yield return(new WaitForEndOfFrame());
#endif
            }

            // Capture all resolutions
            foreach (ScreenshotResolution resolution in resolutions)
            {
                if (!resolution.IsValid())
                {
                    continue;
                }

                // Delegate call
                onResolutionUpdateStartDelegate(resolution);

                if (colorFormat == ColorFormat.RGBA && recomputeAlphaMask)
                {
                    yield return(StartCoroutine(CaptureAlphaMaskCoroutine(resolution, captureMode, antiAliasing, colorFormat)));
                }
                else
                {
                    yield return(StartCoroutine(CaptureResolutionTextureCoroutine(resolution, captureMode, antiAliasing, colorFormat)));
                }

                // Delegate call
                onResolutionUpdateEndDelegate(resolution);



#if UNITY_EDITOR
                // Dirty hack: we force a gameview repaint, to prevent the coroutine to stay locked.
                if (!Application.isPlaying)
                {
                    GameViewUtils.GetGameView().Repaint();
                }
#endif


                //				if (captureMode == CaptureMode.RENDER_TO_TEXTURE && captureGameUI) {
                //					RestoreCanvasRenderMode ();
                //				}
            }

            // Restore screen config
            if (restore && captureMode == CaptureMode.GAMEVIEW_RESIZING)
            {
                GameViewController.RestoreGameViewSize();
            }


#if (UNITY_EDITOR && !UNITY_5_4_OR_NEWER)
            // Call restore layout for old unity versions
            if (restore && captureMode == CaptureMode.GAMEVIEW_RESIZING)
            {
                m_NeedRestoreLayout = true;
            }
#endif

#if UNITY_EDITOR
            // Dirty hack, try to force an editor Update() to get the gameview back to normal
            if (!Application.isPlaying)
            {
                GameViewUtils.GetGameView().Repaint();
            }
#endif

            // Restore everything
            if (Application.isPlaying && stopTime)
            {
                RestoreTime();
            }
            if (Application.isEditor || restore)
            {
                RestoreSettings();
            }

            // End
            m_IsRunning = false;
        }
        public IEnumerator CaptureScreenshotsCoroutine(List <ScreenshotResolution> resolutions,
                                                       List <ScreenshotCamera> cameras,
                                                       List <ScreenshotOverlay> overlays,
                                                       CaptureMode captureMode,
                                                       int antiAliasing = 8,
                                                       bool export      = false,
                                                       TextureExporter.ImageFormat imageFormat = TextureExporter.ImageFormat.PNG,
                                                       int JPGQuality          = 100,
                                                       bool renderUI           = true,
                                                       bool playSound          = false,
                                                       ColorFormat colorFormat = ColorFormat.RGB,
                                                       bool recomputeAlphaMask = false,
                                                       bool stopTime           = true,
                                                       bool restore            = true)
        {
            if (resolutions == null)
            {
                Debug.LogError("Resolution list is null.");
                yield break;
            }
            if (cameras == null)
            {
                Debug.LogError("Cameras list is null.");
                yield break;
            }
            if (overlays == null)
            {
                Debug.LogError("Overlays list is null.");
                yield break;
            }
            if (m_IsRunning == true)
            {
                Debug.LogError("A capture process is already running.");
                yield break;
            }

            if (captureMode == CaptureMode.RENDER_TO_TEXTURE && !UnityVersion.HasPro())
            {
                Debug.LogError("RENDER_TO_TEXTURE requires Unity Pro or Unity 5.0 and later.");
                yield break;
            }

                        #if (!UNITY_EDITOR && !UNITY_STANDALONE_WIN)
            if (captureMode == CaptureMode.GAMEVIEW_RESIZING)
            {
                Debug.LogError("GAMEVIEW_RESIZING capture mode is only available for Editor and Windows Standalone.");
                yield break;
            }
                        #endif

            // Init
            m_IsRunning = true;

            // Stop the time so all screenshots are exactly the same
            if (Application.isPlaying && stopTime)
            {
                StopTime();
            }


            // Apply settings: enable and disable the cameras and canvas
            ApplySettings(cameras, overlays, captureMode, renderUI);

            // Save the screen config to be restored after the capture process
            if (captureMode == CaptureMode.GAMEVIEW_RESIZING)
            {
                GameViewController.SaveCurrentGameViewSize();

                                #if !UNITY_EDITOR && UNITY_STANDALONE_WIN
                yield return(null);

                yield return(new WaitForEndOfFrame());
                                #endif
            }

            // Capture all resolutions
            foreach (ScreenshotResolution resolution in resolutions)
            {
                // Play the shot sound
                if (playSound)
                {
                    PlaySound();
                }

                // Update the texture
                yield return(StartCoroutine(CaptureResolutionTextureCoroutine(resolution, captureMode, antiAliasing, colorFormat, recomputeAlphaMask)));

                // Export to file
                if (export)
                {
                    if (TextureExporter.ExportToFile(resolution.m_Texture, resolution.m_FileName, imageFormat, JPGQuality))
                    {
                        Debug.Log("Screenshot created : " + resolution.m_FileName);
                        onResolutionExportSuccessDelegate(resolution);
                    }
                    else
                    {
                        Debug.LogError("Failed to create : " + resolution.m_FileName);
                        onResolutionExportFailureDelegate(resolution);
                    }
                }
            }

            // Restore screen config
            if (restore && captureMode == CaptureMode.GAMEVIEW_RESIZING)
            {
                GameViewController.RestoreGameViewSize();
            }

                        #if (UNITY_EDITOR && !UNITY_5_4_OR_NEWER)
            // Call restore layout for old unity versions
            if (restore && captureMode == CaptureMode.GAMEVIEW_RESIZING)
            {
                m_NeedRestoreLayout = true;
            }
                        #endif

                        #if UNITY_EDITOR
            // Dirty hack, try to force an editor Update() to get the gameview back to normal
            if (captureMode == CaptureMode.FIXED_GAMEVIEW || captureMode == CaptureMode.GAMEVIEW_RESIZING)
            {
                if (!Application.isPlaying)
                {
                    GameViewUtils.GetGameView().Repaint();
                }
            }
                        #endif

            // Restore everything
            if (Application.isPlaying && stopTime)
            {
                RestoreTime();
            }
            if (Application.isEditor || restore)
            {
                RestoreSettings();
            }

            // End
            m_IsRunning = false;
        }