栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

unity录制序列帧

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 allFrame;
    public int Size = 64;

    [Button("cap")]

    public void StartCap()
    {
        IE_Cap();
    }

    public async void IE_Cap()
    {
        renderer.gameObject.SetActive(false);
        allFrame = new List(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());
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900936.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号