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

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

相关文章:

  • 12芯束装光纤不同包层线颜色之间的排列顺序
  • Vue3后代传祖先组件通讯方法
  • 04.Spring 框架注解体系详解
  • L2-006 树的遍历
  • Logisim数字逻辑实训——寄存器设计与应用
  • 【datawhaleAI春训营第一期笔记】AI+航空安全
  • openbmb/MiniCPM-V-2_6 和 AIDC-AI/Ovis2-1B 的网络结构体对比
  • 专著出版能为评职助力吗?
  • IPTV电视信息发布直播点播系统:营造数字化个性化融合化多媒体IPTV电视信息发布平台
  • 数据结构学习笔记 :二叉搜索树与高效查找算法详解
  • 认知觉醒是什么? 如何做到 ? ( 持续更新ing )
  • FairMOT算法详解
  • 《软件设计师》复习笔记(12.3)——质量管理、风险管理
  • 《数据牢笼》-来自DeepSeek
  • iPaaS应用集成平台在交通运输行业有哪些应用场景
  • 第一期第16讲,17讲21:50
  • 淘宝商品搜索爬虫:Python 实现教程
  • Animated Raindrop Ripples In HLSL
  • Ext系列⽂件系统
  • C语言---FILE结构体
  • 海南开展药品安全“清源”行动,严查非法渠道购药等违法行为
  • 白宫慌了!将设工作组紧急处理对中国加征关税危机
  • 坚定信心主动应变局谋发展,上海市领导走访调研外资外贸企业
  • 多米尼加俱乐部屋顶坍塌事故死亡人数升至232人
  • 习近平同柬埔寨首相洪玛奈举行会谈
  • 上海加大对中小微企业信贷支持力度,奖补资金规模提升到5亿元