Skip to content

Instantly share code, notes, and snippets.

@keijiro
Last active January 26, 2022 05:01
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save keijiro/7429201 to your computer and use it in GitHub Desktop.
Save keijiro/7429201 to your computer and use it in GitHub Desktop.
Screen recording utility.
using UnityEngine;
using System.Collections;
public class ScreenRecorder : MonoBehaviour
{
public int framerate = 30;
public int superSize;
public bool autoRecord;
int frameCount;
bool recording;
void Start ()
{
if (autoRecord) StartRecording ();
}
void StartRecording ()
{
System.IO.Directory.CreateDirectory ("Capture");
Time.captureFramerate = framerate;
frameCount = -1;
recording = true;
}
void Update ()
{
if (recording)
{
if (Input.GetMouseButtonDown (0))
{
recording = false;
enabled = false;
}
else
{
if (frameCount > 0)
{
var name = "Capture/frame" + frameCount.ToString ("0000") + ".png";
Application.CaptureScreenshot (name, superSize);
}
frameCount++;
if (frameCount > 0 && frameCount % 60 == 0)
{
Debug.Log ((frameCount / 60).ToString() + " seconds elapsed.");
}
}
}
}
void OnGUI ()
{
if (!recording && GUI.Button (new Rect (0, 0, 200, 50), "Start Recording"))
{
StartRecording ();
Debug.Log ("Click Game View to stop recording.");
}
}
}
@KensukeSakakibara
Copy link

Application.CaptureScreenshot is already removed since unity 2017.
To use ScreenCapture.CaptureScreenshot is good.
https://gist.github.com/KensukeSakakibara/6987fd6a7c707db7a0596f703276f3a4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment