在unity中利用ReadRixels可以实现截屏,现在分析一下各个参数的作用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenShot : MonoBehaviour {
public Texture2D texture;//建立一个纹理图保存图片,设为public方便查看
public int a, b, c, d,e,f;//测试用变量,改变值并测试
// Use this for initialization
void Start () {
texture = new Texture2D(1920, 1080, TextureFormat.RGB24, false);//初始化纹理
}
{
if (Input.GetKeyUp(KeyCode.A)){
texture.ReadPixels(new Rect(a, b, c, d), e, f);
texture.Apply();//按下a键实现截屏,abcd为截取范围,ef为保存位置, 最后apple一下保存
}
}
}
rect:这个对象是用来存储成对出现的参数,比如,一个矩形框的左上角坐标、宽度和高度。这里用它表示截取范围。
如图,ab为开始点坐标,cd表示截取大小(相对开始选择范围)。
ef则表示保存时左下角据图片左下角的坐标。(注意截取范围与保存都不要越界)
截图完成时会显示如下错误。
ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.
这是由于屏幕没有渲染完就截屏造成的。
实际使用中应使用携程,调用yield return new WaitForEndOfFrame();//链接有关报错