This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
/
ScreenshotTaker.cs
215 lines (143 loc) · 5.32 KB
/
ScreenshotTaker.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
//C# Example
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class Screenshot : EditorWindow
{
int resWidth = Screen.width*4;
int resHeight = Screen.height*4;
public Camera myCamera;
int scale = 1;
string path = "";
bool showPreview = true;
RenderTexture renderTexture;
bool isTransparent = false;
// Add menu item named "My Window" to the Window menu
[MenuItem("Tools/Saad Khawaja/Instant High-Res Screenshot")]
public static void ShowWindow()
{
//Show existing window instance. If one doesn't exist, make one.
EditorWindow editorWindow = EditorWindow.GetWindow(typeof(Screenshot));
editorWindow.autoRepaintOnSceneChange = true;
editorWindow.Show();
editorWindow.title = "Screenshot";
}
float lastTime;
void OnGUI()
{
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 loosing quality.",MessageType.None);
EditorGUILayout.Space();
GUILayout.Label ("Save Path", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.TextField(path,GUILayout.ExpandWidth(false));
if(GUILayout.Button("Browse",GUILayout.ExpandWidth(false)))
path = EditorUtility.SaveFolderPanel("Path to Save Images",path,Application.dataPath);
EditorGUILayout.EndHorizontal();
EditorGUILayout.HelpBox("Choose the folder in which to save the screenshots ",MessageType.None);
EditorGUILayout.Space();
//isTransparent = EditorGUILayout.Toggle(isTransparent,"Transparent Background");
GUILayout.Label ("Select Camera", EditorStyles.boldLabel);
myCamera = EditorGUILayout.ObjectField(myCamera, typeof(Camera), true,null) as Camera;
if(myCamera == null)
{
myCamera = Camera.main;
}
isTransparent = EditorGUILayout.Toggle("Transparent Background", isTransparent);
EditorGUILayout.HelpBox("Choose the camera of which to capture the render. You can make the background transparent using the transparency option.",MessageType.None);
EditorGUILayout.Space();
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField ("Default Options", EditorStyles.boldLabel);
if(GUILayout.Button("Set To Screen Size"))
{
resHeight = (int)Handles.GetMainGameViewSize().y;
resWidth = (int)Handles.GetMainGameViewSize().x;
}
if(GUILayout.Button("Default Size"))
{
resHeight = 1440;
resWidth = 2560;
scale = 1;
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
EditorGUILayout.LabelField ("Screenshot will be taken at " + resWidth*scale + " x " + resHeight*scale + " px", EditorStyles.boldLabel);
if(GUILayout.Button("Take Screenshot",GUILayout.MinHeight(60)))
{
if(path == "")
{
path = EditorUtility.SaveFolderPanel("Path to Save Images",path,Application.dataPath);
Debug.Log("Path Set");
TakeHiResShot();
}
else
{
TakeHiResShot();
}
}
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
if(GUILayout.Button("Open Last Screenshot",GUILayout.MaxWidth(160),GUILayout.MinHeight(40)))
{
if(lastScreenshot != "")
{
Application.OpenURL("file://" + lastScreenshot);
Debug.Log("Opening File " + lastScreenshot);
}
}
if(GUILayout.Button("Open Folder",GUILayout.MaxWidth(100),GUILayout.MinHeight(40)))
{
Application.OpenURL("file://" + path);
}
if(GUILayout.Button("More Assets",GUILayout.MaxWidth(100),GUILayout.MinHeight(40)))
{
Application.OpenURL("https://www.assetstore.unity3d.com/en/#!/publisher/5951");
}
EditorGUILayout.EndHorizontal();
if (takeHiResShot)
{
int resWidthN = resWidth*scale;
int resHeightN = resHeight*scale;
RenderTexture rt = new RenderTexture(resWidthN, resHeightN, 24);
myCamera.targetTexture = rt;
TextureFormat tFormat;
if(isTransparent)
tFormat = TextureFormat.ARGB32;
else
tFormat = TextureFormat.RGB24;
Texture2D screenShot = new Texture2D(resWidthN, resHeightN, tFormat,false);
myCamera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidthN, resHeightN), 0, 0);
myCamera.targetTexture = null;
RenderTexture.active = null;
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidthN, resHeightN);
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
//Application.OpenURL(filename);
takeHiResShot = false;
}
EditorGUILayout.HelpBox("In case of any error, make sure you have Unity Pro as the plugin requires Unity Pro to work.",MessageType.Info);
}
private bool takeHiResShot = false;
public string lastScreenshot = "";
public string ScreenShotName(int width, int height) {
string strPath="";
strPath = string.Format("{0}/screen_{1}x{2}_{3}.png",
path,
width, height,
System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
lastScreenshot = strPath;
return strPath;
}
public void TakeHiResShot() {
Debug.Log("Taking Screenshot");
takeHiResShot = true;
}
}