【Unity3D】Unity3D 摄像机带透明截图

时间:2023-03-09 06:56:28
【Unity3D】Unity3D 摄像机带透明截图

转载请注明出处:http://www.cnblogs.com/shamoyuu/p/CropCamera.html

↓↓↓下面的废话可以不看↓↓↓

最近处理了一批我的游戏的图标,步骤特别繁琐,

需要先摆好位置,截图,然后PS处理透明,然后合成到宫格图里,

而且一次只能处理一个,一个就要好几分钟,总共好几十个,后期肯定会有好几百甚至上千个,真是要了命了【Unity3D】Unity3D 摄像机带透明截图

然后昨天睡觉前就想,为什么不自动处理呢,我们程序员不就应该懒一点吗【Unity3D】Unity3D 摄像机带透明截图

然后就打算写一个,其他的步骤都比较简单,所以这里只放出透明截图的做法。

↓↓↓下面是正文↓↓↓

直接贴代码

using System;
using UnityEngine;
using System.IO; public class CropPicture : MonoBehaviour
{
public Camera cropCamera; //待截图的目标摄像机
RenderTexture renderTexture;
Texture2D texture2D; void Start()
{
renderTexture = new RenderTexture(, , );
texture2D = new Texture2D(, , TextureFormat.ARGB32, false);
cropCamera.targetTexture = renderTexture;
} void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(, , renderTexture.width, renderTexture.height), , );
texture2D.Apply();
RenderTexture.active = null; byte[] bytes = texture2D.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "//pic//" + (DateTime.UtcNow - new DateTime(, , , , , , )).TotalMilliseconds + ".png", bytes);
}
}
}

然后把这个脚本拖到主摄像机上

新建一个需要截图的摄像机,为什么需要新建呢?因为它不能有天空盒。

然后把这个摄像机物体拖到主摄像机CropPicture脚本上的CropCamera变量上

然后设置这个需要截图的摄像机的属性如下

【Unity3D】Unity3D 摄像机带透明截图

颜色这里只要A是0就可以了,其他3个随意

在工程目录下新建一个pic文件夹,然后运行,按空格就截图了

【Unity3D】Unity3D 摄像机带透明截图