Skip to content

Instantly share code, notes, and snippets.

@makochang
Last active August 29, 2015 14:13
Show Gist options
  • Save makochang/fc4a414be9507e9a2e42 to your computer and use it in GitHub Desktop.
Save makochang/fc4a414be9507e9a2e42 to your computer and use it in GitHub Desktop.
Unityでスクリーンショットを保存する
using UnityEngine;
using System.Collections;
public class ScreenCapture : MonoBehaviour {
//キャプチャーの開始・終了.
private bool rec = false;
//照合キー.
private char checkKey;
//日付.
private string date;
//連番.
private int seqNum = 0;
//ファイルパス.
private string filePath;
//キャプチャーモード(1枚だけ/連番).
public enum capMode {
ONE_SHOT,
SEQUENCE
}
public capMode captureMode = capMode.SEQUENCE;
//ファイル拡張子.
private string extension = ".png";
//ファイル名.
public string fileName = "Screenshot";
//ショートカットキー.
public string recKey = "f";
//フレームレート.
public int fps = 30;
void ExitMessage () {
Debug.Log ("キャプチャー終了 " + Application.dataPath + " に保存しました");
}
void StartCapturing () {
filePath = fileName + "_" + date + "_" + seqNum.ToString ("0000") + extension;
Application.CaptureScreenshot (filePath);
if (captureMode == capMode.ONE_SHOT) {
rec = false;
ExitMessage ();
}
seqNum++;
}
// Use this for initialization
void Start () {
date = System.DateTime.Now.ToString ("yyyyMMdd");
if (recKey.Length < 1) {
Debug.LogWarning ("ショートカットキーが割り当てられていません");
} else {
checkKey = recKey [0];
}
Time.captureFramerate = fps;
}
// Update is called once per frame
void Update () {
//キーボド入力を取得してショートカットと比較する.
foreach (char c in Input.inputString) {
if (c == checkKey) {
rec = !rec;
if (rec) {
Debug.Log ("キャプチャー開始");
} else {
ExitMessage ();
}
}
}
if (rec) {
StartCapturing ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment