Unityな日々(Unity Geek)

Unityで可視化アプリを開発するための試行錯誤の覚書

Unityから連番画像を書き出す

画面のスクリーンショットはApplication.CaptureScreenshot(ファイル名)で取得する。
下の例は、スクリーンショットをループ(Update等)に入れて、動画を連番ファイルで書き出すサンプル。(出典:ScreenShotMovie - Unify Community Wiki

using UnityEngine;
 
public class ScreenShot : MonoBehaviour
{
	// The folder we place all screenshots inside.
	// If the folder exists we will append numbers to create an empty folder.
	public string folder = "ScreenshotFolder";
	public int frameRate = 25;
 
	private string realFolder = "";
 
	void Start()
	{
		// Set the playback framerate!
		// (real time doesn't influence time anymore)
		Time.captureFramerate = frameRate;
		// Find a folder that doesn't exist yet by appending numbers!
		realFolder = folder;
		int count = 1;
		while (System.IO.Directory.Exists(realFolder))
		{
			realFolder = folder + count;
			count++;
		}
		// Create the folder
		System.IO.Directory.CreateDirectory(realFolder);
	}
 
	void Update()
	{
		// name is "realFolder/0005 shot.png"
		var name = string.Format("{0}/{1:D04} shot.png", realFolder, Time.frameCount);
		// Capture the screenshot
		Application.CaptureScreenshot(name);
	}
}

画像書き出しの負荷はかなり高く、Updateのインターバルはかなり長くなる。
後、細かいTipsだが、テキストフォーマットで{:D04}とすると、4桁の数字としてString化できる。