当前位置:网站首页>玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
2022-07-25 10:29:00 【InfoQ】
- CSDN主页
- GitHub开源地址
- Unity3D插件分享
- 简书地址
- 我的个人博客
- QQ群:1040082875
一、前言
二、效果


三、代码
using UnityEngine;
public class Screenshot : MonoBehaviour
{
//截图相机
Camera capCamera;
//保存图片
Texture2D screenShot;
void Start()
{
capCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
}
void OnGUI()
{
if (GUILayout.Button("Show"))
{
CaptureCamera();
}
}
void CaptureCamera()
{
Rect rect = new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f);
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
capCamera.targetTexture = rt;
capCamera.Render();
// 激活这个rt, 并从中中读取像素。
RenderTexture.active = rt;
screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
//从RenderTexture.active中读取像素
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
// 重置相关参数,以使用camera继续在屏幕上显示
capCamera.targetTexture = null;
//避免重复添加的错误
RenderTexture.active = null;
//销毁这个对象
Destroy(rt);
//保存图片
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.streamingAssetsPath + "/2.png";
System.IO.File.WriteAllBytes(filename, bytes);
}
}
边栏推荐
- 上周热点回顾(7.18-7.24)
- AI系统前沿动态第43期:OneFlow v0.8.0正式发布;GPU发现人脑连接;AI博士生在线众筹研究主题
- [递归] 938. 二叉搜索树的范围和
- Guys, flick CDC table API, Mysql to MySQL, an application that can
- ArcMap无法启动解决方法
- Redis sentry, high availability executor
- Learn NLP with Transformer (Chapter 5)
- HCIP(12)
- B2B2C多商户系统功能丰富,极易二开!!!
- UE4 framework introduction
猜你喜欢
Learn NLP with Transformer (Chapter 4)

性能测试中TPS的计算【杭州多测师】【杭州多测师_王sir】

SQL语言(六)

HCIP (01)

SQL语言(一)

The most detailed MySQL index analysis (mind map is attached at the end of the article)
Learn NLP with Transformer (Chapter 2)

A troubleshooting record of DirectShow playback problems
信息熵的定义

Flask框架——flask-caching缓存
随机推荐
学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
企业实践开源的动机
Digital twin everything can be seen | connecting the real world and digital space
Learn NLP with Transformer (Chapter 5)
UE4 framework introduction
B2B2C多商户系统功能丰富,极易二开!!!
BGP联邦实验
Learn NLP with Transformer (Chapter 6)
Learn NLP with Transformer (Chapter 1)
How to notify users of wechat applet version update?
How to optimize the performance when the interface traffic increases suddenly?
【Servlet】请求的解析
三万字速通Servlet
HCIP(11)
HDD杭州站全程体验有感
HCIA experiment (06)
JS bidirectional linked list 02
JDBC的APi补充
Reinforcement learning (III)
Code representation learning: introduction to codebert and other related models