-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenRecorder.cs
363 lines (273 loc) · 12.8 KB
/
ScreenRecorder.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using UnityEngine;
using System.Collections;
using System.IO;
// root camera position:
// x = -59
// z = -100
// y = 374.7 (usually- this can change)
// Screen Recorder will save individual images of active scene in any resolution and of a specific image format
// including raw, jpg, png, and ppm. Raw and PPM are the fastest image formats for saving.
//
// You can compile these images into a video using ffmpeg:
// ffmpeg -i screen_3840x2160_%d.ppm -y test.avi
public class ScreenRecorder : MonoBehaviour
{
Camera cam;
// 4k = 3840 x 2160 1080p = 1920 x 1080
public int captureWidth = 768;
public int captureHeight = 768;
// optional game object to hide during screenshots (usually your scene canvas hud)
public GameObject hideGameObject;
// optimize for many screenshots will not destroy any objects so future screenshots will be fast
public bool optimizeForManyScreenshots = true;
// configure with raw, jpg, png, or ppm (simple raw format)
public enum Format { RAW, JPG, PNG, PPM };
public Format format = Format.PNG;
// folder to write output (defaults to data path)
public string folder;
// private vars for screenshot
private Rect rect;
private RenderTexture renderTexture;
private Texture2D screenShot;
private int counter = 0; // image #
// commands
private bool captureScreenshot = false;
private bool captureVideo = false;
// create a unique filename using a one-up variable
private string uniqueFilename(int width, int height)
{
// if folder not specified by now use a good default
if (folder == null || folder.Length == 0)
{
folder = Application.dataPath;
if (Application.isEditor)
{
// put screenshots in folder above asset path so unity doesn't index the files
var stringPath = folder + "/..";
folder = Path.GetFullPath(stringPath);
}
folder += "/assets/screenshots";
// make sure directoroy exists
System.IO.Directory.CreateDirectory(folder);
// count number of files of specified format in folder
string mask = string.Format("screen_{0}x{1}*.{2}", width, height, format.ToString().ToLower());
counter = Directory.GetFiles(folder, mask, SearchOption.TopDirectoryOnly).Length;
}
// use width, height, and counter for unique file name
var filename = string.Format("{0}/screen_{1}x{2}_{3}.{4}", folder, width, height, counter, format.ToString().ToLower());
// up counter for next call
++counter;
// return unique filename
return filename;
}
void Start()
{
//delete folder and recreate
string screenshot_dir = "assets/screenshots";
Directory.Delete(screenshot_dir, true);
Directory.CreateDirectory(screenshot_dir);
string data_dir = "assets/data";
Directory.Delete(data_dir, true);
Directory.CreateDirectory(data_dir);
}
public void CaptureScreenshot()
{
captureScreenshot = true;
}
public void write_data(string filename)
{
cam = GetComponent<Camera>();
//BOTTOM LEFT
//create list
GameObject[] bottom_left;
bottom_left = GameObject.FindGameObjectsWithTag ("bottom_left");
// record pos of bottom left coords
Vector3[] screen_shot_pos_left = new Vector3[bottom_left.Length];
// record names of bottom left coords
string[] names_bottom_left = new string[bottom_left.Length];
// iterate through all bottom left points
for (int i=0;i<bottom_left.Length;i++)
{
// convert coords to camera coords
screen_shot_pos_left[i] = cam.WorldToScreenPoint(bottom_left[i].transform.position);
// records names into arr
names_bottom_left[i] = bottom_left[i].name;
//Debug.Log(string.Format("position = {0}, name = {1}", screen_shot_pos_left[i], names_bottom_left[i]));
}
// initialise serialized data string
string serializedData = "";
// prep serialized data string
for (int i=0;i<screen_shot_pos_left.Length;i++)
{
serializedData = serializedData + filename + ", " + names_bottom_left[i] + ", " + screen_shot_pos_left[i].x + "," + screen_shot_pos_left[i].y + "\n";
}
//TOP RIGHT
//create list
GameObject[] top_right;
top_right = GameObject.FindGameObjectsWithTag ("top_right");
// record pos of bottom left coords
Vector3[] screen_shot_pos_right = new Vector3[top_right.Length];
// record names of bottom left coords
string[] names_top_right = new string[top_right.Length];
// iterate through all bottom left points
for (int i=0;i<top_right.Length;i++)
{
// convert coords to camera coords
screen_shot_pos_right[i] = cam.WorldToScreenPoint(top_right[i].transform.position);
// records names into arr
names_top_right[i] = top_right[i].name;
//Debug.Log(string.Format("position = {0}, name = {1}", screen_shot_pos_right[i], names_top_right[i]));
}
// prep serialized data string
for (int i=0;i<screen_shot_pos_right.Length;i++)
{
serializedData = serializedData + filename + ", " + names_top_right[i] + ", " + screen_shot_pos_right[i].x + "," + screen_shot_pos_right[i].y + "\n";
}
//TOP LEFT
//create list
GameObject[] top_left;
top_left = GameObject.FindGameObjectsWithTag ("top_left");
// record pos of bottom left coords
Vector3[] screen_shot_pos_top_left = new Vector3[top_left.Length];
// record names of bottom left coords
string[] names_top_left = new string[top_left.Length];
// iterate through all bottom left points
for (int i=0;i<top_left.Length;i++)
{
// convert coords to camera coords
screen_shot_pos_top_left[i] = cam.WorldToScreenPoint(top_left[i].transform.position);
// records names into arr
names_top_left[i] = top_left[i].name;
//Debug.Log(string.Format("position = {0}, name = {1}", screen_shot_pos_top_left[i], names_top_left[i]));
}
// prep serialized data string
for (int i=0;i<screen_shot_pos_top_left.Length;i++)
{
serializedData = serializedData + filename + ", " + names_top_left[i] + ", " + screen_shot_pos_top_left[i].x + "," + screen_shot_pos_top_left[i].y + "\n";
}
//BOTTOM RIGHT
//create list
GameObject[] bottom_right;
bottom_right = GameObject.FindGameObjectsWithTag ("bottom_right");
// record pos of bottom left coords
Vector3[] screen_shot_pos_bottom_right = new Vector3[bottom_right.Length];
// record names of bottom left coords
string[] names_bottom_right = new string[bottom_right.Length];
// iterate through all bottom left points
for (int i=0;i<bottom_right.Length;i++)
{
// convert coords to camera coords
screen_shot_pos_bottom_right[i] = cam.WorldToScreenPoint(bottom_right[i].transform.position);
// records names into arr
names_bottom_right[i] = bottom_right[i].name;
//Debug.Log(string.Format("position = {0}, name = {1}", screen_shot_pos_bottom_right[i], names_bottom_right[i]));
}
// prep serialized data string
for (int i=0;i<screen_shot_pos_bottom_right.Length;i++)
{
serializedData = serializedData + filename + ", " + names_bottom_right[i] + ", " + screen_shot_pos_bottom_right[i].x + "," + screen_shot_pos_bottom_right[i].y + "\n";
}
// write to txt
System.IO.File.WriteAllText("Assets/data/"+filename+".txt", serializedData);
}
public void take_screenshot(bool captureScreenshot, bool captureVideo) {
if (captureScreenshot || captureVideo)
{
//set to false
captureScreenshot = false;
// hide optional game object if set
if (hideGameObject != null) {
GameObject.Find("lower2").SetActive(false);
}
// create screenshot objects if needed
if (renderTexture == null)
{
// creates off-screen render texture that can rendered into
rect = new Rect(0, 0, captureWidth, captureHeight);
renderTexture = new RenderTexture(captureWidth, captureHeight, 24);
screenShot = new Texture2D(captureWidth, captureHeight, TextureFormat.RGB24, false);
}
// get main camera and manually render scene into rt
Camera camera = this.GetComponent<Camera>(); // NOTE: added because there was no reference to camera in original script; must add this script to Camera
camera.targetTexture = renderTexture;
camera.Render();
// read pixels will read from the currently active render texture so make our offscreen
// render texture active and then read the pixels
RenderTexture.active = renderTexture;
screenShot.ReadPixels(rect, 0, 0);
// reset active camera texture and render texture
camera.targetTexture = null;
RenderTexture.active = null;
// get our unique filename
string filename = uniqueFilename((int) rect.width, (int) rect.height);
string[] words = filename.Split('/');
string last_word = words[words.Length - 1];
// pull in our file header/data bytes for the specified image format (has to be done from main thread)
byte[] fileHeader = null;
byte[] fileData = null;
if (format == Format.RAW)
{
fileData = screenShot.GetRawTextureData();
}
else if (format == Format.PNG)
{
fileData = screenShot.EncodeToPNG();
}
else if (format == Format.JPG)
{
fileData = screenShot.EncodeToJPG();
}
else // ppm
{
// create a file header for ppm formatted file
string headerStr = string.Format("P6\n{0} {1}\n255\n", rect.width, rect.height);
fileHeader = System.Text.Encoding.ASCII.GetBytes(headerStr);
fileData = screenShot.GetRawTextureData();
}
// create new thread to save the image to file (only operation that can be done in background)
new System.Threading.Thread(() =>
{
// create file and write optional header with image bytes
var f = System.IO.File.Create(filename);
if (fileHeader != null) f.Write(fileHeader, 0, fileHeader.Length);
f.Write(fileData, 0, fileData.Length);
f.Close();
//Debug.Log(string.Format("Wrote screenshot {0} of size {1}", filename, fileData.Length));
}).Start();
//write data
write_data(last_word);
// unhide optional game object if set
if (hideGameObject != null) hideGameObject.SetActive(true);
// cleanup if needed
if (optimizeForManyScreenshots == false)
{
Destroy(renderTexture);
renderTexture = null;
screenShot = null;
}
}
}
void Update()
{
// check keyboard 'k' for one time screenshot capture and holding down 'v' for continious screenshots
//captureScreenshot |= Input.GetKeyDown("k");
//captureVideo = Input.GetKey("v");
captureScreenshot = true;
double left_x = -60;
double right_x = 450;
double top_z = -97;
double bottom_z = -348;
// go across
Vector3 cam_pos = Camera.main.transform.position;
if (left_x<=cam_pos.x && cam_pos.x<=right_x) {
transform.Translate(10, 0, 0);
take_screenshot(captureScreenshot, captureVideo);
}
else if (bottom_z<=cam_pos.z && cam_pos.z<=top_z) {
//move down by 38 points
Vector3 new_pos = new Vector3 ((float)left_x, (float)Camera.main.transform.position.y,(float)(Camera.main.transform.position.z-38));
Camera.main.transform.position = new_pos;
}
Debug.Log(string.Format("position = {0}", Camera.main.transform.position));
}
}