Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using Object = UnityEngine.Object;
- public class ScreenShot : EditorWindow {
- #region Window
- private bool panorama = false;
- private int height = 1024;
- private int width = 1024;
- private int angle = 180;
- private Space space = Space.World;
- private Camera cam;
- private void OnGUI() {
- EditorGUILayout.LabelField("Custom screenshot", EditorStyles.boldLabel);
- EditorGUIUtility.labelWidth = 100f;
- EditorGUILayout.Space();
- cam = EditorGUILayout.ObjectField("Camera", cam, typeof(Camera), true) as Camera;
- EditorGUILayout.Space();
- if(!cam)
- cam = Camera.main;
- panorama = EditorGUILayout.Toggle("Panorama", panorama);
- height = EditorGUILayout.IntSlider("Height", height, 64, 8192);
- if(!panorama)
- width = EditorGUILayout.IntSlider("Width", width, 64, 8192);
- else {
- angle = EditorGUILayout.IntSlider("Angle", angle, 1, 360);
- space = (Space)EditorGUILayout.EnumPopup("Rotation space", space);
- }
- GUI.enabled = cam;
- GUILayout.FlexibleSpace();
- if(GUILayout.Button("Shot!!!")) {
- var texture = panorama ? GetCameraRenderPanorama(cam, height, angle, space) : GetCameraRender(cam, width, height);
- SaveTexture(texture);
- DestroyImmediate(texture);
- }
- EditorGUILayout.Space();
- GUI.enabled = true;
- }
- #endregion
- #region MenuItens
- [MenuItem("Screenshot/High resolutions may crash unity", false, -200)]
- private static void Warn() { }
- [MenuItem("Screenshot/High resolutions may crash unity", true, -100)]
- private static bool WarnBool() { return false; }
- [MenuItem("Screenshot/Custom", false, -100)]
- private static void Custom() {
- var window = CreateInstance<ScreenShot>();
- #if UNITY_5_0
- window.title = "Custom screenshot";
- #else
- window.titleContent = new GUIContent("Custom screenshot");
- #endif
- window.maxSize = window.minSize = new Vector2(245f, 160f);
- window.ShowUtility();
- }
- [MenuItem("Screenshot/720p", false, 0)]
- private static void Screenshot720p() {
- Screenshot(1024, 768);
- }
- [MenuItem("Screenshot/1080p", false, 0)]
- private static void Screenshot1080p() {
- Screenshot(1920, 1080);
- }
- [MenuItem("Screenshot/2160p (4k)", false, 0)]
- private static void Screenshot4k() {
- Screenshot(3840, 2160);
- }
- [MenuItem("Screenshot/Current resolution", false, 0)]
- private static void ScreenshotCurrent() {
- Screenshot(Camera.main.pixelWidth, Camera.main.pixelHeight);
- }
- [MenuItem("Screenshot/1024 x 1024", false, 200)]
- private static void Screenshot1024x1024() {
- Screenshot(1024, 1024);
- }
- [MenuItem("Screenshot/2048 x 1024", false, 200)]
- private static void Screenshot2048x1024() {
- Screenshot(2048, 1024);
- }
- [MenuItem("Screenshot/2048 x 2048", false, 200)]
- private static void Screenshot2048x2048() {
- Screenshot(2048, 2048);
- }
- [MenuItem("Screenshot/4096 x 1024", false, 200)]
- private static void Screenshot4096x1024() {
- Screenshot(4096, 1024);
- }
- [MenuItem("Screenshot/4096 x 2048", false, 200)]
- private static void Screenshot4096x2048() {
- Screenshot(4096, 2048);
- }
- [MenuItem("Screenshot/4096 x 4096", false, 200)]
- private static void Screenshot4096x4096() {
- Screenshot(4096, 4096);
- }
- [MenuItem("Screenshot/8196 x 1024", false, 200)]
- private static void Screenshot8196x1024() {
- Screenshot(8196, 1024);
- }
- [MenuItem("Screenshot/8196 x 2048", false, 200)]
- private static void Screenshot8196x2048() {
- Screenshot(8196, 2048);
- }
- [MenuItem("Screenshot/8196 x 4096", false, 200)]
- private static void Screenshot8196x4096() {
- Screenshot(8196, 4096);
- }
- [MenuItem("Screenshot/8196 x 8196", false, 200)]
- private static void Screenshot8196x8196() {
- Screenshot(8196, 8196);
- }
- [MenuItem("Screenshot/Panorama 720p 360º", false, 400)]
- private static void ScreenshotPanorama720p360() {
- SaveTexture(GetCameraRenderPanorama(Camera.main, 720, 360));
- }
- [MenuItem("Screenshot/Panorama 720p 180º", false, 400)]
- private static void ScreenshotPanorama720p180() {
- SaveTexture(GetCameraRenderPanorama(Camera.main, 720, 180));
- }
- [MenuItem("Screenshot/Panorama 1080p 360º", false, 400)]
- private static void ScreenshotPanorama1080p360() {
- SaveTexture(GetCameraRenderPanorama(Camera.main, 1080, 360));
- }
- [MenuItem("Screenshot/Panorama 1080p 180º", false, 400)]
- private static void ScreenshotPanorama1080p180() {
- SaveTexture(GetCameraRenderPanorama(Camera.main, 1080, 180));
- }
- [MenuItem("Screenshot/Panorama 2160p (4k) 360º", false, 400)]
- private static void ScreenshotPanorama4k360() {
- SaveTexture(GetCameraRenderPanorama(Camera.main, 2160, 360));
- }
- [MenuItem("Screenshot/Panorama 2160p (4k) 180º", false, 400)]
- private static void ScreenshotPanorama4k180() {
- SaveTexture(GetCameraRenderPanorama(Camera.main, 2160, 180));
- }
- #endregion
- #region Functions
- private static void Screenshot(int width, int height) {
- var texture = GetCameraRender(Camera.main, width, height);
- SaveTexture(texture);
- DestroyImmediate(texture);
- }
- private static void Panorama(int height, int angle) {
- var texture = GetCameraRenderPanorama(Camera.main, height, angle);
- SaveTexture(texture);
- DestroyImmediate(texture);
- }
- private static bool SaveTexture(Texture2D texture) {
- var path = EditorUtility.SaveFilePanel("Save screenshot", "", "screenshot", "png");
- if(path.Length == 0)
- return false;
- return SaveTexture(texture, path);
- }
- private static bool SaveTexture(Texture2D texture, string path) {
- try {
- var bytes = texture.EncodeToPNG();
- File.WriteAllBytes(path, bytes);
- Debug.LogFormat("Saved screenshot, size: {0}", EditorUtility.FormatBytes(bytes.LongLength));
- return true;
- }
- catch {
- return false;
- }
- }
- private static Texture2D GetCameraRender(Camera cam, int width, int height) {
- var texture = new Texture2D(width, height, TextureFormat.RGB24, false);
- var render = RenderTexture.GetTemporary(width, height, 24);
- RenderTexture.active = cam.targetTexture = render;
- cam.Render();
- texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
- RenderTexture.active = cam.targetTexture = null;
- RenderTexture.ReleaseTemporary(render);
- Object.DestroyImmediate(render);
- return texture;
- }
- private static Texture2D GetCameraRenderPanorama(Camera cam, int height, int angle, Space rotation = Space.World) {
- var width = height * angle / (int)cam.fieldOfView;
- var texture = new Texture2D(width, height, TextureFormat.RGB24, false);
- var render = RenderTexture.GetTemporary(1, height, 24);
- var euler = cam.transform.eulerAngles;
- RenderTexture.active = cam.targetTexture = render;
- cam.transform.Rotate(Vector3.up * angle / -2, rotation);
- for(int i = 0; i < width; i++) {
- EditorUtility.DisplayProgressBar("Creating panorama", "Rendering...", (float)i / width);
- cam.transform.Rotate(Vector3.up * angle / width, rotation);
- cam.Render();
- texture.ReadPixels(new Rect(0, 0, 1, height), i, 0);
- }
- cam.transform.eulerAngles = euler;
- EditorUtility.ClearProgressBar();
- RenderTexture.active = cam.targetTexture = null;
- RenderTexture.ReleaseTemporary(render);
- Object.DestroyImmediate(render);
- return texture;
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement