-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInstantScreenshot.cs
239 lines (214 loc) · 9.37 KB
/
InstantScreenshot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class InstantScreenshot : EditorWindow
{
private int resWidth = Screen.width * 4; // Default resolution width
private int resHeight = Screen.height * 4; // Default resolution height
private Camera screenshotCamera; // Camera to take the screenshot from
private int scale = 1; // Scale factor for the screenshot
private string savePath = ""; // Path to save the screenshot
private bool isTransparent = false; // Whether the screenshot background is transparent
private bool isTakingScreenshot = false; // Flag to indicate if a screenshot is being taken
private string lastScreenshot = ""; // Path of the last screenshot taken
private bool openFileAfterScreenshot = false; // Flag to indicate if the last screenshot should be opened after taking
[MenuItem("Tools/Saad Khawaja/Instant High-res Screenshot")]
public static void ShowWindow()
{
// Show existing window instance or create a new one
EditorWindow editorWindow = GetWindow<InstantScreenshot>();
editorWindow.autoRepaintOnSceneChange = true;
editorWindow.Show();
editorWindow.titleContent = new GUIContent("Instant Screenshot");
}
[MenuItem("Tools/Saad Khawaja/Take Screenshot %&t")] // Shortcut: Ctrl + Shift + T
public static void TakeScreenshotShortcut()
{
// Trigger screenshot through shortcut
InstantScreenshot window = GetWindow<InstantScreenshot>();
window.TakeHighResShot();
}
private Vector2 scrollPosition; // Scroll position for the scroll view
private void OnGUI()
{
// Add padding to the window
GUILayout.BeginHorizontal();
GUILayout.Space(10);
GUILayout.BeginVertical(GUILayout.ExpandWidth(true));
GUILayout.Space(10);
// Create a scroll view
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.ExpandWidth(true));
// Create a GUIStyle for the label
GUIStyle titleStyle = new GUIStyle(GUI.skin.label)
{
fontSize = 40,
fontStyle = FontStyle.Bold,
alignment = TextAnchor.MiddleCenter
};
// Draw the label at the start
GUILayout.Label("Instant Screenshot", titleStyle);
// Draw different sections of the UI
DrawResolutionSettings();
GUILayout.Space(10);
DrawSavePathSettings();
GUILayout.Space(10);
DrawCameraSettings();
GUILayout.Space(10);
DrawDefaultOptions();
GUILayout.Space(10);
DrawScreenshotOptions();
// End the scroll view and the padding
GUILayout.EndScrollView();
GUILayout.Space(10);
GUILayout.EndVertical();
GUILayout.Space(10);
GUILayout.EndHorizontal();
}
private void DrawResolutionSettings()
{
EditorGUILayout.LabelField("Resolution", EditorStyles.boldLabel);
resWidth = EditorGUILayout.IntField("Width", resWidth);
resHeight = EditorGUILayout.IntField("Height", resHeight);
EditorGUILayout.Space();
scale = EditorGUILayout.IntSlider("Scale", scale, 1, 15);
EditorGUILayout.HelpBox("The default mode of screenshot is crop - so choose a proper width and height. The scale is a factor to multiply or enlarge the renders without losing quality.", MessageType.None);
EditorGUILayout.Space();
}
private void DrawSavePathSettings()
{
GUILayout.Label("Save Path", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.TextField(savePath, GUILayout.ExpandWidth(false));
if (GUILayout.Button("Browse", GUILayout.ExpandWidth(false)))
savePath = EditorUtility.SaveFolderPanel("Path to Save Images", savePath, Application.dataPath);
EditorGUILayout.EndHorizontal();
EditorGUILayout.HelpBox("Choose the folder in which to save the screenshots", MessageType.None);
EditorGUILayout.Space();
}
private void DrawCameraSettings()
{
GUILayout.Label("Select Camera", EditorStyles.boldLabel);
screenshotCamera = EditorGUILayout.ObjectField(screenshotCamera, typeof(Camera), true) as Camera;
if (screenshotCamera == null)
{
screenshotCamera = Camera.main;
}
isTransparent = EditorGUILayout.Toggle("Transparent Background", isTransparent);
EditorGUILayout.HelpBox("Choose the camera to capture the render. You can make the background transparent using the transparency option.", MessageType.None);
EditorGUILayout.Space();
}
private void DrawDefaultOptions()
{
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Set To Screen Size", GUILayout.Height(30)))
{
// Set resolution to current screen size
resWidth = (int)Handles.GetMainGameViewSize().x;
resHeight = (int)Handles.GetMainGameViewSize().y;
}
if (GUILayout.Button("Default Size", GUILayout.Height(30)))
{
// Set resolution to default size
resWidth = 2560;
resHeight = 1440;
scale = 1;
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
}
private void DrawScreenshotOptions()
{
EditorGUILayout.LabelField($"Screenshot will be taken at {resWidth * scale} x {resHeight * scale} px", EditorStyles.boldLabel);
if (GUILayout.Button("Take Screenshot", GUILayout.MinHeight(60)))
{
if (string.IsNullOrEmpty(savePath))
{
// If save path is not set, prompt user to set it
savePath = EditorUtility.SaveFolderPanel("Path to Save Images", savePath, Application.dataPath);
Debug.Log("Path Set");
TakeHighResShot();
}
else
{
// Take the screenshot
TakeHighResShot();
}
}
EditorGUILayout.Space();
DrawActionButtons();
}
private void DrawActionButtons()
{
openFileAfterScreenshot = EditorGUILayout.Toggle("Open Last File", openFileAfterScreenshot);
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Open Last Screenshot", GUILayout.MaxWidth(160), GUILayout.MinHeight(40)))
{
if (!string.IsNullOrEmpty(lastScreenshot))
{
// Open the last screenshot taken
Application.OpenURL("file://" + lastScreenshot);
Debug.Log("Opening File " + lastScreenshot);
}
}
if (GUILayout.Button("Open Folder", GUILayout.MaxWidth(100), GUILayout.MinHeight(40)))
{
// Open the folder containing the screenshots
Application.OpenURL("file://" + savePath);
}
if (GUILayout.Button("More Assets", GUILayout.MaxWidth(100), GUILayout.MinHeight(40)))
{
// Open the asset store page
Application.OpenURL("https://assetstore.unity.com/publishers/5951");
}
if (GUILayout.Button("Github", GUILayout.MaxWidth(100), GUILayout.MinHeight(40)))
{
// Open the GitHub page
Application.OpenURL("https://github.com/saadnkhawaja/instant-screenshot-unity");
}
EditorGUILayout.EndHorizontal();
}
private string GenerateScreenshotName(int width, int height)
{
// Generate a unique filename for the screenshot
string fileName = string.Format("{0}/screen_{1}x{2}_{3}.png", savePath, width, height, System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
lastScreenshot = fileName;
return fileName;
}
public void TakeHighResShot()
{
Debug.Log("Taking Screenshot");
isTakingScreenshot = true;
}
private void Update()
{
if (isTakingScreenshot)
{
// Calculate resolution
int resWidthN = resWidth * scale;
int resHeightN = resHeight * scale;
RenderTexture rt = new RenderTexture(resWidthN, resHeightN, 24);
screenshotCamera.targetTexture = rt;
// Set texture format based on transparency option
TextureFormat tFormat = isTransparent ? TextureFormat.ARGB32 : TextureFormat.RGB24;
Texture2D screenShot = new Texture2D(resWidthN, resHeightN, tFormat, false);
screenshotCamera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidthN, resHeightN), 0, 0);
screenshotCamera.targetTexture = null;
RenderTexture.active = null;
// Save the screenshot
byte[] bytes = screenShot.EncodeToPNG();
string fileName = GenerateScreenshotName(resWidthN, resHeightN);
System.IO.File.WriteAllBytes(fileName, bytes);
Debug.Log($"Took screenshot to: {fileName}");
ShowNotification(new GUIContent("Screenshot taken!"));
// Open the file if the option is selected
if (openFileAfterScreenshot)
{
Application.OpenURL("file://" + fileName);
}
isTakingScreenshot = false;
}
}
}