実装
using UnityEditor;
using UnityEngine;
using System;
using System.IO;
/// <summary>
/// スクリーンショットウィンドウ
/// </summary>
public class ScreenShotWindow : EditorWindow
{
private string _filePath = "Assets/ScreenShot/";
//スクリーンショットボタンを表示する
[MenuItem("Editor/ScreenShotWindow")]
static void WindowOpen()
{
var window = EditorWindow.GetWindow<ScreenShotWindow>();
}
//エディタウィンドウの見た目を表示
private void OnGUI()
{
ButtonDisp();
}
//撮影ボタンを表示する
private void ButtonDisp()
{
if (GUILayout.Button("ScreenShot"))
{
ScreenShot();
}
}
//スクリーンショットを行う
private void ScreenShot()
{
string fileName = _filePath + DateTime.Now.ToString("yy-MM-dd-HH-mm-ss") + ".png";
ScreenCapture.CaptureScreenshot(fileName);
File.Exists(fileName);
}
}
今回は、ゲーム再生時、エディタ編集時のどちらでもゲーム画面を撮影できるようにEditorWindowを使って実装しました。
ScreenCapture.CaptureScreenshot関数
ScreenCapture-CaptureScreenshot - Unity スクリプトリファレンス
PNG ファイルとして filename のパスでスクリーンショットを撮ります。
この関数では、ファイル名を指定することで、ゲームシーン画面のスクリーンショットを保存することが出来ます。