Exemple #1
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;
        }
        public void DrawCaptureModeGUI()
        {
            EditorGUILayout.PropertyField(m_CaptureMode);


            if (m_Config.m_CaptureMode == ScreenshotTaker.CaptureMode.GAMEVIEW_RESIZING)
            {
                EditorGUILayout.HelpBox("GAMEVIEW_RESIZING is for Editor and Windows Standalone only, can capture the UI, can capture custom resolutions.", MessageType.Info);
            }
            else if (m_Config.m_CaptureMode == ScreenshotTaker.CaptureMode.RENDER_TO_TEXTURE)
            {
                EditorGUILayout.HelpBox("RENDER_TO_TEXTURE is for Editor and all platforms, can not capture the UI, can capture custom resolutions.", MessageType.Info);
            }
            else if (m_Config.m_CaptureMode == ScreenshotTaker.CaptureMode.FIXED_GAMEVIEW)
            {
                EditorGUILayout.HelpBox("FIXED_GAMEVIEW is for Editor and all platforms, can capture the UI, can only capture at the screen resolution.", MessageType.Info);
            }

            if (m_Config.m_CaptureMode == ScreenshotTaker.CaptureMode.RENDER_TO_TEXTURE)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(m_AntiAliasing);
                EditorGUI.indentLevel--;

                if (m_Config.m_MultisamplingAntiAliasing != ScreenshotConfig.AntiAliasing.NONE)
                {
                    bool incompatibility = false;
                    foreach (ScreenshotCamera camera in m_Config.m_Cameras)
                    {
                        if (camera.m_Camera == null)
                        {
                            continue;
                        }
                                                #if UNITY_5_6_OR_NEWER
                        if (camera.m_Camera.allowHDR)
                        {
                                                #else
                        if (camera.m_Camera.hdr)
                        {
                                                        #endif
                            incompatibility = true;
                        }
                    }
                    if (incompatibility)
                    {
                        EditorGUILayout.HelpBox("It is impossible to use MultiSampling Antialiasing when one or more camera is using HDR.", MessageType.Warning);
                    }
                }

                if (!UnityVersion.HasPro())
                {
                    EditorGUILayout.HelpBox("RENDER_TO_TEXTURE requires Unity Pro or Unity 5.0 and later.", MessageType.Error);
                }
            }
        }

        #endregion

        #region CAMERA

        void CreateCameraReorderableList()
        {
            m_CameraReorderableList = new ReorderableList(serializedObject, m_Cameras, true, true, true, true);
            m_CameraReorderableList.drawElementCallback = (Rect position, int index, bool active, bool focused) => {
                SerializedProperty element = m_CameraReorderableList.serializedProperty.GetArrayElementAtIndex(index);
                EditorGUI.PropertyField(position, element);
            };
            m_CameraReorderableList.drawHeaderCallback = (Rect position) => {
                EditorGUI.LabelField(position, "Active             Camera                                                  Settings");
            };
            m_CameraReorderableList.onAddCallback = (ReorderableList list) => {
                m_Config.m_Cameras.Add(new ScreenshotCamera());
                EditorUtility.SetDirty(m_Obj);
            };
            m_CameraReorderableList.elementHeight = 8 * 20;
        }
        void DrawCaptureModeUI()
        {
            EditorGUILayout.PropertyField(m_CaptureMode);


            if (m_ScreenshotManager.m_Config.m_CaptureMode == ScreenshotTaker.CaptureMode.GAMEVIEW_RESIZING)
            {
                EditorGUILayout.HelpBox("GAMEVIEW_RESIZING is for Editor and Windows Standalone only, can capture the UI, with custom resolutions.", MessageType.Info);
            }
            else if (m_ScreenshotManager.m_Config.m_CaptureMode == ScreenshotTaker.CaptureMode.RENDER_TO_TEXTURE)
            {
                EditorGUILayout.HelpBox("RENDER_TO_TEXTURE is for Editor and all platforms, can not capture the UI, with custom resolutions.", MessageType.Info);
            }
            else if (m_ScreenshotManager.m_Config.m_CaptureMode == ScreenshotTaker.CaptureMode.FIXED_GAMEVIEW)
            {
                EditorGUILayout.HelpBox("FIXED_GAMEVIEW is for Editor and all platforms, can capture the UI, only at the screen resolution.", MessageType.Info);
            }

            if (m_ScreenshotManager.m_Config.m_CaptureMode == ScreenshotTaker.CaptureMode.RENDER_TO_TEXTURE)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(m_AntiAliasing);
                EditorGUI.indentLevel--;

                if (m_ScreenshotManager.m_Config.m_MultisamplingAntiAliasing != ScreenshotConfig.AntiAliasing.NONE)
                {
                    bool incompatibility = false;
                    foreach (ScreenshotCamera camera in m_ScreenshotManager.m_Config.m_Cameras)
                    {
                        if (camera.m_Camera == null)
                        {
                            continue;
                        }
                                                                                                #if UNITY_5_6_OR_NEWER
                        if (camera.m_Camera.allowHDR)
                        {
                                                                                                #else
                        if (camera.m_Camera.hdr)
                        {
                                                                                                                #endif
                            incompatibility = true;
                        }
                    }
                    if (incompatibility)
                    {
                        EditorGUILayout.HelpBox("It is impossible to use MultiSampling Antialiasing when one or more camera is using HDR.", MessageType.Warning);
                    }
                }

                if (!UnityVersion.HasPro())
                {
                    EditorGUILayout.HelpBox("RENDER_TO_TEXTURE requires Unity Pro or Unity 5.0 and later.", MessageType.Error);
                }
            }
        }

        void CreateCameraReorderableList()
        {
            m_CameraReorderableList = new ReorderableList(serializedObject, m_Cameras, true, true, true, true);
            m_CameraReorderableList.drawElementCallback = (Rect position, int index, bool active, bool focused) => {
                SerializedProperty element = m_CameraReorderableList.serializedProperty.GetArrayElementAtIndex(index);
                EditorGUI.PropertyField(position, element);
            };
            m_CameraReorderableList.drawHeaderCallback = (Rect position) => {
                EditorGUI.LabelField(position, "Active             Camera                                                  Settings");
            };
            m_CameraReorderableList.onAddCallback = (ReorderableList list) => {
                m_ScreenshotManager.m_Config.m_Cameras.Add(new ScreenshotCamera());
                EditorUtility.SetDirty(m_ScreenshotManager);
            };
            m_CameraReorderableList.elementHeight = 8 * 20;
        }

        void DrawCamerasGUI()
        {
            // Title
            m_ScreenshotManager.m_ShowCameras = EditorGUILayout.Foldout(m_ScreenshotManager.m_ShowCameras, "Cameras".ToUpper());
            if (m_ScreenshotManager.m_ShowCameras == false)
            {
                return;
            }


            EditorGUILayout.PropertyField(m_CameraMode);

            if (m_ScreenshotManager.m_Config.m_CameraMode == ScreenshotConfig.CamerasMode.CUSTOM_CAMERAS)
            {
                EditorGUILayout.Separator();


                // List
                m_CameraReorderableList.DoLayoutList();
            }
            else if (m_ScreenshotManager.m_Config.m_CaptureMode == ScreenshotTaker.CaptureMode.RENDER_TO_TEXTURE)
            {
                EditorGUILayout.HelpBox("RENDER_TO_TEXTURE requires the use the CUSTOM_CAMERAS mode.", MessageType.Warning);
            }
        }

        #endregion

        #region RESOLUTIONS

        void CreateResolutionReorderableList()
        {
            m_ResolutionReorderableList = new ReorderableList(serializedObject, m_Resolutions, true, true, true, true);
            m_ResolutionReorderableList.drawElementCallback = (Rect position, int index, bool active, bool focused) => {
                SerializedProperty element = m_ResolutionReorderableList.serializedProperty.GetArrayElementAtIndex(index);
                EditorGUI.PropertyField(position, element);
            };

            m_ResolutionReorderableList.onChangedCallback = (ReorderableList list) => {
                m_ScreenshotManager.UpdateRatios();
                EditorUtility.SetDirty(m_ScreenshotManager);
            };

            m_ResolutionReorderableList.onSelectCallback = (ReorderableList list) => {
                m_ScreenshotManager.UpdateRatios();
                EditorUtility.SetDirty(m_ScreenshotManager);
            };

            m_ResolutionReorderableList.drawHeaderCallback = (Rect position) => {
                if (typeof(ScreenshotManager).Assembly.GetType("AlmostEngine.Preview.ResolutionGalleryWindow") != null)
                {
                    EditorGUI.LabelField(position, "Active  Width     Height  Scale Ratio   Orientation        PPI/Forced       %             Name                 Category");
                }
                else
                {
                    EditorGUI.LabelField(position, "Active  Width     Height  Scale Ratio   Orientation        Name                 Category");
                }
            };



            m_ResolutionReorderableList.onAddDropdownCallback = (Rect position, ReorderableList list) => {
                var menu = new GenericMenu();

                ConstructResolutionPresetsMenu(menu);

                menu.AddItem(new GUIContent("custom"), false, OnResolutionSelectCallback, new ScreenshotResolution());

                menu.ShowAsContext();
            };
        }

        void ConstructResolutionPresetsMenu(GenericMenu menu)
        {
            foreach (string key in ScreenshotManager.m_Categories.Keys)
            {
                menu.AddItem(new GUIContent(key + "/(add all)"), false, OnResolutionSelectAllCallback, ScreenshotManager.m_Categories [key]);
            }

            foreach (ScreenshotResolution res in ScreenshotManager.m_ResolutionPresets)
            {
                string name = res.m_Category + "/" + res.ToString();
                menu.AddItem(new GUIContent(name), false, OnResolutionSelectCallback, res);
            }
            EditorUtility.SetDirty(m_ScreenshotManager);
        }

        void OnResolutionSelectAllCallback(object target)
        {
            List <ScreenshotResolution> selection = (List <ScreenshotResolution>)target;

            foreach (ScreenshotResolution res in selection)
            {
                m_ScreenshotManager.m_Config.m_Resolutions.Add(new ScreenshotResolution(res));
            }
            m_ScreenshotManager.UpdateRatios();
            EditorUtility.SetDirty(m_ScreenshotManager);
        }

        void OnResolutionSelectCallback(object target)
        {
            ScreenshotResolution selection = (ScreenshotResolution)target;

            m_ScreenshotManager.m_Config.m_Resolutions.Add(new ScreenshotResolution(selection));
            m_ScreenshotManager.UpdateRatios();
            EditorUtility.SetDirty(m_ScreenshotManager);
        }