当前位置:网站首页>Unity 截屏小工具
Unity 截屏小工具
2022-07-26 22:43:00 【淡定九号】
Unity截屏小工具
前言
在unity日常开发中,有时候美工会遇到将3D场景保存为2D图片的功能,用普通的截图工具截出来的图片不是特别理想,所以需要程序员提供一个简易的截屏小工具
实现思路
1、根据主摄像机生成一个用于渲染的摄像机
2、新建一个RenderTexture,将摄像机渲染出来的画面指定给RenderTexture
3、将RenderTexture保存为图片
代码如下:
using System.IO;
using UnityEngine;
/// <summary>
/// 截屏小工具
/// </summary>
public class CaptureUtil : MonoBehaviour
{
private RenderTexture m_RenderTexture;
void Start()
{
//创建RenderTexture,保存的图片大小
m_RenderTexture = new RenderTexture(4096, 4096, 16);
m_RenderTexture.antiAliasing = 4;
//创建子摄像机
if (Camera.main)
{
GameObject newCam = Instantiate(Camera.main.gameObject);
newCam.name = "renderCamera";
newCam.transform.parent = Camera.main.transform;
newCam.transform.localPosition = Vector3.zero;
newCam.transform.localEulerAngles = Vector3.zero;
newCam.transform.localScale = Vector3.one;
//可根据自己需求修改摄像机配置
Camera camera = newCam.GetComponent<Camera>();
camera.targetTexture = m_RenderTexture;
camera.clearFlags = CameraClearFlags.SolidColor;
camera.backgroundColor = new Color(0, 0, 0, 0);
Destroy(newCam.GetComponent<CaptureUtil>());
Destroy(newCam.GetComponent<AudioListener>());
}
}
private void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 200, 200), "Capture"))
{
SaveRenderToPng(m_RenderTexture, "D:/Capture", "Capture");
}
}
/// <summary>
/// 将rendertexture保存为Png图片
/// </summary>
/// <param name="renderT"></param>
/// <param name="folderName"></param>
/// <param name="name"></param>
/// <returns></returns>
public static Texture2D SaveRenderToPng(RenderTexture renderT, string folderName, string name)
{
int width = renderT.width;
int height = renderT.height;
Texture2D tex2d = new Texture2D(width, height, TextureFormat.ARGB32, false);
RenderTexture.active = renderT;
tex2d.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex2d.Apply();
byte[] b = tex2d.EncodeToPNG();
if (!Directory.Exists(folderName))
Directory.CreateDirectory(folderName);
FileStream file = File.Open(folderName + "/" + name + GetTimeName() + ".png", FileMode.Create);
BinaryWriter writer = new BinaryWriter(file);
writer.Write(b);
file.Close();
return tex2d;
}
/// <summary>
/// 保存文件名称
/// </summary>
/// <returns></returns>
public static string GetTimeName()
{
return System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() +
System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() +
System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() +
System.DateTime.Now.Millisecond.ToString();
}
}
使用说明
1、美工设置好场景中的主摄像机镜头
2、将脚本拖动到主摄像机上,运行场景
3、点击左上角Capture按钮即可
4、可以在运行起来的时候,在主摄像机下,找到renderCamera(渲染摄像机),根据自己需求调整摄像机参数
边栏推荐
- Website log collection and analysis process
- 吴恩达深度学习系列教学视频学习笔记(一)——用于二分类的logistic回归函数
- 快来帮您一分钟了解移动互联网
- Li Hongyi machine learning (2017 Edition)_ P1-2: introduction to machine learning
- Verilog过程赋值语句
- Verilog的基本语法
- SQL learning (3) -- complex query and function operation of tables
- 基于485总线的评分系统
- 解决Pytorch中Cuda无法GPU加速问题
- 4. Root user login
猜你喜欢

Jenkins -- Basic -- 5.3 -- system configuration -- global security configuration

做题笔记1

快来:鼓励高校毕业生返乡创业就业,助力乡村振兴

李宏毅机器学习(2017版)_P5:误差

Play guest cloud brush machine 5.9

复杂度OJ题

Li Hongyi machine learning (2017 Edition)_ P6-8: gradient descent

李宏毅机器学习(2017版)_P3-4:回归

VIS工作流 - PlantCV

Six ways for the Internet of things to improve our lives
随机推荐
SQL learning (2) -- basic query and sorting of tables
解决Pytorch中Cuda无法GPU加速问题
PlantCV中文文档
最大公约数的求法
The MySQL character set is set to UTF-8, but the console still has the problem of Chinese garbled code
MySQL关闭连接事务自动提交的问题
Keil开发环境的搭建送安装包
数据库期中(一)
4. European Champions League
Play guest cloud with zerotier nanny level teaching to ensure learning waste
数据库期中(二)
The setup of KEIL development environment is delivered to the installation package
IDEA导入外部项目时pom文件的依赖无效问题解决
Verilog过程赋值语句
7. Formula F1 champion
The dependency of POM file is invalid when idea imports external projects. Solution
SQL学习(2)——表的基础查询与排序
Surrounded area
Deep understanding of pod objects: basic management
Jenkins--基础--02--安装
