【Unity】スクショとるエディタスクリプト

スクショとるエディタスクリプト

プロジェクトのルートディレクトリの直下(Assetsとかと同じ階層)に「ScreenShot」フォルダが作られ、そこに配置される。

using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Runtime.CompilerServices;

public class ScreenShotCapturer : MonoBehaviour
{
    private const string Path = "ScreenShot/";
    [MenuItem("MyTools/Caputure ScreenShot")]
    private static void Capture()
    {
        var assetPath = string.Format(System.IO.Path.Combine(Path, "ScreenShot_{0}.png"),
            DateTime.Now.ToString("yyyyMMddHHmmss"));
        SafeCreateDirectory(Path);
        ScreenCapture.CaptureScreenshot(string.Format(assetPath));
        Debug.Log($"スクリーンショットを保存しました: {(Application.dataPath).Replace("Assets", "") + assetPath}");
    }

    private static DirectoryInfo SafeCreateDirectory(string path)
    {
        var fullPath = (Application.dataPath).Replace("Assets", "") + path;
        return Directory.Exists(fullPath) ? null : Directory.CreateDirectory(fullPath);
    }
}

git ignore に追加を忘れないように

git管理しているプロジェクトなら、「ScreenShot」フォルダはignoreしておこう:)