将游戏画面设置为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();
}
}
}
[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];
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());
}
}
|