private void WarnCanvases() { Canvas[] canvases = GameObject.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 != Camera.main) { error = true; dOut.Add("Canvas " + c.gameObject.name + " does not have the main camera attached and cannot render to texture."); } } if (error) { foreach (string s in dOut) { SSHDebug.LogWarning(s); } } }
IEnumerator TakeScreenShots() { #if UNITY_EDITOR if (!Directory.Exists(savePath)) { string newPath = GameViewUtils.SelectFileFolder(System.IO.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 ShotSize initialRes = new ShotSize(Screen.width, Screen.height); #pragma warning restore 0219 if (isTakingShots) { yield break; } isTakingShots = true; string fileName = ""; #if UNITY_EDITOR int currentIndex = GameViewUtils.GetCurrentSizeIndex(); //Maximize game view seems to cause crashes //GameViewUtils.MaximizeGameView(true); #endif 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 (useRenderTexture) { RenderTexture rt = new RenderTexture(shot.width, shot.height, Depth); Camera.main.targetTexture = rt; tex = new Texture2D(shot.width, shot.height, TextureFormat.RGB24, false); Camera.main.Render(); RenderTexture.active = rt; tex.ReadPixels(new Rect(0, 0, shot.width, shot.height), 0, 0); Camera.main.targetTexture = null; RenderTexture.active = null; Destroy(rt); } else { tex = new Texture2D(Screen.width, Screen.height, textureFormat, false); Vector2 camUpperLeft = Camera.main.pixelRect.min; //lower left camUpperLeft.y = Camera.main.pixelRect.min.y; float offsetX = 0f; //0.5f * Screen.width; float offsetY = 0f; //0.5f * Screen.height; #if UNITY_EDITOR //Vector2 gameViewSize = GameViewUtils.GetMainGameViewSize(); //offsetX = 338f; //offsetY = gameViewSize.y; #endif // Was in while there was an issue in Unity when taking screenshots from editor. //Debug.LogFormat("screen: ({0},{1}) -- shot: ({2},{3}) -- offset: ({4},{5})", Screen.width, Screen.height, shot.width, shot.height, offsetX, offsetY); tex.ReadPixels(new Rect(offsetX, offsetY, Screen.width, Screen.height), 0, 0); tex.Apply(); TextureScale.Bilinear(tex, shot.width, shot.height); } if (tex != null) { byte[] screenshot = tex.EncodeToPNG(); var file = File.Open(Path.Combine(savePath, fileName), FileMode.Create); var binaryWriter = new BinaryWriter(file); binaryWriter.Write(screenshot); file.Close(); Destroy(tex); } else { SSHDebug.LogError("Something went wrong! Texture is null"); } } #if UNITY_EDITOR //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 SSHDebug.Log("Screenshots saved to " + savePath); Application.OpenURL(savePath); isTakingShots = false; Time.timeScale = timeScaleStart; }