forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenShotting.cs
179 lines (159 loc) · 6.28 KB
/
ScreenShotting.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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
namespace UnityEditor
{
internal class ScreenShots
{
public static Color kToolbarBorderColor = new Color(0.54f, 0.54f, 0.54f, 1f);
public static Color kWindowBorderColor = new Color(0.51f, 0.51f, 0.51f, 1f);
public static bool s_TakeComponentScreenshot = false;
[MenuItem("Window/Internal/Screenshot/Set Window Size %&l", false, 1000, true)]
public static void SetMainWindowSize()
{
var main = Resources.FindObjectsOfTypeAll<MainView>()[0];
main.window.position = new Rect(0, 0, 1024, 768);
}
[MenuItem("Window/Internal/Screenshot/Set Window Size Small", false, 1000, true)]
public static void SetMainWindowSizeSmall()
{
var main = Resources.FindObjectsOfTypeAll<MainView>()[0];
main.window.position = new Rect(0, 0, 800 - 38, 600);
}
[MenuItem("Window/Internal/Screenshot/Snap View %&j", false, 1000, true)]
public static void Screenshot()
{
GUIView v = GetMouseOverView();
if (v != null)
{
string name = GetGUIViewName(v);
Rect r = v.screenPosition;
r.y -= 1;
r.height += 2;
SaveScreenShot(r, name);
}
}
[MenuItem("Window/Internal/Screenshot/Snap View Toolbar", false, 1000, true)]
public static void ScreenshotToolbar()
{
GUIView v = GetMouseOverView();
if (v != null)
{
string name = GetGUIViewName(v) + "Toolbar";
Rect r = v.screenPosition;
r.y += 19;
r.height = 16;
r.width -= 2;
SaveScreenShotWithBorder(r, kToolbarBorderColor, name);
}
}
[MenuItem("Window/Internal/Screenshot/Snap View Extended Right %&k", false, 1000, true)]
public static void ScreenshotExtendedRight()
{
GUIView v = GetMouseOverView();
if (v != null)
{
string name = GetGUIViewName(v) + "Extended";
var main = Resources.FindObjectsOfTypeAll<MainView>()[0];
Rect r = v.screenPosition;
r.xMax = main.window.position.xMax;
r.y -= 1;
r.height += 2;
SaveScreenShot(r, name);
}
}
[MenuItem("Window/Internal/Screenshot/Snap Component", false, 1000, true)]
public static void ScreenShotComponent()
{
s_TakeComponentScreenshot = true;
}
public static void ScreenShotComponent(Rect contentRect, Object target)
{
s_TakeComponentScreenshot = false;
contentRect.yMax += 2;
contentRect.xMin += 1;
ScreenShots.SaveScreenShotWithBorder(contentRect, kWindowBorderColor, target.GetType().Name + "Inspector");
}
[MenuItem("Window/Internal/Screenshot/Snap Game View Content", false, 1000, true)]
public static void ScreenGameViewContent()
{
string path = GetUniquePathForName("ContentExample");
ScreenCapture.CaptureScreenshot(path);
Debug.Log(string.Format("Saved screenshot at {0}", path));
}
static GUIView GetMouseOverView()
{
GUIView v = GUIView.mouseOverView;
if (v == null)
{
EditorApplication.Beep();
Debug.LogWarning("Could not take screenshot.");
}
return v;
}
static string GetGUIViewName(GUIView view)
{
HostView host = view as HostView;
if (host != null)
return host.actualView.GetType().Name;
return "Window";
}
public static void SaveScreenShot(Rect r, string name)
{
SaveScreenShot((int)r.width, (int)r.height, InternalEditorUtility.ReadScreenPixel(new Vector2(r.x, r.y), (int)r.width, (int)r.height), name);
}
// Adds a gray border around the screenshot
// Useful for e.g. toolbars because they don't have a nice border all the way round due to the tabs
public static string SaveScreenShotWithBorder(Rect r, Color borderColor, string name)
{
int w = (int)r.width;
int h = (int)r.height;
Color[] colors1 = InternalEditorUtility.ReadScreenPixel(new Vector2(r.x, r.y), w, h);
Color[] colors2 = new Color[(w + 2) * (h + 2)];
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
colors2[x + 1 + (w + 2) * (y + 1)] = colors1[x + w * y];
}
}
for (int x = 0; x < w + 2; x++)
{
colors2[x] = borderColor;
colors2[x + (w + 2) * (h + 1)] = borderColor;
}
for (int y = 0; y < h + 2; y++)
{
colors2[y * (w + 2)] = borderColor;
colors2[y * (w + 2) + (w + 1)] = borderColor;
}
return SaveScreenShot((int)(r.width + 2), (int)(r.height + 2), colors2, name);
}
static string SaveScreenShot(int width, int height, Color[] pixels, string name)
{
Texture2D t = new Texture2D(width, height);
t.SetPixels(pixels, 0);
t.Apply(true);
byte[] bytes = t.EncodeToPNG();
Object.DestroyImmediate(t, true);
string path = GetUniquePathForName(name);
System.IO.File.WriteAllBytes(path, bytes);
Debug.Log(string.Format("Saved screenshot at {0}", path));
return path;
}
static string GetUniquePathForName(string name)
{
string path = string.Format("{0}/../../{1}.png", Application.dataPath, name);
int i = 0;
while (System.IO.File.Exists(path))
{
path = string.Format("{0}/../../{1}{2:000}.png", Application.dataPath, name, i);
i++;
}
return path;
}
}
}