private void RemoveAllCustomSizes() { #if UNITY_EDITOR foreach (ShotSize shot in shotInfo) { GameViewUtils.RemoveCustomSize(shot.width, shot.height); } #endif }
IEnumerator FinishRoutine(int currentIndex, float timeScaleStart) { #if UNITY_EDITOR // Don't maximize game view... causes crash //GameViewUtils.MaximizeGameView(false); GameViewUtils.SetSize(currentIndex); if (OnScreenChanged != null) { yield return(new WaitForEndOfFrame()); OnScreenChanged(); yield return(new WaitForEndOfFrame()); } RemoveAllCustomSizes(); #else Screen.SetResolution(initialRes.width, initialRes.height, false); #endif #if SSH_DIAG Debug.LogFormat("Total shots: {0} Total time: {1:N2}s Avg time: {2:N2}s", shotInfo.Count, (double)stopwatch.ElapsedMilliseconds / 1000f, (double)stopwatch.ElapsedMilliseconds / (1000f * shotInfo.Count)); #endif SSHDebug.Log("Screenshots saved to " + savePath); if (openFolderWhenDone) { Application.OpenURL(savePath); } IsTakingShots = false; yield return(new WaitForEndOfFrame()); Time.timeScale = timeScaleStart; OnComplete.Invoke(); }
IEnumerator TakeScreenShots() { if (IsTakingShots) { yield break; } IsTakingShots = true; #if UNITY_EDITOR if (!Directory.Exists(savePath)) { string newPath = GameViewUtils.SelectFileFolder( Directory.GetCurrentDirectory(), ""); if (!string.IsNullOrEmpty(newPath)) { savePath = newPath; if (PathChange != null) { PathChange(newPath); } } } #endif if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } float timeScaleStart = Time.timeScale; Time.timeScale = 0f; #pragma warning disable 0219 initialRes = new ShotSize(Screen.width, Screen.height); #pragma warning restore 0219 string fileName = ""; #if UNITY_EDITOR int currentIndex = GameViewUtils.GetCurrentSizeIndex(); //Maximize game view seems to cause crashes //GameViewUtils.MaximizeGameView(true); #else int currentIndex = 0; #endif #if SSH_DIAG stopwatch = new Stopwatch(); stopwatch.Start(); #endif shotCounter = shotInfo.Count; foreach (var shot in shotInfo) { fileName = GetScreenShotName(shot); #if UNITY_EDITOR GameViewUtils.SetSize(shot.width, shot.height); if (OnScreenChanged != null) { yield return(new WaitForEndOfFrame()); OnScreenChanged(); yield return(new WaitForEndOfFrame()); } Canvas.ForceUpdateCanvases(); #else float ratio = (1f * shot.width) / (1f * shot.height); SSH_IntVector2 thisRes = new SSH_IntVector2(shot.width, shot.height); if (shot.height > maxRes.height) { thisRes.width = Mathf.FloorToInt(maxRes.height * ratio); thisRes.height = maxRes.height; } Screen.SetResolution(thisRes.width, thisRes.height, false); Canvas.ForceUpdateCanvases(); yield return(new WaitForEndOfFrame()); int attempts = 0; while (Screen.width != thisRes.width && attempts < 10) { Screen.SetResolution(thisRes.width, thisRes.height, false); Canvas.ForceUpdateCanvases(); yield return(new WaitForEndOfFrame()); attempts++; } #endif yield return(new WaitForEndOfFrame()); yield return(new WaitForEndOfFrame()); Texture2D tex = null; if (cameras == null || cameras.Length <= 0) { cameras = FindObjectsOfType <Camera>(); } if (useRenderTexture) { // sort cameras by depth lowest to heighest cameras = cameras.OrderBy(x => x.depth).ToArray(); #if USE_THREADING // Threaded with 2 cameras takes 0.5s per shot (13 shots at ~7s). var colors = SSHUtil.GetCameraTexturesColors( cameras, shot, TextureFormat.ARGB32, Depth); // unbox string shotFileName = fileName; var sshot = shot; SSHUtil.CombineTexturesThread(colors, (result) => { unityThreadQueue.Enqueue(() => { var tempText = new Texture2D( sshot.width, sshot.height, textureFormat, false); tempText.SetPixels(result); tempText.Apply(); SSHUtil.SaveTexture(tempText, savePath, shotFileName); shotCounter--; if (shotCounter == 0) { RunFinishRoutine(currentIndex, timeScaleStart); } }); }); #else // Non-threaded with 2 cameras takes ~1.5sec per shot (13 shots at 19.51s). tex = SSHUtil.CombineCamerasNonThreaded( cameras, shot, TextureFormat.ARGB32, Depth); if (tex == null) { SSHDebug.LogError("Something went wrong! Texture is null"); } else { SSHUtil.SaveTexture(tex, savePath, fileName); shotCounter--; if (shotCounter == 0) { RunFinishRoutine(currentIndex, timeScaleStart); } } #endif } else { // Non-render texture method with bilinear scaling. tex = SSHUtil.GetScreenBilinearScaling(textureFormat, shot); if (tex != null) { SSHUtil.SaveTexture(tex, savePath, fileName); shotCounter--; if (shotCounter == 0) { RunFinishRoutine(currentIndex, timeScaleStart); } } else { SSHDebug.LogError("Something went wrong! Texture is null"); } } } }
public override void OnInspectorGUI() { ScreenshotHelper ssHelper = (ScreenshotHelper)target; serializedObject.Update(); var cameraArrayProp = serializedObject.FindProperty("cameras"); EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField( cameraArrayProp, new GUIContent("Cameras (leave empty to autodetect)"), true); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); } RenderTextureToggleAndWarning(ssHelper); CameraSolidColorTransparencyWarning(ssHelper); MakeSpace(1); EditorGUI.BeginChangeCheck(); MakeSpace(1); ssHelper.orientation = (SSHOrientation)EditorGUILayout.EnumPopup( "Orientation", ssHelper.orientation); if (EditorGUI.EndChangeCheck()) { ssHelper.UpdateDimensions(); } //sizes header MakeSpace(1); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Screen Shot Sizes", EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); ssHelper.fileNamePrefix = EditorGUILayout.TextField("Filename prefix", ssHelper.fileNamePrefix); MakeSpace(1); SetSizesSubs(ssHelper); //add a size EditorGUILayout.Space(); EditorGUI.BeginDisabledGroup(ssHelper.IsTakingShots); if (GUILayout.Button(new GUIContent(" Add a size", AddIcon), ButtonHeight)) { ssHelper.shotInfo.Add(new ShotSize(0, 0)); } //ssHelper.didPressEditorScreenshotButton = false; didStartTaking = false; if (GUILayout.Button(new GUIContent("Take Shots!", ShutterIcon))) { EditorApplication.isPlaying = true; ssHelper.didPressEditorScreenshotButton = true; } //Debug.LogFormat("didPress: {0} isPlay: {1}", ssHelper.didPressEditorScreenshotButton, EditorApplication.isPlaying); if (ssHelper.didPressEditorScreenshotButton && EditorApplication.isPlaying) { didStartTaking = true; ssHelper.didPressEditorScreenshotButton = false; ssHelper.OnComplete += () => { //Debug.Log("OnComplete"); EditorApplication.isPlaying = false; }; //Debug.Log("Call runtake"); ssHelper.GetScreenShots(); } if (didStartTaking && !EditorApplication.isPlaying) { didStartTaking = false; ssHelper.didPressEditorScreenshotButton = false; } MakeSpace(3); ssHelper.openFolderWhenDone = EditorGUILayout.ToggleLeft( "Open folder when done?", ssHelper.openFolderWhenDone); EditorGUILayout.HelpBox( "In-editor Save location: " + ssHelper.savePath, MessageType.None); if (GUILayout.Button( new GUIContent(" Set in-editor save location", FolderIcon), ButtonHeight)) { ssHelper.savePath = GameViewUtils.SelectFileFolder( Directory.GetCurrentDirectory(), ""); PathChange(ssHelper.savePath); } MakeSpace(1); EditorGUILayout.HelpBox( "Build Save location example: " + ssHelper.BuildSaveLocation(), MessageType.None); if (GUILayout.Button( new GUIContent(" Set build save location", FolderIcon), ButtonHeight)) { ScreenshotHelperBuildSaveLocationWindow.ShowWindow(ssHelper); } MakeSpace(2); if (GUILayout.Button(new GUIContent(" Save Presets", SaveIcon), ButtonHeight)) { string newConfig = SavePreset(ssHelper); if (!string.IsNullOrEmpty(newConfig)) { ssHelper.configFile = newConfig; } } LoadPresetsButton(ssHelper); MakeSpace(2); if (GUILayout.Button(new GUIContent(" Load Defaults", OpenIcon), ButtonHeight)) { ssHelper.SetDefaults(); ssHelper.configFile = ""; } EditorGUI.EndDisabledGroup(); MakeSpace(1); }