-
-
Save ByteDecoder/299b323fe65ac3d1f4953da3b0682ef4 to your computer and use it in GitHub Desktop.
Unity make a screenshot from editor
This file contains 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; | |
public class ScreenCaptureEditor : EditorWindow | |
{ | |
private static string directory = "Screenshots/Capture/"; | |
private static string latestScreenshotPath = ""; | |
private bool initDone = false; | |
private GUIStyle BigText; | |
void InitStyles() | |
{ | |
initDone = true; | |
BigText = new GUIStyle(GUI.skin.label) | |
{ | |
fontSize = 20, | |
fontStyle = FontStyle.Bold | |
}; | |
} | |
private void OnGUI() | |
{ | |
if (!initDone) | |
{ | |
InitStyles(); | |
} | |
GUILayout.Label("Screen Capture", BigText); | |
if (GUILayout.Button("Take a screenshot")) | |
{ | |
TakeScreenshot(); | |
} | |
GUILayout.Label("Resolution: " + GetResolution()); | |
if (GUILayout.Button("Reveal in Explorer")) | |
{ | |
ShowFolder(); | |
} | |
GUILayout.Label("Directory: " + directory); | |
} | |
[MenuItem("Tools/Screenshots/Open Window")] | |
public static void ShowWindow() | |
{ | |
EditorWindow.GetWindow(typeof(ScreenCaptureEditor)); | |
} | |
[MenuItem("Tools/Screenshots/Reveal in Explorer")] | |
private static void ShowFolder() | |
{ | |
if (File.Exists(latestScreenshotPath)) | |
{ | |
EditorUtility.RevealInFinder(latestScreenshotPath); | |
return; | |
} | |
Directory.CreateDirectory(directory); | |
EditorUtility.RevealInFinder(directory); | |
} | |
[MenuItem("Tools/Screenshots/Take a Screenshot")] | |
private static void TakeScreenshot() | |
{ | |
Directory.CreateDirectory(directory); | |
var currentTime = System.DateTime.Now; | |
var filename = currentTime.ToString().Replace('/', '-').Replace(':', '_') + ".png"; | |
var path = directory + filename; | |
ScreenCapture.CaptureScreenshot(path); | |
latestScreenshotPath = path; | |
Debug.Log($"Screenshot saved: <b>{path}</b> with resolution <b>{GetResolution()}</b>"); | |
} | |
private static string GetResolution() | |
{ | |
Vector2 size = UnityEditor.Handles.GetMainGameViewSize(); | |
Vector2Int sizeInt = new Vector2Int((int)size.x, (int)size.y); | |
return $"{sizeInt.x.ToString()}x{sizeInt.y.ToString()}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment