Unity--截取屏幕任意区域

时间:2022-07-19 18:25:20

原地址:http://blog.csdn.net/tanmengwen/article/details/8501612

void Update () {
if(Input.GetKeyDown(KeyCode.A))
{
StartCoroutine(getScreenTexture());
//unity 自带截屏,只能是截全屏
Application.CaptureScreenshot("shot.png");
}
} public int capx=;
public int capy=;
public int capwidth=;
public int capheight=;
IEnumerator getScreenTexture()
{
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(capwidth, capheight,TextureFormat.RGB24, true);//需要正确设置好图片保存格式
t.ReadPixels(new Rect(capx,capy,capwidth,capheight), , , false);//按照设定区域读取像素;注意是以左下角为原点读取
t.Apply();
//二进制转换
byte[] byt = t.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + Time.time + ".png", byt);
}