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

Unity-微信截图功能简单复刻-02屏幕采样

思路

采样相机画面,更新到纹理,使用RawImage组件显示纹理

示例脚本

using UnityEngine;
using UnityEngine.UI;
public class TestScreenSample : MonoBehaviour
{[SerializeField] RawImage contentShow;//采样内容显示到图片组件上[SerializeField] Camera drawCamera;//采样相机Texture2D texture;//纹理private void Awake(){//创建一个纹理texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);texture.filterMode = FilterMode.Point;texture.wrapMode = TextureWrapMode.Clamp;contentShow.texture = texture;//使用空白纹理信息填充byte[] blankTextureData = new byte[Screen.width * Screen.height * 4];texture.LoadRawTextureData(blankTextureData);texture.Apply(false);}private void Update(){//按下空格键进行屏幕采样 更新纹理if (Input.GetKeyDown(KeyCode.Space))Sampling(ref texture, drawCamera, texture.width, texture.height);}void Sampling(ref Texture2D texture, Camera drawCamera, int width, int height){//创建临时RenderTextureRenderTexture RT = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, 1);//相机画面渲染到RenderTexturevar targetTexture = drawCamera.targetTexture;drawCamera.targetTexture = RT;drawCamera.Render();drawCamera.targetTexture = targetTexture;//读取RenderTexture的像素数据到texturevar activeTexture = RenderTexture.active;RenderTexture.active = RT;texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);texture.Apply(false);RenderTexture.active = activeTexture;RenderTexture.ReleaseTemporary(RT);}
}

场景结构

在这里插入图片描述
Canvas渲染模式为Overlay,
TestScreenSample对象添加测试脚本,设置引用对象。

运行画面

在这里插入图片描述
运行,按下空格键,左上角显示采样内容。
移动立方体,按下空格键,采样内容变化。

技术组合

Texture2D
Camera
RenderTexture

http://www.xdnf.cn/news/181.html

相关文章:

  • 青少年编程与数学 02-016 Python数据结构与算法 28课题、图像处理算法
  • 深入理解红黑树:原理、实现与应用
  • openbmb/MiniCPM-V-2_6 和 AIDC-AI/Ovis2-1B 的网络结构体对比
  • 日本公司如何实现B2B商城订货系统的自动化和个性化?
  • 电子电器架构 --- EOL 工厂刷写(产线)
  • Debian服务器环境下env变量丢失怎么办
  • 万物互联时代,AWS IoT Core如何构建企业级物联网中枢平台?
  • UML 状态图:以网络媒体教学系统为例解析
  • Python项目--基于机器学习的股票预测分析系统
  • 路由交换网络专题 | 第五章 | ISIS | RIP | 路由引入 | 策略路由
  • 探索大语言模型(LLM):循环神经网络的深度解析与实战(RNN、LSTM 与 GRU)
  • 工业触摸显示器助力智慧工业实验室发展
  • Privacy Risks of General-Purpose Language Models
  • 【第48节】探究汇编使用特性:从基础到混合编程
  • Linux 日常运维命令大全
  • 高级java每日一道面试题-2025年4月14日-微服务篇[Nacos篇]-Nacos是如何实现对多数据中心的支持的?
  • 算法思想之链表
  • 探索C++中的数据结构:栈(Stack)的奥秘
  • Oceanbase单机版上手示例
  • 拉取windows的docker镜像转到服务器上构建服务镜像
  • 探索Spring Boot Web模块:设计思想与技术实现
  • Polkadot:引领区块链大规模应用的核心力量
  • 纯CSS实现自动滚动到底部
  • Redis面试——缓存淘汰和过期删除
  • CSS伪元素
  • Python 之 Faker 测试数据生成
  • uniapp中uni-easyinput 使用@input 不改变绑定的值
  • Linux中的管道
  • EDID结构
  • zookeeper启动报错have small server identifier