//根据screenshotRectTransform 进行截图 他的坐标点必须为0.0 不然会有偏差
public IEnumerator CaptureScreen(Action<string> ScreenshotCall)
{string ssname = "lastss.png";string sspath = Path.poraryCachePath, ssname);if (File.Exists(sspath)){File.Delete(sspath);}Texture2D texture1;byte[] bytes;// Wait for screen rendering to completeyield return new WaitForEndOfFrame();Rect rect = GetScreenshotRect(null);texture1 = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);texture1.ReadPixels(rect, 0, 0);texture1.Apply();yield return 0;bytes = texture1.EncodeToPNG();File.WriteAllBytes(sspath, bytes);ScreenshotCall.Invoke(sspath);
}//获取截图区域对应的Rect
private Rect GetScreenshotRect(RectTransform screenshotRectTransform)
{return new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f); //直接截全屏的图Rect m_Rect;float originalVillegas = 1280.00f / 720.00f;float gap = 0.66f;float des = Screen.width - originalVillegas * Screen.height;m_Rect = new Rect();Vector3[] fourCornersArray = new Vector3[4];screenshotRectTransform.GetWorldCorners(fourCornersArray);m_Rect.width = fourCornersArray[2].x - fourCornersArray[0].x;m_Rect.height = fourCornersArray[2].y - fourCornersArray[0].y;m_Rect.width = screenshotRectTransform.sizeDelta.x * (Screen.width - des) / 1000.00f * gap;m_Rect.height = screenshotRectTransform.sizeDelta.y * Screen.height / 1000.00f * 1.05f;m_Rect.x = Screen.width * 0.5f - m_Rect.width / 2;m_Rect.y = Screen.height * 0.5f - m_Rect.height / 2;return m_Rect;
}
//微信分享截屏public void WeChatShareScreen(){CoroutineMgr.StartCoroutinee(CaptureScreen(ScreenFinsh));}//截屏完成private void ScreenFinsh(string imagePath){WeChatShareImage(imagePath, "", "", WxShareSceneType.Friend);}
/// <summary>
/// 截图保存,目前是保存到 win 上
/// </summary>
/// <param name="aObject"></param>
/// <param name="contents"></param>
/// <param name="pngName"></param>
/// <returns></returns>
public async ETTask SaveRenderTextureToPNG(GObject aObject)
{DisplayObject dObject = aObject.displayObject;dObject.EnterPaintingMode(1024, null);//纹理将在本帧渲染后才能更新,所以访问纹理的代码需要延迟到下一帧执行。await ETModel.Game.Scene.GetComponent<TimerComponent>().WaitAsync(10);RenderTexture rt = (RenderTexture)ure.nativeTexture;//得到tex后,你可以使用Unity的方法保存为图片或者进行其他处理。具体处理略。//处理结束后结束绘画模式。id要和Enter方法的对应。dObject.LeavePaintingMode(1024);RenderTexture prev = RenderTexture.active;RenderTexture.active = rt;Texture2D png = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);png.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);byte[] bytes = png.EncodeToPNG();string ssname = "lastss.png";string sspath = Path.poraryCachePath, ssname);Log.Info($"sspath----------->{sspath}");if (File.Exists(sspath)){File.Delete(sspath);}File.WriteAllBytes(sspath, bytes);Texture2D.DestroyImmediate(png);png = null;RenderTexture.active = prev;}
// TODO 现在是截图
await Game.Scene.GetComponent<GlobalComponent>().SaveRenderTextureToPNG(this.fui.GObject);
本文发布于:2024-02-01 00:01:06,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170671686932351.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |