private void WarnCanvases() { Canvas[] canvases = FindObjectsOfType <Canvas>(); List <string> dOut = new List <string>(); dOut.Add("Canvases need to use ScreenSpaceCamera or Worldspace to properly render " + "to texture and must have the main camera attached."); bool error = false; foreach (Canvas c in canvases) { if (c.renderMode == RenderMode.ScreenSpaceOverlay) { error = true; dOut.Add("Canvas " + c.gameObject.name + " is in Screen Space Overlay mode and will not render to texture."); } if (c.worldCamera == null) { error = true; dOut.Add("Canvas " + c.gameObject.name + " does not have a camera attached and cannot render to texture."); } } if (error) { foreach (string s in dOut) { SSHDebug.LogWarning(s); } } }
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"); } } } }