unity3d 截图制作视频代码

1 篇文章 0 订阅
1 篇文章 0 订阅

先来效果
这里写图片描述

直接上代码

#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.IO;
using System;
public class AnimationToPNG : MonoBehaviour
{

    public string animationName = "";
    public string folder = "PNG_Animations";
    public string folderPath = "";
    public int frameRate = 30;
    public int framesToCapture = 90;
    private Camera whiteCam;
    private Camera blackCam;
    public float fieldOfView = 20;
    private bool useRenderTexture = false;
    public int videoframe = 0;
    private float originaltimescaleTime;
    private string realFolder = "";
    private bool done = false;
    private bool readyToCapture = false;
    private Texture2D texb;

    private Texture2D texw;

    private Texture2D outputtex;

    private RenderTexture blackCamRenderTexture; // black camera render texure

    private RenderTexture whiteCamRenderTexture; // white camera render texure

    public void OnStart()
    {
        useRenderTexture = Application.HasProLicense();

        Time.captureFramerate = frameRate;

        realFolder = folder + "/" + folderPath;
        int count = 1;
        while (Directory.Exists(realFolder))
        {
            realFolder = realFolder + count;
            count++;
        }
        Directory.CreateDirectory(realFolder);

        originaltimescaleTime = Time.timeScale;
        if (blackCam == null)
        {
            GameObject bc = new GameObject("Black Camera");
            bc.transform.parent = gameObject.transform;
            bc.transform.localPosition = Vector3.zero;
            bc.transform.localScale = Vector3.one;
            bc.transform.localRotation = new Quaternion(0, 0, 0, 0);

            blackCam = bc.AddComponent<Camera>();
            blackCam.backgroundColor = Color.black;
            blackCam.fieldOfView = fieldOfView;
            blackCam.tag = "MainCamera";
            blackCam.cullingMask = ~(1 << LayerMask.NameToLayer("Default"));
        }
        if (whiteCam == null)
        {
            GameObject wc = new GameObject("White Camera");
            wc.transform.parent = gameObject.transform;
            wc.transform.localPosition = Vector3.zero;
            wc.transform.localScale = Vector3.one;
            wc.transform.localRotation = new Quaternion(0, 0, 0, 0);


            whiteCam = wc.AddComponent<Camera>();
            whiteCam.backgroundColor = Color.white;
            whiteCam.fieldOfView = fieldOfView;
            whiteCam.cullingMask = ~(1 << LayerMask.NameToLayer("Default"));


            if (!useRenderTexture)
            {
                blackCam.rect = new Rect(0.0f, 0.0f, 0.5f, 1.0f);
                whiteCam.rect = new Rect(0.5f, 0.0f, 0.5f, 1.0f);
            }
            readyToCapture = true;
        }
    }

    void Update()
    {
        if (!done && readyToCapture)
        {
            StartCoroutine(Capture());
        }
    }

    void LateUpdate()
    {
        if (done)
        {
            DestroyImmediate(texb);
            DestroyImmediate(texw);
            DestroyImmediate(outputtex);

            if (useRenderTexture)
            {
                //Clean Up
                whiteCam.targetTexture = null;
                RenderTexture.active = null;
                DestroyImmediate(whiteCamRenderTexture);

                blackCam.targetTexture = null;
                RenderTexture.active = null;
                DestroyImmediate(blackCamRenderTexture);
            }
        }
    }

    IEnumerator Capture()
    {
        if (videoframe < framesToCapture)
        {
            animationName = videoframe + "_";
            string filename = String.Format("{0}/" + animationName + "{1:D04}.png", realFolder, Time.frameCount);

            Time.timeScale = 0;
            yield return new WaitForEndOfFrame();

            if (useRenderTexture)
            {
                //Initialize and render textures
                blackCamRenderTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
                whiteCamRenderTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);

                blackCam.targetTexture = blackCamRenderTexture;
                blackCam.Render();
                RenderTexture.active = blackCamRenderTexture;
                texb = GetTex2D(true);

                //Now do it for Alpha Camera
                whiteCam.targetTexture = whiteCamRenderTexture;
                whiteCam.Render();
                RenderTexture.active = whiteCamRenderTexture;
                texw = GetTex2D(true);
            }

            else
            {
                texb = GetTex2D(true);
                texw = GetTex2D(false);
            }

            if (texw && texb)
            {

                int width = Screen.width;
                int height = Screen.height;
                if (!useRenderTexture)
                {
                    width = width / 2;
                }
                outputtex = new Texture2D(width, height, TextureFormat.ARGB32, false);

                for (int y = 0; y < outputtex.height; ++y)
                { // each row
                    for (int x = 0; x < outputtex.width; ++x)
                    { // each column
                        float alpha;
                        if (useRenderTexture)
                        {
                            alpha = texw.GetPixel(x, y).r - texb.GetPixel(x, y).r;
                        }
                        else
                        {
                            alpha = texb.GetPixel(x + width, y).r - texb.GetPixel(x, y).r;
                        }
                        alpha = 1.0f - alpha;
                        Color color;
                        if (alpha == 0)
                        {
                            color = Color.clear;
                        }
                        else
                        {
                            color = texb.GetPixel(x, y) / alpha;
                        }
                        color.a = alpha;
                        outputtex.SetPixel(x, y, color);
                    }
                }

                // Encode the resulting output texture to a byte array then write to the file
                byte[] pngShot = outputtex.EncodeToPNG();
                File.WriteAllBytes(filename, pngShot);

                // Reset the time scale, then move on to the next frame.
                Time.timeScale = originaltimescaleTime;
                videoframe++;
            }

            // Debug.Log("Frame " + name + " " + videoframe);
        }
        else
        {
            Debug.Log("完成");
            done = true;
            GameObject.DestroyImmediate(whiteCam.gameObject);
            GameObject.DestroyImmediate(blackCam.gameObject);
            GameObject.DestroyImmediate(this);
        }
    }

    private Texture2D GetTex2D(bool renderAll)
    {
        int width = Screen.width;
        int height = Screen.height;
        if (!renderAll)
        {
            width = width / 2;
        }

        Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        tex.Apply();
        return tex;
    }
}
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值