欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > 【Unity - 屏幕截图】技术要点

【Unity - 屏幕截图】技术要点

2024/10/23 23:19:58 来源:https://blog.csdn.net/gzylongxingtianxia/article/details/142961486  浏览:    关键词:【Unity - 屏幕截图】技术要点

在Unity中想要实现全屏截图或者截取某个对象区域的图片都是可以通过下面的函数进行截取

Texture2D/// <summary>///   <para>Reads the pixels from the current render target (the screen, or a RenderTexture), and writes them to the texture.</para>/// </summary>/// <param name="source">The region of the render target to read from.</param>/// <param name="destX">The horizontal pixel position in the texture to write the pixels to.</param>/// <param name="destY">The vertical pixel position in the texture to write the pixels to.</param>/// <param name="recalculateMipMaps">If this parameter is true, Unity automatically recalculates the mipmaps for the texture after writing the pixel data. Otherwise, Unity does not do this automatically.</param>public void ReadPixels(Rect source, int destX, int destY, [DefaultValue("true")] bool recalculateMipMaps)

Texture2D.ReadPixels() 函数参数可以翻译注释了解用法,比较简单,我下面简单说一下:

source: 读取像素的区域,坐标起点是 左下角,这个要注意

destX: 读取后写入到texture的坐标X

destY: 读取后写入到texture的坐标Y

recalculateMipMaps : 是否重新计算minimaps,逐级渐远纹理,一般都会关闭的,直接False

好,接下来,当你点击截图按钮,调用函数

    public void SaveImage(string path){CoroutineManager.StartMonoCoroutine(SavePng(path));}

开启协程, 我用的是自己封装的管理器,你可以用原装的

然后开始截图操作,然后保存

     private IEnumerator SavePng(string path){//这里一定要等到帧渲染完毕yield return new WaitForEndOfFrame();//截图区域的本地坐标转换成 屏幕坐标var screenPoint = UiConfig.camera.WorldToScreenPoint(m_ScanImage.transform.position);//适配比例float ratio_x = Screen.width / 3662f; //注意这里,一定要加上比例//float ratio_y = Screen.height / 2060f;var imageWidth = m_ScanImage.rectTransform.sizeDelta.x * ratio_x;var imageHeight = m_ScanImage.rectTransform.sizeDelta.y * ratio_x;var t = new Texture2D((int)imageWidth, (int)imageHeight, TextureFormat.RGB24, false);//由于rect参数是从左下角开始读取,而我的m_ScanImage.transform锚点在左上角,所以rect的y值要减去他的高度t.ReadPixels(new Rect(screenPoint.x, screenPoint.y - imageHeight, imageWidth, imageHeight), 0, 0, false);t.Apply();var b = t.EncodeToPNG();if (b != null){File.WriteAllBytes(path, b);}

这里说明一下 3662这个值的来历,看一下Canvas的设置
在这里插入图片描述
我的设计分辨率是4K的 3840 * 2060, 这里选择使用高度适配,宽度做拉伸,根据我电脑显示器的分辨率和Match = 1的比例换算后得出实际的设计分辨率 3662 * 2060 大致是这个

	//适配比例float ratio_x = Screen.width / 3662f; //注意这里,一定要加上比例var imageWidth = m_ScanImage.rectTransform.sizeDelta.x * ratio_x;var imageHeight = m_ScanImage.rectTransform.sizeDelta.y * ratio_x;

这个比例加上以后,你的缩放操作就不会影响截图的范围了

好了,大致的要点就这么多,下面说下 坐标转换 ,这个经常用到

// 本地坐标转屏幕坐标, UiConfig.camera.WorldToScreenPoint(transform.position);  //注意这里使用position//屏幕坐标转本地坐标var b = RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, screenPoint, cam, localPoint);

今天讲的都是比较基础的,温故而知新,祝大家生活工作愉快~!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com