-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenshot.cs
147 lines (112 loc) · 3.71 KB
/
Screenshot.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
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
[ExecuteInEditMode]
public class Screenshot : EditorWindow
{
int ssWidth = 2400;
int ssHeight = 1600;
int ssScale = 1;
bool setBGTransparent = false;
string ssPath = "";
public Camera renderCam;
TextureFormat ssTexFormat;
public string ScreenShotName(int width, int height)
{
string sceneName = SceneManager.GetActiveScene().name;
string ssCompletePath = string.Format("{0}/" + sceneName + "_SS_{1}x{2}_{3}.png",
ssPath,
width, height,
System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
return ssCompletePath;
}
[MenuItem("Tools/Screenshot Tool")]
public static void ShowWindow()
{
EditorWindow ssWindow = GetWindow(typeof(Screenshot));
ssWindow.autoRepaintOnSceneChange = true;
ssWindow.titleContent = new GUIContent("Screenshot Tool");
ssWindow.maxSize = new Vector2(268f, 386f);
ssWindow.minSize = ssWindow.maxSize;
ssWindow.Show();
}
void TakeScreenshot()
{
int resWidthN = ssWidth * ssScale;
int resHeightN = ssHeight * ssScale;
RenderTexture renderTex = new RenderTexture(resWidthN, resHeightN, 24);
renderCam.targetTexture = renderTex;
if (setBGTransparent)
{
ssTexFormat = TextureFormat.ARGB32;
}
else
{
ssTexFormat = TextureFormat.RGB24;
}
Texture2D ssTex2D = new Texture2D(resWidthN, resHeightN, ssTexFormat, false);
renderCam.Render();
RenderTexture.active = renderTex;
ssTex2D.ReadPixels(new Rect(0, 0, resWidthN, resHeightN), 0, 0);
renderCam.targetTexture = null;
RenderTexture.active = null;
byte[] ssBytes = ssTex2D.EncodeToPNG();
string ssFilename = ScreenShotName(resWidthN, resHeightN);
System.IO.File.WriteAllBytes(ssFilename, ssBytes);
Application.OpenURL(ssFilename);
}
void OnGUI()
{
EditorGUILayout.LabelField ("Resolution", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Default Res"))
{
ssWidth = 2400;
ssHeight = 1600;
ssScale = 1;
}
if (GUILayout.Button("Window Res"))
{
ssHeight = (int)Handles.GetMainGameViewSize().y;
ssWidth = (int)Handles.GetMainGameViewSize().x;
}
EditorGUILayout.EndHorizontal();
GUILayout.Label("Select Render Camera", EditorStyles.boldLabel);
renderCam = EditorGUILayout.ObjectField(renderCam, typeof(Camera), true, null) as Camera;
if (renderCam == null)
{
renderCam = Camera.main;
}
EditorGUILayout.HelpBox("Choose an appropriate width and height.\nThe image will be cropped.", MessageType.Warning);
ssWidth = EditorGUILayout.IntField ("Width: ", ssWidth);
ssHeight = EditorGUILayout.IntField ("Height: ", ssHeight);
EditorGUILayout.Space();
EditorGUILayout.HelpBox("Multiply res without quality loss (max 4x).", MessageType.Info);
ssScale = EditorGUILayout.IntSlider ("Scale: ", ssScale, 1, 4);
EditorGUILayout.Space();
GUILayout.Label("Background", EditorStyles.boldLabel);
setBGTransparent = EditorGUILayout.Toggle("Transparency Support?", setBGTransparent);
EditorGUILayout.Space();
GUILayout.Label ("Screenshot File Path:", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.TextField(ssPath,GUILayout.ExpandWidth(false));
if(ssPath == null || ssPath == "")
{
ssPath = Application.dataPath;
}
if (GUILayout.Button("Browse", GUILayout.ExpandWidth(false)))
{
ssPath = EditorUtility.SaveFolderPanel("Path to Save Images", ssPath, Application.dataPath);
}
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("Open Folder...", GUILayout.MaxWidth(100), GUILayout.MinHeight(20)))
{
Application.OpenURL("file://" + ssPath);
}
EditorGUILayout.Space();
if(GUILayout.Button("Take Screenshot!",GUILayout.MinHeight(40)))
{
TakeScreenshot();
}
}
}