Advertisement
Muk99

Screenshot

Feb 16th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.04 KB | None | 0 0
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using Object = UnityEngine.Object;
  5.  
  6. public class ScreenShot : EditorWindow {
  7.  
  8.     #region Window
  9.     private bool panorama = false;
  10.     private int height = 1024;
  11.     private int width = 1024;
  12.     private int angle = 180;
  13.     private Space space = Space.World;
  14.     private Camera cam;
  15.  
  16.     private void OnGUI() {
  17.         EditorGUILayout.LabelField("Custom screenshot", EditorStyles.boldLabel);
  18.         EditorGUIUtility.labelWidth = 100f;
  19.         EditorGUILayout.Space();
  20.  
  21.         cam = EditorGUILayout.ObjectField("Camera", cam, typeof(Camera), true) as Camera;
  22.         EditorGUILayout.Space();
  23.  
  24.         if(!cam)
  25.             cam = Camera.main;
  26.  
  27.         panorama = EditorGUILayout.Toggle("Panorama", panorama);
  28.         height = EditorGUILayout.IntSlider("Height", height, 64, 8192);
  29.  
  30.         if(!panorama)
  31.             width = EditorGUILayout.IntSlider("Width", width, 64, 8192);
  32.         else {
  33.             angle = EditorGUILayout.IntSlider("Angle", angle, 1, 360);
  34.             space = (Space)EditorGUILayout.EnumPopup("Rotation space", space);
  35.         }
  36.  
  37.         GUI.enabled = cam;
  38.         GUILayout.FlexibleSpace();
  39.         if(GUILayout.Button("Shot!!!")) {
  40.             var texture = panorama ? GetCameraRenderPanorama(cam, height, angle, space) : GetCameraRender(cam, width, height);
  41.             SaveTexture(texture);
  42.             DestroyImmediate(texture);
  43.         }
  44.         EditorGUILayout.Space();
  45.         GUI.enabled = true;
  46.     }
  47.     #endregion
  48.  
  49.     #region MenuItens
  50.     [MenuItem("Screenshot/High resolutions may crash unity", false, -200)]
  51.     private static void Warn() { }
  52.  
  53.     [MenuItem("Screenshot/High resolutions may crash unity", true, -100)]
  54.     private static bool WarnBool() { return false; }
  55.  
  56.     [MenuItem("Screenshot/Custom", false, -100)]
  57.     private static void Custom() {
  58.         var window = CreateInstance<ScreenShot>();
  59. #if UNITY_5_0
  60.         window.title = "Custom screenshot";
  61. #else
  62.         window.titleContent = new GUIContent("Custom screenshot");
  63. #endif
  64.         window.maxSize = window.minSize = new Vector2(245f, 160f);
  65.         window.ShowUtility();
  66.     }
  67.  
  68.     [MenuItem("Screenshot/720p", false, 0)]
  69.     private static void Screenshot720p() {
  70.         Screenshot(1024, 768);
  71.     }
  72.  
  73.     [MenuItem("Screenshot/1080p", false, 0)]
  74.     private static void Screenshot1080p() {
  75.         Screenshot(1920, 1080);
  76.     }
  77.  
  78.     [MenuItem("Screenshot/2160p (4k)", false, 0)]
  79.     private static void Screenshot4k() {
  80.         Screenshot(3840, 2160);
  81.     }
  82.  
  83.     [MenuItem("Screenshot/Current resolution", false, 0)]
  84.     private static void ScreenshotCurrent() {
  85.         Screenshot(Camera.main.pixelWidth, Camera.main.pixelHeight);
  86.     }
  87.  
  88.     [MenuItem("Screenshot/1024 x 1024", false, 200)]
  89.     private static void Screenshot1024x1024() {
  90.         Screenshot(1024, 1024);
  91.     }
  92.  
  93.     [MenuItem("Screenshot/2048 x 1024", false, 200)]
  94.     private static void Screenshot2048x1024() {
  95.         Screenshot(2048, 1024);
  96.     }
  97.  
  98.     [MenuItem("Screenshot/2048 x 2048", false, 200)]
  99.     private static void Screenshot2048x2048() {
  100.         Screenshot(2048, 2048);
  101.     }
  102.  
  103.     [MenuItem("Screenshot/4096 x 1024", false, 200)]
  104.     private static void Screenshot4096x1024() {
  105.         Screenshot(4096, 1024);
  106.     }
  107.  
  108.     [MenuItem("Screenshot/4096 x 2048", false, 200)]
  109.     private static void Screenshot4096x2048() {
  110.         Screenshot(4096, 2048);
  111.     }
  112.  
  113.     [MenuItem("Screenshot/4096 x 4096", false, 200)]
  114.     private static void Screenshot4096x4096() {
  115.         Screenshot(4096, 4096);
  116.     }
  117.  
  118.     [MenuItem("Screenshot/8196 x 1024", false, 200)]
  119.     private static void Screenshot8196x1024() {
  120.         Screenshot(8196, 1024);
  121.     }
  122.  
  123.     [MenuItem("Screenshot/8196 x 2048", false, 200)]
  124.     private static void Screenshot8196x2048() {
  125.         Screenshot(8196, 2048);
  126.     }
  127.  
  128.     [MenuItem("Screenshot/8196 x 4096", false, 200)]
  129.     private static void Screenshot8196x4096() {
  130.         Screenshot(8196, 4096);
  131.     }
  132.  
  133.     [MenuItem("Screenshot/8196 x 8196", false, 200)]
  134.     private static void Screenshot8196x8196() {
  135.         Screenshot(8196, 8196);
  136.     }
  137.  
  138.     [MenuItem("Screenshot/Panorama 720p 360º", false, 400)]
  139.     private static void ScreenshotPanorama720p360() {
  140.         SaveTexture(GetCameraRenderPanorama(Camera.main, 720, 360));
  141.     }
  142.  
  143.     [MenuItem("Screenshot/Panorama 720p 180º", false, 400)]
  144.     private static void ScreenshotPanorama720p180() {
  145.         SaveTexture(GetCameraRenderPanorama(Camera.main, 720, 180));
  146.     }
  147.  
  148.     [MenuItem("Screenshot/Panorama 1080p 360º", false, 400)]
  149.     private static void ScreenshotPanorama1080p360() {
  150.         SaveTexture(GetCameraRenderPanorama(Camera.main, 1080, 360));
  151.     }
  152.  
  153.     [MenuItem("Screenshot/Panorama 1080p 180º", false, 400)]
  154.     private static void ScreenshotPanorama1080p180() {
  155.         SaveTexture(GetCameraRenderPanorama(Camera.main, 1080, 180));
  156.     }
  157.  
  158.     [MenuItem("Screenshot/Panorama 2160p (4k) 360º", false, 400)]
  159.     private static void ScreenshotPanorama4k360() {
  160.         SaveTexture(GetCameraRenderPanorama(Camera.main, 2160, 360));
  161.     }
  162.  
  163.     [MenuItem("Screenshot/Panorama 2160p (4k) 180º", false, 400)]
  164.     private static void ScreenshotPanorama4k180() {
  165.         SaveTexture(GetCameraRenderPanorama(Camera.main, 2160, 180));
  166.     }
  167.     #endregion
  168.  
  169.     #region Functions
  170.     private static void Screenshot(int width, int height) {
  171.         var texture = GetCameraRender(Camera.main, width, height);
  172.         SaveTexture(texture);
  173.         DestroyImmediate(texture);
  174.     }
  175.  
  176.     private static void Panorama(int height, int angle) {
  177.         var texture = GetCameraRenderPanorama(Camera.main, height, angle);
  178.         SaveTexture(texture);
  179.         DestroyImmediate(texture);
  180.     }
  181.  
  182.     private static bool SaveTexture(Texture2D texture) {
  183.         var path = EditorUtility.SaveFilePanel("Save screenshot", "", "screenshot", "png");
  184.  
  185.         if(path.Length == 0)
  186.             return false;
  187.  
  188.         return SaveTexture(texture, path);
  189.     }
  190.  
  191.     private static bool SaveTexture(Texture2D texture, string path) {
  192.         try {
  193.             var bytes = texture.EncodeToPNG();
  194.             File.WriteAllBytes(path, bytes);
  195.             Debug.LogFormat("Saved screenshot, size: {0}", EditorUtility.FormatBytes(bytes.LongLength));
  196.             return true;
  197.         }
  198.         catch {
  199.             return false;
  200.         }
  201.     }
  202.  
  203.     private static Texture2D GetCameraRender(Camera cam, int width, int height) {
  204.         var texture = new Texture2D(width, height, TextureFormat.RGB24, false);
  205.         var render = RenderTexture.GetTemporary(width, height, 24);
  206.  
  207.         RenderTexture.active = cam.targetTexture = render;
  208.         cam.Render();
  209.         texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  210.  
  211.         RenderTexture.active = cam.targetTexture = null;
  212.         RenderTexture.ReleaseTemporary(render);
  213.         Object.DestroyImmediate(render);
  214.  
  215.         return texture;
  216.     }
  217.  
  218.     private static Texture2D GetCameraRenderPanorama(Camera cam, int height, int angle, Space rotation = Space.World) {
  219.  
  220.         var width = height * angle / (int)cam.fieldOfView;
  221.         var texture = new Texture2D(width, height, TextureFormat.RGB24, false);
  222.         var render = RenderTexture.GetTemporary(1, height, 24);
  223.         var euler = cam.transform.eulerAngles;
  224.  
  225.         RenderTexture.active = cam.targetTexture = render;
  226.  
  227.         cam.transform.Rotate(Vector3.up * angle / -2, rotation);
  228.  
  229.         for(int i = 0; i < width; i++) {
  230.             EditorUtility.DisplayProgressBar("Creating panorama", "Rendering...", (float)i / width);
  231.             cam.transform.Rotate(Vector3.up * angle / width, rotation);
  232.             cam.Render();
  233.             texture.ReadPixels(new Rect(0, 0, 1, height), i, 0);
  234.         }
  235.  
  236.         cam.transform.eulerAngles = euler;
  237.  
  238.         EditorUtility.ClearProgressBar();
  239.         RenderTexture.active = cam.targetTexture = null;
  240.         RenderTexture.ReleaseTemporary(render);
  241.         Object.DestroyImmediate(render);
  242.  
  243.         return texture;
  244.     }
  245.     #endregion
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement