/// <summary>
/// 左下角的截图坐标
/// </summary>
public Image leftBottomImg;
/// <summary>
/// 右上角的截图坐标
/// </summary>
public Image rightTopImg;
/// <summary>
/// 截图后显示的载体
/// </summary>
public Image showImg;
private int capBeginX;
private int capBeginY;
private int capFinishX;
private int capFinishY;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
Vector2 beginPos = new Vector2(mousePos.x, mousePos.y);
capBeginX = (int)mousePos.x;
capBeginY = (int)mousePos.y;
}
if (Input.GetMouseButtonUp(0))
{
Vector3 mousePos = Input.mousePosition;
Vector2 finishPos = new Vector2(mousePos.x, mousePos.y);
capFinishX = (int)mousePos.x;
capFinishY = (int)mousePos.y;
//重新计算截取的位置
int capLeftX = (capBeginX < capFinishX) ? capBeginX : capFinishX;
int capRightX = (capBeginX < capFinishX) ? capFinishX : capBeginX;
int capLeftY = (capBeginY < capFinishY) ? capBeginY : capFinishY;
int capRightY = (capBeginY < capFinishY) ? capFinishY : capBeginY;
Rect rect = new Rect(capLeftX, capLeftY, capRightX, capRightY);
StartCoroutine(Captrue(rect));
}
if(Input.GetMouseButtonUp(2))
{
//StartCoroutine(Captrue2());
StartCoroutine(Captrue(leftBottomImg.rectTransform.position,rightTopImg.rectTransform.position));
}
}
IEnumerator Captrue(Vector2 leftBottomPoint,Vector2 rightTopPoint)
{
int t_width = (int)Mathf.Abs(rightTopPoint.x - leftBottomPoint.x);
int t_length = (int)Mathf.Abs(rightTopPoint.y - leftBottomPoint.y);
Rect rect = new Rect(leftBottomPoint.x, leftBottomPoint.y, t_width, t_length);
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(t_width, t_length, TextureFormat.RGB24, true);//需要正确设置好图片保存格式
t.ReadPixels(rect, 0, 0, false);//按照设定区域读取像素;注意是以左下角为原点读取
t.Apply();
byte[] byt = t.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + Time.time + ".png", byt);
Sprite target = Sprite.Create(t, new Rect(0, 0, t_width, t_length), Vector2.zero);
showImg.sprite = target;
}
IEnumerator Captrue2()
{
int t_width =(int)Mathf.Abs(rightTopImg.rectTransform.position.x - leftBottomImg.rectTransform.position.x);
int t_length = (int)Mathf.Abs(rightTopImg.rectTransform.position.y - leftBottomImg.rectTransform.position.y);
Rect rect= new Rect(leftBottomImg.rectTransform.position.x, leftBottomImg.rectTransform.position.y, t_width, t_length);
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(t_width, t_length, TextureFormat.RGB24, true);//需要正确设置好图片保存格式
t.ReadPixels(rect, 0, 0, false);//按照设定区域读取像素;注意是以左下角为原点读取
t.Apply();
byte[] byt = t.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + Time.time + ".png", byt);
Sprite target = Sprite.Create(t, new Rect(0, 0, t_width, t_length), Vector2.zero);
showImg.sprite = target;
}
IEnumerator Captrue(Rect rect)
{
int t_width = Mathf.Abs(capFinishX - capBeginX);
int t_length = Mathf.Abs(capFinishY - capBeginY);
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(t_width, t_length, TextureFormat.RGB24, true);//需要正确设置好图片保存格式
t.ReadPixels(rect, 0, 0, false);//按照设定区域读取像素;注意是以左下角为原点读取
t.Apply();
byte[] byt = t.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + Time.time + ".png", byt);
Sprite target = Sprite.Create(t, new Rect(0, 0, t_width, t_length), Vector2.zero);
showImg.sprite = target;
}