当前位置:   article > 正文

unity录制序列帧_unity 录制序列帧

unity 录制序列帧

将游戏画面设置为128*128 调整相机设置 size 为刚好能完全开到粒子的播放。

cap 录制
play 在一个spriteRender上播放刚录制的画面
save 将刚才的序列帧 合并到一张贴图上

using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class ScreenToPng : MonoBehaviour
{
    public GameObject Prefab;

    public SpriteRenderer renderer;

    public int FrameDelay;
    public int FrameCount;
    private string path = "/Thired/AsyncArtAssets/ParticleSystemToPng";

    public List<Texture2D> allFrame;
    public int Size = 64;

    [Button("cap")]

    public void StartCap()
    {
        IE_Cap();
    }

    public async void IE_Cap()
    {
        renderer.gameObject.SetActive(false);
        allFrame = new List<Texture2D>(FrameCount);
        GameObject.Instantiate(Prefab);
        await new WaitForEndOfFrame();
        for (int i = 0; i < FrameCount; i++)
        {
            Texture2D screenShot = ScreenCapture.CaptureScreenshotAsTexture();
            allFrame.Add(screenShot);

            for (int j = 0; j < FrameDelay; j++)
            {
                await new WaitForEndOfFrame();
            }
        }

        //string myFileName = Application.dataPath + "/myFileName.png";
        //File.WriteAllBytes(myFileName, bytes);
    }
    [Button("play")]
    public async void PlayFrame()
    {
        renderer.gameObject.SetActive(true);
        for (int i = 0; i < FrameCount; i++)
        {
            renderer.sprite = Sprite.Create(allFrame[i], new Rect(0, 0, Size, Size), new Vector2(0.5f, 0.5f));

            for (int j = 0; j < FrameDelay; j++)
            {
                await new WaitForEndOfFrame();
            }
        }
    }

    [Button("save")]
    public void SaveToPng(string name)
    {
        Texture2D nTex = new Texture2D(512, 512, TextureFormat.ARGB32, true);
        Color[] colors = new Color[nTex.width * nTex.height];//262144
        for (int i = 0; i < allFrame.Count; i++)
        {
            for (int j = 0; j < Size; j++)
            {
                int row = 3 - i / 4;
                int col = i % 4;
                for (int k = 0; k < Size; k++)
                {
                    int index = (row * 128 + j) * 512 + col * 128 + k;
                    colors[index] = allFrame[i].GetPixel(j, k);
                }
            }

        }
        nTex.SetPixels(colors);
        nTex.Apply();

        name = Prefab.name + name;
        string myFileName = Application.dataPath + path + string.Format("/{0}.png", name);
        File.WriteAllBytes(myFileName, nTex.EncodeToPNG());
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/138448
推荐阅读
相关标签
  

闽ICP备14008679号