SA_ScreenshotMaker.cs
1.27 KB
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
////////////////////////////////////////////////////////////////////////////////
//
// @module Assets Common Lib
// @author Osipov Stanislav (Stan's Assets)
// @support support@stansassets.com
// @website https://stansassets.com
//
////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using System;
using System.Collections;
namespace SA.Common.Models {
public class ScreenshotMaker : MonoBehaviour {
//Actions
public Action<Texture2D> OnScreenshotReady = delegate {};
public static ScreenshotMaker Create() {
return new GameObject("ScreenshotMaker").AddComponent<ScreenshotMaker>();
}
void Awake() {
DontDestroyOnLoad(gameObject);
}
public void GetScreenshot() {
StartCoroutine(SaveScreenshot());
}
private IEnumerator SaveScreenshot() {
yield return new WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D( width, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
tex.Apply();
if(OnScreenshotReady != null) {
OnScreenshotReady(tex);
}
Destroy(gameObject);
}
}
}