スマゲ

スマートなゲームづくりを目指して日々精進

UnityEditorでゲームシーンのスクリーンショットを撮るEditor拡張

UnityEditorでGameシーンのスクリーンショットを撮るためのEditor拡張を作成しました。

github.com

■使い方

f:id:sanukin39:20161116235526p:plain

■動作要件
・Unity4以上
・Mac, Windows

■導入
・GitHubリポジトリのunitypackageフォルダ以下のUniEditorScreenshot.unitypackageを利用

■機能
・スクリーンショットを撮る
・出力フォルダを選択
・出力フォルダを開く

f:id:sanukin39:20161117000021p:plain

■実装
以下のコードをAssets/Editor以下に置く

using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Collections;

public class CaptureWindow : EditorWindow{

    private string saveFileName = string.Empty;
    private string saveDirPath = string.Empty;

    [MenuItem("Window/Capture Editor")]
    private static void Capture() {
        EditorWindow.GetWindow (typeof(CaptureWindow)).Show ();
    }

    void OnGUI() {
        EditorGUILayout.LabelField ("OUTPUT FOLDER PATH:");
        EditorGUILayout.LabelField (saveDirPath + "/");

        if (string.IsNullOrEmpty (saveDirPath)) {
            saveDirPath = Application.dataPath;
        }

        if (GUILayout.Button("Select output directory")) {
            string path = EditorUtility.OpenFolderPanel("select directory", saveDirPath, Application.dataPath);
            if (!string.IsNullOrEmpty(path)) {
                saveDirPath = path;
            }
        }

        if (GUILayout.Button("Open output directory")) {
            System.Diagnostics.Process.Start (saveDirPath);
        }

        // insert blank line
        GUILayout.Label ("");

        if (GUILayout.Button("Take screenshot")) {
            var outputPath = saveDirPath + "/" + DateTime.Now.ToString ("yyyyMMddHHmmss") + ".png";
            Application.CaptureScreenshot (outputPath);
            Debug.Log ("Export scrennshot at " + outputPath);
        }
    }
}

■参考
Unity - スクリプトリファレンス: Application.CaptureScreenshot
Unityエディタ上からGameビューのスクリーンショットを撮るEditor拡張 - Qiita