当前位置:网站首页>玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用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);
}
}
边栏推荐
猜你喜欢

机智云物联网平台 STM32 ESP8266-01S 简单无线控灯
学习周刊 - 总第 63 期 - 一款开源的本地代码片段管理工具

用Unity不会几个插件怎么能行?Unity各类插件及教程推荐

Learn NLP with Transformer (Chapter 1)

Flask框架——flask-caching缓存

Flask框架——Flask-WTF表单:文件上传、验证码

What is MySQL transaction

redis 哨兵,高可用的执行者

游戏背包系统,“Inventory Pro插件”,研究学习-----妈妈再也不用担心我不会做背包了(Unity3D)

Learn NLP with Transformer (Chapter 6)
随机推荐
SQL语言(六)
机智云物联网平台 STM32 ESP8266-01S 简单无线控灯
Reinforcement learning (III)
【flask高级】结合源码解决flask经典报错:Working outside of application context
Flask framework - session and cookies
SQL语言(四)
Learn NLP with Transformer (Chapter 5)
Learn NLP with Transformer (Chapter 8)
NowCoderTOP7-11——持续更新ing
Visual thematic map of American airport go style: ArcGIS Pro version
【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
2021 CEC written examination summary
[flask advanced] combined with the source code, explain the operation mechanism of flask (in and out of the stack)
[high concurrency] how to realize distributed flow restriction under 100 million level traffic? You must master these theories!!
[cloud enjoys freshness] community weekly · Vol 72 - the first opening ceremony of the 2022 Huawei developer competition in China was launched; Huawei cloud koomessage is in hot public beta
The integration of two in one has a long way to go
[递归] 938. 二叉搜索树的范围和
C3d model pytorch source code sentence by sentence analysis (III)
MLX90640 红外热成像仪测温模块开发笔记(五)
B2B2C多商户系统功能丰富,极易二开!!!