Skip to content

Instantly share code, notes, and snippets.

@v21
Created December 24, 2013 11:36
Show Gist options
  • Save v21/8112074 to your computer and use it in GitHub Desktop.
Save v21/8112074 to your computer and use it in GitHub Desktop.
Unity3D editor script to run the game and extract a set number of images and save them to a file. Put this code into a file called MakeGif.cs inside a folder called "Editor"
using System;
using UnityEditor;
using UnityEngine;
using System.Collections;
public class MakeGif : EditorWindow {
public string folder = "ScreenshotFolder";
public int frameRate = 25;
public int frames;
public int currentFrame;
public bool recording = false;
[MenuItem("Window/RecordGif")]
static void MakeWindow()
{
var window = EditorWindow.GetWindow(typeof(MakeGif), false, "Make GIF");
}
void OnGUI()
{
frames = EditorGUILayout.IntField("Frames", frames);
frameRate = EditorGUILayout.IntField("Frame Rate", frameRate);
EditorGUILayout.LabelField("Duration", (frames / frameRate).ToString());
folder = EditorGUILayout.TextField("Folder", folder);
if (GUILayout.Button("Record"))
{
StartRecording();
}
}
void StartRecording()
{
// Set the playback framerate (real time will not relate to game time after this).
Time.captureFramerate = frameRate;
// Create the folder
System.IO.Directory.CreateDirectory(folder);
recording = true;
currentFrame = 0;
EditorApplication.isPlaying = true;
}
void Update()
{
if (recording)
{
// Append filename to folder name (format is '0005shot.png"')
var name = String.Format("{0}/{1:D04}shot.png", folder, Time.frameCount);
// Capture the screenshot to the specified file.
Application.CaptureScreenshot(name);
currentFrame++;
if (currentFrame > frames)
{
recording = false;
EditorApplication.isPlaying = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment