Beispiel #1
0
        void Awake()
        {
            if (OnComplete == null)
            {
                OnComplete += () => { }
            }
            ;

            if (_instance == null)
            {
                _instance = this;
                DontDestroyOnLoad(this);
            }
            else
            {
                if (this != _instance)
                {
                    Destroy(gameObject);
                }
            }

#if !UNITY_EDITOR
            savePath = BuildSaveLocation();
#endif
            maxRes = new ShotSize(Screen.currentResolution.width, Screen.currentResolution.height);
        }
Beispiel #2
0
        public static List <Color[]> GetCameraTexturesColors(
            Camera[] cameras, ShotSize shot, TextureFormat format, int depth)
        {
            var colors = new List <Color[]>();


            for (int i = 0; i < cameras.Length; i++)
            {
                // Skip cameras who already render to texture.
                if (cameras[i].targetTexture != null)
                {
                    continue;
                }

                RenderTexture rtNew = new RenderTexture(shot.width, shot.height, depth);

                cameras[i].targetTexture = rtNew;
                var tempTex = new Texture2D(shot.width, shot.height, format, false);
                cameras[i].Render();
                RenderTexture.active = rtNew;

                tempTex.ReadPixels(new Rect(0, 0, shot.width, shot.height), 0, 0);
                tempTex.Apply();
                colors.Add(tempTex.GetPixels());

                cameras[i].targetTexture = null;
                RenderTexture.active     = null;
                Object.Destroy(tempTex);
                Object.Destroy(rtNew);
            }

            return(colors);
        }
Beispiel #3
0
        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]);
            }
        }
Beispiel #4
0
        public static Texture2D GetScreenBilinearScaling(
            TextureFormat textureFormat, ShotSize shot)
        {
            var tex = new Texture2D(Screen.width, Screen.height, textureFormat, false);
            // Deprecated - was used to handle issue where Unity Editor would capture the
            // entire Game window resulting in letterboxing or an offset.
            //Vector2 camUpperLeft = mainCamera.pixelRect.min; //lower left
            //camUpperLeft.y = mainCamera.pixelRect.min.y;

            float offsetX = 0f;
            float offsetY = 0f;

            tex.ReadPixels(new Rect(offsetX, offsetY, Screen.width, Screen.height), 0, 0);
            tex.Apply();
            TextureScale.Bilinear(tex, shot.width, shot.height);

            return(tex);
        }
Beispiel #5
0
        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(fileNamePrefix))
            {
                pre = fileNamePrefix + "_";
            }

            if (!string.IsNullOrEmpty(shot.label))
            {
                pre += shot.label + "_";
            }
            return(pre + ext + "_" + num.ToString() + ".png");
        }
Beispiel #6
0
        public static Texture2D CombineCamerasNonThreaded(
            Camera[] cameras, ShotSize shot, TextureFormat format, int depth)
        {
            Texture2D result = null;

            for (int i = 0; i < cameras.Length; i++)
            {
                // Skip cameras who already render to texture.
                if (cameras[i].targetTexture != null)
                {
                    continue;
                }

                RenderTexture rtNew = new RenderTexture(shot.width, shot.height, depth);

                cameras[i].targetTexture = rtNew;
                var tempTex = new Texture2D(shot.width, shot.height, format, false);
                cameras[i].Render();
                RenderTexture.active = rtNew;

                tempTex.ReadPixels(new Rect(0, 0, shot.width, shot.height), 0, 0);
                tempTex.Apply();
                if (i == 0)
                {
                    result = tempTex;
                }
                else
                {
                    result = Combine(result, tempTex);
                }

                cameras[i].targetTexture = null;
                RenderTexture.active     = null;
                Object.Destroy(tempTex);
                Object.Destroy(rtNew);
            }

            return(result);
        }
Beispiel #7
0
        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");
                    }
                }
            }
        }