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

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

 

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

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

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

而且一次只能处理一个,一个就要好几分钟,总共好几十个,后期肯定会有好几百甚至上千个,真是要了命了

然后昨天睡觉前就想,为什么不自动处理呢,我们程序员不就应该懒一点吗

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

 

↓↓↓下面是正文↓↓↓

直接贴代码

using System;
using UnityEngine;
using System.IO;

public class CropPicture : MonoBehaviour
{
    public Camera cropCamera; //待截图的目标摄像机
    RenderTexture renderTexture;
    Texture2D texture2D;

    void Start()
    {
        renderTexture = new RenderTexture(800, 600, 32);
        texture2D = new Texture2D(800, 600, TextureFormat.ARGB32, false);
        cropCamera.targetTexture = renderTexture;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            RenderTexture.active = renderTexture;
            texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
            texture2D.Apply();
            RenderTexture.active = null;
            
            byte[] bytes = texture2D.EncodeToPNG();
            File.WriteAllBytes(Application.dataPath + "//pic//" + (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds + ".png", bytes);
        }
    }
}

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

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

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

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

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

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

 

posted @ 2017-06-11 21:37  静茹♂鱼  阅读(2258)  评论(0编辑  收藏  举报