Unity运行时点击相应按钮进行截屏,此功能为截全屏
void Screenshot()
{
Application.CaptureScreenshot(Application.dataPath + "Screenshot" + System.DateTime.Now.Second + ".png");
PathTips.text = "截图完成,保存在" + Application.dataPath + "中";
StartCoroutine(Clear());
}
private IEnumerator Clear()
{
yield return new WaitForSeconds(2);
PathTips.text = null;
}
*********************************************
Application.CaptureScreenshot(Application.dataPath + "Screenshot" + System.DateTime.Now.Second + ".png");第一个参数为保存路径,第二个参数为放大倍数,不填则默认为0,为0时表示不进行放大,参数为1时表示放大一倍,参数为2时放大两倍
局部截屏
IEnumerator Screenshot(Rect rect)
{
//等待所有摄像机和GUI被渲染完之后调用
yield return new WaitForEndOfFrame();
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
//设置偏移
screenShot.ReadPixels(new Rect(50, 50, rect.width, rect.height), 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
Destroy(screenShot);
File.WriteAllBytes(Application.dataPath + "/Screenshot1.png", bytes);
}
需要截屏时调用此方法即可,一开始我没用协成,总是报错,和曾经的老师探讨了一个多小时依然没有效果,今天才发现,然来是只能用协成,等所有相机和UI渲染完毕之后再进行截屏就可以了