-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenshotCamera.cs
64 lines (42 loc) · 1.27 KB
/
ScreenshotCamera.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
using UnityEngine;
using System.Collections;
public class ScreenshotCamera : MonoBehaviour
{
public static ScreenshotCamera Instance;
public delegate void ScreenReadyEventDelegate ();
public event ScreenReadyEventDelegate ScreenReadyEvent;
public Texture2D screenshot { get; private set; }
private bool capturing = false;
private Rect captureRect;
private int oldAntiAliasingSettings;
private int noAACountdown;
void Awake() {
if (Instance == null) {
Instance = this;
}
}
public void TakeScreenshot (float startX, float startY, float endX, float endY)
{
oldAntiAliasingSettings = QualitySettings.antiAliasing;
QualitySettings.antiAliasing = 0;
captureRect = new Rect (startX, startY, endX - startX, endY - startY);
noAACountdown = 2;
capturing = true;
}
void OnPostRender ()
{
if (capturing)
{
noAACountdown--;
if (noAACountdown > 0)
return;
screenshot = new Texture2D (Mathf.RoundToInt (captureRect.width), Mathf.RoundToInt (captureRect.height), TextureFormat.ARGB32, true);
screenshot.ReadPixels (captureRect, 0, 0, true);
screenshot.Apply ();
capturing = false;
QualitySettings.antiAliasing = oldAntiAliasingSettings;
if (ScreenReadyEvent != null)
ScreenReadyEvent ();
}
}
}