-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathSaveScreenshot.cs
38 lines (29 loc) · 892 Bytes
/
SaveScreenshot.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class SaveScreenshot : MonoBehaviour
{
int width = 1920;
int height = 1080;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Camera cam = Camera.main;
var screenRT = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
cam.targetTexture = screenRT;
cam.Render();
cam.targetTexture = null;
Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
RenderTexture.active = screenRT;
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
RenderTexture.active = null;
screenRT.Release();
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
Debug.Log(Application.temporaryCachePath);
File.WriteAllBytes(Application.temporaryCachePath + "/Screenshot.png", bytes);
}
}
}