当前位置: 首页 > 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.mrgr.cn/news/50794.html

相关文章:

  • 人工智能之动物识别专家系统
  • vue使用jquery的ajax,页面跳转
  • 【Java 并发编程】单例模式
  • 鸿蒙开发(NEXT/API 12)【发送数据到服务器】远场通信场景
  • ai-scientist部署和使用
  • 用于病理图像诊断的跨尺度多实例学习|文献速递-基于深度学习的医学影像分类,分割与多模态应用
  • vue3.0 + vue-i18n:使用方法和自动引入多个语言文件
  • Vulhub DerpNStink: 1靶机详解
  • MySQL索引、事物与存储引擎
  • (二)Python输入输出函数
  • MATLAB(Octave)混电动力能耗评估
  • 2024年四非边缘鼠鼠计算机保研回忆(记录版 碎碎念)
  • Redis技术指南:数据类型、事务处理与过期键管理
  • VRP_用MDP建模_20241015
  • [Linux] 创建可以免密登录的SFTP用户
  • Pyenv 介绍和安装指南 - Ubuntu 24
  • opencv学习:人脸识别器特征提取BPHFaceRecognizer_create算法的使用
  • unity学习-Directional light光的设置
  • 爬虫之数据解析
  • 详解腐烂的苹果(图+代码+广度优先遍历)