当前位置: 首页 > news >正文

【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);

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

http://www.lryc.cn/news/465044.html

相关文章:

  • 句句深刻,字字经典,创客匠人老蒋金句出炉,哪一句让你醍醐灌顶?
  • 柯尼卡美能达CA-310 FPD色彩分析仪
  • 二维EKF的MATLAB代码
  • 大数据治理:数据时代的挑战与应对
  • 绿联NAS免驱安装MacOS
  • 聊聊ASSERT处理在某些场景下的合理用法
  • SAP Odata 服务
  • 【java数据结构】栈
  • 从头开始的可视化数据 matplotlib:初学者努力绘制数据图
  • vscode 远程linux服务器 连接git
  • 不同jdk版本中的接口规范
  • 人工智能图像信号处理器(AI ISP)技术介绍
  • 3D Slicer 教程三 ---- 坐标系
  • Video-LLaMA论文解读和项目部署教程
  • Elasticsearch设置 X-Pack认证,设置账号和密码
  • 机器学习——量子机器学习(Quantum Machine Learning)
  • Android Studio 的 Gradle 任务列表只显示测试任务
  • Keepalived:高可用性的守护神
  • Golang笔记_day08
  • 如何在 React 中更新状态对象的某个值
  • edge浏览器:你的连接不是专用连接
  • PDF 软件如何帮助您编辑、转换和保护文件
  • 如何使用Java爬虫处理API接口返回的JSON数据?
  • Ajax是什么?
  • 技术方向简介
  • 延迟队列实现及其原理详解
  • web APIs
  • 【Web前端概述】
  • 文献阅读:一种基于艾伦脑图谱的空间表达数据可视化、空间异质性描绘和单细胞配准工具
  • Redis学习笔记(三)--Redis客户端