void Awake() { if (_instance == null) { _instance = this; DontDestroyOnLoad(this); } else { if (this != _instance) { Destroy(this.gameObject); } } /* #if UNITY_EDITOR * if (string.IsNullOrEmpty(savePath)) * savePath = System.IO.Directory.GetCurrentDirectory() + "/screenshothelper"; */ #if !UNITY_EDITOR savePath = BuildSaveLocation(); #endif maxRes = new ShotSize(Screen.currentResolution.width, Screen.currentResolution.height); }
private void SortSizes() { ScreenshotHelper myTarget = (ScreenshotHelper)target; List <ShotSize> shotSizes = myTarget.shotInfo; List <string> fileNames = new List <string>(); for (int i = 0; i < shotSizes.Count; i++) { fileNames.Add(myTarget.GetScreenShotName(shotSizes[i])); } fileNames.Sort(); ShotSize[] tempShotSizes = new ShotSize[shotSizes.Count]; for (int i = 0; i < fileNames.Count; i++) { for (int j = 0; j < shotSizes.Count; j++) { if (myTarget.GetScreenShotName(shotSizes[j]) == fileNames[i]) { tempShotSizes[i] = shotSizes[j]; } } } myTarget.shotInfo = new List <ShotSize>(); for (int i = 0; i < tempShotSizes.Length; i++) { myTarget.shotInfo.Add(tempShotSizes[i]); } }
public string GetScreenShotName(ShotSize shot) { string ext = shot.GetFileNameBase(); //shot.width.ToString() + "x" + shot.height.ToString(); int num = GetFileNum(ext); string pre = ""; if (!string.IsNullOrEmpty(shot.label)) { pre = shot.label + "_"; } return(pre + ext + "_" + num.ToString() + ".png"); }
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; }