截取屏幕并转png格式保存到本地
public static IEnumerator ScreenShotOfReadPixels(Rect rect)
{
yield return new WaitForEndOfFrame();
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
texture = screenShot;
imgbytes = screenShot.EncodeToPNG();
if (Application.platform == RuntimePlatform.WindowsEditor)
{
File.WriteAllBytes(@"C:\Users\we\Desktop\截图\" + key + ".png", imgbytes);
}
}
public static void ScreenShotOfCamera(Camera camera)
{
Rect rect = new Rect(0, 0, Screen.width, Screen.height);
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
camera.targetTexture = rt;
camera.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
texture = screenShot;
camera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
imgbytes = screenShot.EncodeToPNG();
}
DataJson = JsonMapper.ToJson(dataConns); //生成jSON
解析json: