注释
unity 对某一矩形UI进行截取
cpp
/// <summary>
/// 截取UI
/// </summary>
/// <param name="UIRect">UI(中点在最下面)</param>
/// <param name="mFileName">路径</param>
/// <returns></returns>
public IEnumerator CaptureByUI(RectTransform UIRect, string mFileName)
{
//等待帧画面渲染结束
yield return new WaitForEndOfFrame();
Vector3[] corners = new Vector3[4];
UIRect.GetWorldCorners(corners);
float a = corners[2].x - corners[0].x;
float b = corners[1].y - corners[0].y;
Texture2D tex = new Texture2D((int)a, (int)b, TextureFormat.RGB24, false);
//从屏幕读取像素, leftBtmX/leftBtnY 是读取的初始位置,width、height是读取像素的宽度和高度
tex.ReadPixels(new Rect(corners[0].x, corners[0].y, a, b), 0, 0);
Debug.Log(new Rect(corners[0].x, corners[0].y, a, b));
//执行读取操作
tex.Apply();
rawImageIcon.texture = tex;
byte[] bytes = tex.EncodeToPNG();
//保存
File.WriteAllBytes(mFileName, bytes);
//DownLoadImg(bytes);
}