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

devtools/2024/10/19 12:46:10/

在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.ppmy.cn/devtools/127010.html

相关文章

Scala入门基础(12)抽象类

抽象类&#xff0c;制定标准&#xff0c;不要求去具体实现 包含了抽象方法的类就是抽象类。抽象方法只是有方法名&#xff0c;没有具体方法体的方法 定义抽象类要用abstract&#xff08;抽象&#xff09;关键字 用智能驾驶技术举例&#xff1a;演示&#xff09…

Win11 安装 PostgreSQL 报错解决方案

一、问题概述 在 Win11 系统中安装 PostgreSQL 时&#xff0c;可能会遇到“Problem running post-install”的报错情况。这一报错给用户带来了极大的困扰&#xff0c;使得安装过程无法顺利进行。 二、报错原因分析 &#xff08;一&#xff09;权限不足问题 在 Win11 中&…

sankey.top - 桑基图/桑吉图/流程图/能量流/物料流/能量分析

sankey.top 桑基图大师(SankeyMaster)是您创建复杂桑基图表的首选工具。轻松输入数据并创建桑基图表&#xff0c;准确揭示复杂的数据关系&#xff01; 应用 https://apps.apple.com/cn/app/sankeymaster-sankey-diagram/id6474908221 在线编辑器 https://studio.sankeymaste…

Spring Security 如何进行权限验证

阅读本文之前&#xff0c;请投票支持这款 全新设计的脚手架 &#xff0c;让 Java 再次伟大&#xff01; FilterSecurityInterceptor FilterSecurityInterceptor 是负责权限验证的过滤器。一般来说&#xff0c;权限验证是一系列业务逻辑处理完成以后&#xff0c;最后需要解决的…

python 区间循环数

目录 区间循环加上步长2&#xff1a; 区间循环加上步长1&#xff1a; 方法3&#xff1a; 区间循环加上步长2&#xff1a; for i in range(0, 11, 2):print(i) 区间循环加上步长1&#xff1a; for i in range(start, stop):# 你的代码逻辑pass 方法3&#xff1a; if __nam…

Redis——事务

文章目录 Redis 事务Redis 的事务和 MySQL 事务的区别:事务操作MULTIEXECDISCARDWATCHUNWATCHwatch的实现原理 总结 Redis 事务 什么是事务 Redis 的事务和 MySQL 的事务 概念上是类似的. 都是把⼀系列操作绑定成⼀组. 让这⼀组能够批量执行 Redis 的事务和 MySQL 事务的区别:…

Spring Boot 3 声明式接口:能否完全替代 OpenFeign?

Spring Boot 3 声明式接口&#xff1a;能否完全替代 OpenFeign&#xff1f; 在微服务架构中&#xff0c;服务间的通信是一个核心问题。OpenFeign&#xff0c;作为一个声明式的HTTP客户端&#xff0c;极大地简化了服务调用和负载均衡的实现。然而&#xff0c;随着Spring Boot 3…

通过前端UI界面创建VUE项目

通过前端UI界面创建VUE项目&#xff0c;是比较方面的一种方式&#xff0c;下面我们详细分析一下流程&#xff1a; 1、找到合适目录 右键鼠标&#xff0c;点击在终端打开 2、开始创建 输入 vue ui 浏览器弹出页面 3、点击Create项目 显示已有文件列表&#xff0c;另外可以点击…