コード例 #1
0
        public static void SaveTextureAsFile(Texture2D texture, string folder, string prefix, string suffix, ScreenshotConfig screenshotConfig)
        {
            byte[] bytes;
            string extension;

            switch (screenshotConfig.Type)
            {
                case ScreenshotConfig.Format.PNG:
                    bytes = texture.EncodeToPNG();
                    extension = ".png";
                    break;
                case ScreenshotConfig.Format.JPG:
                    bytes = texture.EncodeToJPG();
                    extension = ".jpg";
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            var fileName = prefix + screenshotConfig.Name + "." + screenshotConfig.Width + "x" + screenshotConfig.Height + suffix;
            var imageFilePath = folder + "/" + MakeValidFileName(fileName + extension);

            // ReSharper disable once PossibleNullReferenceException
            (new FileInfo(imageFilePath)).Directory.Create();
            File.WriteAllBytes(imageFilePath, bytes);

            Debug.Log("Image saved to: " + imageFilePath);
        }
コード例 #2
0
        ScreenshotResolution GetFirstPreviewDevice()
        {
            try
            {
                // We use reflection to access the Editor class that are not accessible from this assembly
                var settingsType = System.AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).FirstOrDefault(t => t.FullName.Equals("AlmostEngine.Preview.ResolutionSettingsWindow"));
                var methodInfo   = settingsType.GetMethod("GetConfig");
                var configAsset  = methodInfo.Invoke(null, null);
                if (configAsset == null)
                {
                    return(null);
                }
                var configAssetType = System.AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).FirstOrDefault(t => t.FullName.Equals("AlmostEngine.Preview.PreviewConfigAsset"));
                var configField     = configAssetType.GetField("m_Config");

                // Get the config asset from the preview settings
                ScreenshotConfig config = (ScreenshotConfig)configField.GetValue(configAsset);

                // Get the first active device
                return(config.GetFirstActiveResolution());
            }
            catch
            {
                return(null);
            }
        }
コード例 #3
0
        public static void TakeScreenshot(Camera camera, string folderName, string prefix, string suffix, ScreenshotConfig screenshotConfig)
        {
            var scrTexture = new Texture2D(screenshotConfig.Width, screenshotConfig.Height, TextureFormat.RGB24, false);
            var scrRenderTexture = new RenderTexture(scrTexture.width, scrTexture.height, 24);
            var camRenderTexture = camera.targetTexture;

            camera.targetTexture = scrRenderTexture;
            camera.Render();
            camera.targetTexture = camRenderTexture;

            RenderTexture.active = scrRenderTexture;
            scrTexture.ReadPixels(new Rect(0, 0, scrTexture.width, scrTexture.height), 0, 0);
            scrTexture.Apply();

            SaveTextureAsFile(scrTexture, folderName, prefix, suffix, screenshotConfig);
        }
コード例 #4
0
        public static void SaveScreenShotTextureAsFile(string folder, string prefix, string suffix, ScreenshotConfig screenshotConfig)
        {
            string extension;

            switch (screenshotConfig.Type)
            {
            case ScreenshotConfig.Format.PNG:
                extension = ".png";
                break;

            case ScreenshotConfig.Format.JPG:
                extension = ".jpg";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var fileName      = prefix + screenshotConfig.Name + "." + screenshotConfig.Width + "x" + screenshotConfig.Height + suffix;
            var imageFilePath = folder + "/" + MakeValidFileName(fileName + extension);

            UnityEngine.ScreenCapture.CaptureScreenshot(imageFilePath);
        }
コード例 #5
0
        public static void SaveTextureAsFile(Texture2D texture, string folder, string prefix, string suffix, ScreenshotConfig screenshotConfig)
        {
            byte[] bytes;
            string extension;

            switch (screenshotConfig.Type)
            {
            case ScreenshotConfig.Format.PNG:
                bytes     = texture.EncodeToPNG();
                extension = ".png";
                break;

            case ScreenshotConfig.Format.JPG:
                bytes     = texture.EncodeToJPG();
                extension = ".jpg";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var fileName      = prefix + screenshotConfig.Name + "." + screenshotConfig.Width + "x" + screenshotConfig.Height + suffix;
            var imageFilePath = folder + "/" + MakeValidFileName(fileName + extension);

            // ReSharper disable once PossibleNullReferenceException
            (new FileInfo(imageFilePath)).Directory.Create();
            File.WriteAllBytes(imageFilePath, bytes);

            Debug.Log("Image saved to: " + imageFilePath);
        }
コード例 #6
0
 public static void TakeScreenshot(string folderName, string prefix, string suffix, ScreenshotConfig screenshotConfig)
 {
     SaveScreenShotTextureAsFile(folderName, prefix, suffix, screenshotConfig);
 }
コード例 #7
0
        public static void TakeScreenshot(ScreenShooterSettings settings, ScreenshotConfig config)
        {
            var suffix = settings.AppendTimestamp ? "." + DateTime.Now.ToString("yyyyMMddHHmmssfff") : "";

            TakeScreenshot(settings.SaveFolder, settings.Tag, suffix, config);
        }
コード例 #8
0
        public static void TakeScreenshot(Camera camera, string folderName, string prefix, string suffix, ScreenshotConfig screenshotConfig)
        {
            var scrTexture       = new Texture2D(screenshotConfig.Width, screenshotConfig.Height, TextureFormat.RGB24, false);
            var scrRenderTexture = new RenderTexture(scrTexture.width, scrTexture.height, 24);
            var camRenderTexture = camera.targetTexture;

            camera.targetTexture = scrRenderTexture;
            camera.Render();
            camera.targetTexture = camRenderTexture;

            RenderTexture.active = scrRenderTexture;
            scrTexture.ReadPixels(new Rect(0, 0, scrTexture.width, scrTexture.height), 0, 0);
            scrTexture.Apply();

            SaveTextureAsFile(scrTexture, folderName, prefix, suffix, screenshotConfig);
        }
コード例 #9
0
 public static void TakeScreenshot(ScreenShooterSettings settings, ScreenshotConfig config)
 {
     var suffix = settings.AppendTimestamp ? "." + DateTime.Now.ToString("yyyyMMddHHmmssfff") : "";
     TakeScreenshot(settings.Camera, settings.SaveFolder, settings.Tag, suffix, config);
 }
コード例 #10
0
 public static void TakeScreenshot(ScreenShooterSettings settings, ScreenshotConfig config)
 {
     TakeScreenshot(settings.Camera, settings.SaveFolder, settings.Tag, config);
 }
コード例 #11
0
        public static void SaveTextureAsFile(Texture2D texture, string folder, string prefix, string suffix, ScreenshotConfig screenshotConfig)
        {
            byte[] bytes;

            switch (screenshotConfig.Type)
            {
            case ScreenshotConfig.Format.PNG:
                bytes = texture.EncodeToPNG();
                break;

            case ScreenshotConfig.Format.JPG:
                bytes = texture.EncodeToJPG();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var imageFilePath = GetScreenshotPath(folder, prefix, suffix, screenshotConfig);

            // ReSharper disable once PossibleNullReferenceException
            File.WriteAllBytes(imageFilePath, bytes);

            Debug.Log("Image saved to: " + imageFilePath);
        }
コード例 #12
0
        public static void TakeScreenshot(IList <Camera> cameras, string folderName, string prefix, string suffix, ScreenshotConfig screenshotConfig)
        {
            Texture2D scrTexture = null;

            scrTexture = new Texture2D(screenshotConfig.Width, screenshotConfig.Height, TextureFormat.RGB24, false);

            if (cameras != null && cameras.Count > 0)
            {
                //scrTexture = new Texture2D(screenshotConfig.Width, screenshotConfig.Height, TextureFormat.RGB24, false);
                var scrRenderTexture = new RenderTexture(scrTexture.width, scrTexture.height, 24);

                foreach (var camera in cameras)
                {
                    var camRenderTexture = camera.targetTexture;
                    camera.targetTexture = scrRenderTexture;
                    camera.Render();
                    camera.targetTexture = camRenderTexture;
                }

                RenderTexture.active = scrRenderTexture;
                scrTexture.ReadPixels(new Rect(0, 0, scrTexture.width, scrTexture.height), 0, 0);
                scrTexture.Apply();

                SaveTextureAsFile(scrTexture, folderName, prefix, suffix, screenshotConfig);
            }
            else
            {
                var imageFilePath = GetScreenshotPath(folderName, prefix, suffix, screenshotConfig);

                //float scale = Mathf.Max(1, screenshotConfig.Width / Screen.width, screenshotConfig.Height / Screen.height);
                ScreenCapture.CaptureScreenshot(imageFilePath);

                Debug.Log("Image saved to: " + imageFilePath);
            }
        }
コード例 #13
0
        private static string GetScreenshotPath(string folder, string prefix, string suffix, ScreenshotConfig screenshotConfig)
        {
            string extension;

            switch (screenshotConfig.Type)
            {
            case ScreenshotConfig.Format.PNG:
                extension = ".png";
                break;

            case ScreenshotConfig.Format.JPG:
                extension = ".jpg";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var fileName      = prefix + screenshotConfig.Name + "." + screenshotConfig.Width + "x" + screenshotConfig.Height + suffix;
            var imageFilePath = folder + "/" + MakeValidFileName(fileName + extension);

            // ReSharper disable once PossibleNullReferenceException
            (new FileInfo(imageFilePath)).Directory.Create();

            return(imageFilePath);
        }