/// <summary>
/// Captures the screenshot2.
/// </summary>
/// <returns>The screenshot2.</returns>
/// <param name="rect">Rect.截图的区域,左下角为o点</param>
public static IEnumerator CaptureScreenshot2(Rect rect, Action<Texture2D> onResult)
{
yield return new WaitForEndOfFrame();
// 先创建一个的空纹理,大小可根据实现需要来设置
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
// 读取屏幕像素信息并存储为纹理数据,
screenShot.ReadPixels(rect, 0, 0);
//缩放2倍图片进行压缩
screenShot = ScaleTexture(screenShot,screenShot.width / 2, screenShot / 2);
screenShot.Apply();
onResult?.Invoke(screenShot);
}
public static Texture2D ScaleTexture (Texture2D source, int targetWidth, int targetHeight)
{
Texture2D result = new Texture2D (targetWidth, targetHeight, source.format, true);
Color[] rpixels = result.GetPixels (0);
float incX = ((float)1 / source.width) * ((float)source.width / targetWidth);
float incY = ((float)1 / source.height) * ((float)source.height / targetHeight);
for (int px = 0; px < rpixels.Length; px++) {
rpixels [px] = source.GetPixelBilinear (incX * ((float)px % targetWidth), incY * ((float)Mathf.Floor (px / targetWidth)));
}
result.SetPixels (rpixels, 0);
result.Apply ();
return result;
}