当前位置:网站首页>Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
2022-07-25 11:23:00 【InfoQ】
- CSDN Home page
- GitHub Open source address
- Unity3D Plug in sharing
- Jane book address
- My personal blog
- QQ Group :1040082875
One 、 Preface
Two 、 effect


3、 ... and 、 Code
using UnityEngine;
public class Screenshot : MonoBehaviour
{
// Screenshot camera
Camera capCamera;
// Save the picture
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);
// Create a RenderTexture object
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// Temporarily set the of the relevant camera targetTexture by rt, And manually render the relevant camera
capCamera.targetTexture = rt;
capCamera.Render();
// Activate this rt, And read pixels from it .
RenderTexture.active = rt;
screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
// from RenderTexture.active Read the picture in the middle school
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
// Reset relevant parameters , To use the camera Continue to display... On the screen
capCamera.targetTexture = null;
// Avoid mistakes of repeated addition
RenderTexture.active = null;
// Destroy this object
Destroy(rt);
// Save the picture
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.streamingAssetsPath + "/2.png";
System.IO.File.WriteAllBytes(filename, bytes);
}
}
边栏推荐
- 倍增Floyd「建议收藏」
- mysql主从复制与读写分离
- MySQL master-slave replication and read-write separation
- Nowcodertop1-6 - continuous updating
- PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying
- BeautifulSoup的一些用法
- Flask framework - Message flash
- shell-第四天作业
- 学习路之PHP--Phpstudy 提示 Mysqld.Exe: Error While Setting Value ‘NO_ENGINE_SUBSTITUTION 错误的解决办法
- Review recitation finishing version
猜你喜欢

只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体

HCIP(13)

机智云物联网平台 STM32 ESP8266-01S 简单无线控灯

Review recitation finishing version

Flask framework - session and cookies

最详细的mysql索引解析(文末附赠思维导图)

Mlx90640 infrared thermal imager temperature measurement module development notes (V)

HCIA experiment (10) nat

Ue4.26 source code version black screen problem of client operation when learning Wan independent server

B2B2C多商户系统功能丰富,极易二开!!!
随机推荐
倍增Floyd「建议收藏」
syncronized锁升级的过程
SQL语言(五)
LVS load balancing lvs-dr builds Web Clusters and LVS combines with kept to build highly available Web Clusters
[flask advanced] deeply understand the application context and request context of flask from the source code
I, AI doctoral student, online crowdfunding research topic
【高并发】如何实现亿级流量下的分布式限流?这些理论你必须掌握!!
From the perspective of open source, analyze the architecture design of SAP classic ERP that will not change in 30 years
学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
What is MySQL transaction
【flask高级】从源码深入理解flask的应用上下文和请求上下文
[high concurrency] deeply analyze the execution process of worker threads in the thread pool through the source code
[递归] 938. 二叉搜索树的范围和
shell-第六章练习
用Unity不会几个插件怎么能行?Unity各类插件及教程推荐
Learn NLP with Transformer (Chapter 6)
Redis之压缩列表ziplist
HCIA experiment (07) comprehensive experiment
API supplement of JDBC
从宏观到微观 零基础 详解bert