当前位置:网站首页>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);
}
}
边栏推荐
- Google Earth engine -- Statistics on the frequency of land classification year by year
- Learn NLP with Transformer (Chapter 8)
- C# Newtonsoft.Json 高级用法
- Learn NLP with Transformer (Chapter 6)
- 【flask高级】结合源码解决flask经典报错:Working outside of application context
- The most detailed MySQL index analysis (mind map is attached at the end of the article)
- 一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测
- Hcip experiment (03)
- 史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)
- Why should the hashcode () method be rewritten when rewriting the equals () method
猜你喜欢

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

MySQL | GROUP_CONCAT函数,将某一列的值用逗号拼接

Learn PHP -- phpstudy tips mysqld Exe: Error While Setting Value ‘NO_ ENGINE_ Solution of substitution error

The most detailed MySQL index analysis (mind map is attached at the end of the article)

HCIA experiment (07) comprehensive experiment

Some usages of beautifulsoup

Learn NLP with Transformer (Chapter 5)

HCIA experiment (08)

Reinforcement Learning 强化学习(四)

NowCoderTOP12-16——持续更新ing
随机推荐
只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体
[动态规划] 70. 爬楼梯
Nowcodertop1-6 - continuous updating
shell-第五章作业
Learn NLP with Transformer (Chapter 4)
游戏背包系统,“Inventory Pro插件”,研究学习-----妈妈再也不用担心我不会做背包了(Unity3D)
Google Earth engine -- Statistics on the frequency of land classification year by year
C# Newtonsoft. Jason advanced usage
UE4.26源码版学习广域网独立服务器时遇到的客户端运行黑屏问题
[servlet] request parsing
tensorflow 调用多块GPU的一些错误
From the perspective of open source, analyze the architecture design of SAP classic ERP that will not change in 30 years
The most detailed MySQL index analysis (mind map is attached at the end of the article)
【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
【高并发】如何实现亿级流量下的分布式限流?这些理论你必须掌握!!
How can you use unity without several plug-ins? Unity various plug-ins and tutorial recommendations
There is a newline problem when passing shell script parameters \r
爬虫基础一
Probe into Druid query timeout configuration → who is the querytimeout of datasource and jdbctemplate effective?
JS convert pseudo array to array