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
|
int width = Screen.width;
int height = Screen.height;
// 读取屏幕像素并存成Texture2D
var screenCapture = new Texture2D(width, height, TextureFormat.RGB24, false);
// 读取屏幕像素信息并存储为纹理数据
screenCapture.ReadPixels(new Rect(0, 0, width, height), destX, destY);
// Rect定义截取的区域, 左下角为(0,0),右上角为(width, height), destX,destY是偏移
screenCapture.Apply();
// 采样以缩小截图的分辨率
int targetWidth = (int) (screenCapture.width * 0.2f);
int targetHeight = (int) (screenCapture.height * 0.2f);
var targetCapture = new Texture2D(targetWidth, targetHeight, screenCapture.format, false);
Color[] rpixels = targetCapture.GetPixels(0);
float incX=((float)1/screenCapture.width)*((float)screenCapture.width/targetWidth);
float incY=((float)1/screenCapture.height)*((float)screenCapture.height/targetHeight);
for(int px=0; px<rpixels.Length; px++) {
rpixels[px] = screenCapture.GetPixelBilinear(incX*((float)px%targetWidth), incY*Mathf.Floor(px/targetWidth));
}
targetCapture.SetPixels(rpixels, 0);
targetCapture.Apply();
// 把Texture2D编码并存储
byte[] bytes = targetCapture.EncodeToJPG();
string path = Application.persistentDataPath + "/ScreenShot" + level + ".png";
System.IO.File.WriteAllBytes(path, bytes);
|