Created
February 13, 2018 09:48
-
-
Save tntmeijs/2226c2ccb8afaa3ed4d25c290f78e005 to your computer and use it in GitHub Desktop.
An Unity editor extension which enables you to take screenshots of the scene view window.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
/* | |
* This creative work (which includes code) is under exclusive copyright by default. | |
* Unless specified otherwise by Tahar Meijs, nobody else may use, copy, distribute, | |
* or modify this work without being at risk of take-downs, shake-downs, or litigation. | |
* | |
* Testing code does not mean you will are allowed to keep on using this codebase for | |
* your own personal / commercial gain. However, it is always encouraged to use this | |
* code for educational purposes | |
*/ | |
public class Screenshot : EditorWindow | |
{ | |
private string m_filePath = ""; | |
private Camera m_sceneCamera = null; | |
[MenuItem("Window/Tahars Tools/Screenshot")] | |
public static void ShowWindow() | |
{ | |
GetWindow(typeof(Screenshot)); | |
} | |
private void OnGUI() | |
{ | |
GUILayout.Label("Save location:"); | |
EditorGUILayout.BeginHorizontal(); | |
m_filePath = GUILayout.TextField(m_filePath); | |
if (GUILayout.Button("Browse...")) | |
{ | |
SetSaveLocation(); | |
} | |
if (GUILayout.Button("Clear Path")) | |
{ | |
int result = EditorUtility.DisplayDialogComplex("Clear Path", "Are you sure you want to clear the screenshot path?", "Yes.", "No.", "Cancel"); | |
if (0 == result) | |
m_filePath = ""; | |
} | |
EditorGUILayout.EndHorizontal(); | |
if (GUILayout.Button("Capture Screenshot")) | |
{ | |
if (ValidSaveLocation()) | |
{ | |
CaptureEditorScreenshot(); | |
} | |
else | |
{ | |
EditorUtility.DisplayDialog("Invalid Location", "Please select a location to save your screenshots to first.", "Browse..."); | |
SetSaveLocation(); | |
} | |
} | |
} | |
private void CaptureEditorScreenshot() | |
{ | |
if (null == m_sceneCamera) | |
{ | |
FindSceneCamera(); | |
} | |
// Additional safety check in case the scene camera could not be found | |
if (null != m_sceneCamera) | |
{ | |
// Save the old render texture for future use | |
RenderTexture oldRenderTexture = RenderTexture.active; | |
// Swap the render textyre | |
RenderTexture.active = m_sceneCamera.targetTexture; | |
// Save the render texture data into a regular Texture2D object | |
Texture2D screenshotPreview = new Texture2D(m_sceneCamera.pixelWidth, m_sceneCamera.pixelHeight); | |
screenshotPreview.ReadPixels(new Rect(0.0f, 0.0f, m_sceneCamera.pixelWidth, m_sceneCamera.pixelHeight), 0, 0); | |
screenshotPreview.Apply(); | |
// Restore the render texture | |
RenderTexture.active = oldRenderTexture; | |
// Write the file to the disk | |
File.WriteAllBytes(m_filePath + "/" + Application.productName + System.DateTime.Now.ToString("yyyy-mm-dd-hh-mm-ss") + ".png", screenshotPreview.EncodeToPNG()); | |
} | |
} | |
private bool ValidSaveLocation() | |
{ | |
if (0 != m_filePath.Length) | |
return true; | |
else | |
return false; | |
} | |
private void SetSaveLocation() | |
{ | |
string path = EditorUtility.OpenFolderPanel("Select save location", "", "Screenshots"); | |
if (0 != path.Length) | |
m_filePath = path; | |
} | |
private void FindSceneCamera() | |
{ | |
Camera[] cams = SceneView.GetAllSceneCameras(); | |
foreach (Camera cam in cams) | |
{ | |
if ("SceneCamera" == cam.name) | |
{ | |
// Found the scene camera | |
m_sceneCamera = cam; | |
return; | |
} | |
} | |
EditorUtility.DisplayDialog("Fatal error", "Could not find the scene camera, please report this to the developer!", "Dismiss"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment