实现代码
private IEnumerator OnlyScreenShot(RectTransform target, string name, ScreenShotCallBack callBack = null, Canvas canvas = null)
{ yield return new WaitForEndOfFrame();
string filePath = GetScreenShotPath(name);
//Canvas的sizeDelta
canvas = canvas ??UGUIRoot.Instance.MainCanvas;
RectTransform canvasTransform = canvas.GetComponent<RectTransform>();
Vector3 canvasSize = canvasTransform.sizeDelta;
RenderTexture screenShotRT = RenderTexture.GetTemporary(Screen.width, Screen.height, 32 );
screenShotRT.name = "ScreenShotTool_ScreenShot_Texture";
screenShotRT.Create();
GrabCameraTexture.GrabCameraTarget(ref screenShotRT, GrabCameraTexture.FinalGrabCameraMask.All, 1);
RenderTexture.active = screenShotRT;
float targetWidth = target.rect.width * target.localScale.x;
float targetHeight = target.rect.height * target.localScale.y;
float WidthRatio = Screen.width / canvasSize.x;
float HeightRatio = Screen.height / canvasSize.y;
Vector3 targetPos = target.transform.position;
var screenPoint = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, targetPos);
var localPosition = new Vector2();
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasTransform, screenPoint, canvas.worldCamera, out localPosition);
#if UNITY_EDITOR
//截取目标
Vector2 origin = new Vector2(((localPosition.x + (canvasSize.x - targetWidth) / 2f) * WidthRatio),
((canvasSize.y - targetHeight) / 2f - target.localPosition.y) * HeightRatio);
#else
//截取目标
Vector2 origin = new Vector2(((localPosition.x + (canvasSize.x - targetWidth) / 2f) * WidthRatio),
((canvasSize.y - targetHeight) / 2f + localPosition.y) * HeightRatio);
#endif
//截取目标的大小
Vector2 size = new Vector2((targetWidth) * WidthRatio, (targetHeight) * HeightRatio);
//截取
Texture2D screenShot = new Texture2D((int)Math.Ceiling(size.x), (int)Math.Ceiling(size.y), TextureFormat.ARGB32, false);
Rect position = new Rect(origin.x, origin.y, size.x, size.y);
//TODO 分析日志
LogInfo($"[OnlyScreenShot] 截取了一张图片: Screen.width:{Screen.width} Screen.height:{Screen.height} canvasSize.x:{canvasSize.x} canvasSize.y:{canvasSize.y}");
LogInfo($"[OnlyScreenShot] 截取了一张图片: WidthRatio:{WidthRatio} HeightRatio:{HeightRatio} screenShotRT.width:{screenShotRT.width} screenShotRT.height:{screenShotRT.height}");
LogInfo($"[OnlyScreenShot] 截取了一张图片: target.rect.width:{target.rect.width} target.rect.height:{target.rect.height} target.localPosition.x:{target.localPosition.x} target.localPosition.y:{target.localPosition.y}");
LogInfo($"[OnlyScreenShot] 截取了一张图片: targetWidth:{targetWidth} target.rect.height:{targetHeight}");
LogInfo($"[OnlyScreenShot] 截取了一张图片: size.x:{size.x} size.y:{size.y} (int)Math.Ceiling(size.x):{(int)Math.Ceiling(size.x)} (int)Math.Ceiling(size.y):{(int)Math.Ceiling(size.y)} ");
LogInfo($"[OnlyScreenShot] 截取了一张图片: Screen.dpi:{Screen.dpi} origin.x:{origin.x} origin.y:{origin.y}");
LogInfo(string.Format("[OnlyScreenShot] 截取了一张图片1: {0}", filePath));
screenShot.ReadPixels(position, 0, 0); //按照设定区域读取像素
screenShot.Apply();
yield return screenShot;
RenderTexture.active = null;
Logger.LogInfo(string.Format("[OnlyScreenShot] 截取了一张图片2: {0}", filePath));
//保存
byte[] byt = screenShot.EncodeToPNG();
File.WriteAllBytes(filePath, byt);
GrabCameraTexture.ReleaseCameraTarget(ref screenShotRT);
DestroyImmediate(screenShot);
screenShot = null;
screenShotRT = null;
Logger.LogInfo(string.Format("[OnlyScreenShot] 截取了一张图片3: {0}", filePath));
callBack?.Invoke(filePath);
yield break;
}
区域截取时编辑器和移动端看到的效果不一致问题,是因为移动端和编辑器下,Rect的坑,计算position是注意一下即可
//编辑器下,rect起点(0,0)在左上角!!!
//移动端下,rect起点(0,0)在左下角!!!