Cameraコンポーネントを拡張して非実行時でもゲームビューのスクリーンショットを撮れるようにします。
もちろん実行時でも撮れます。
Cameraコンポーネントから保存
後述するコードを追加すると"Export and Save"ボタンが表示されます。
"Export and Save"ボタンを押すことで指定した場所にpng画像が保存されます。
エクスポート結果例
コード
以下のコードををEditorフォルダに保存してください。
using System;
using UnityEngine;
using UnityEditor;
using System.IO;
[CustomEditor(typeof(Camera))]
public class CameraScreenshot : Editor
{
string exportFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\Unity_Screenshots";
public override void OnInspectorGUI()
{
EditorGUILayout.BeginVertical(GUI.skin.box);
{
GUILayout.Label("Screenshot");
exportFolderPath = EditorGUILayout.TextField("Folder Path",exportFolderPath);
if (GUILayout.Button("Export and Save"))
{
if (!Directory.Exists(exportFolderPath)) Directory.CreateDirectory(exportFolderPath);
string exportPath = $"{exportFolderPath}/ss_{DateTime.Now:yyyyMMddHHmmss}.png";
ScreenCapture.CaptureScreenshot(exportPath);
File.Exists(exportPath);
Debug.Log("Exported the screenshot.");
}
}
EditorGUILayout.EndVertical();
base.OnInspectorGUI();
}
}
必ずEditorフォルダにコードを置いてください。
初期保存フォルダパスを変更する場合はexportFolderPath、ファイル名を変更する場合は、exportPathの"ss_"以降を変更してください。
シーンビューの位置のスクショが欲しい場合
ゲームビューの位置ではなく、シーンビューの位置でスクリーンショットが欲しい場合は、
カメラオブジェクトを選択した状態でCtrl + Shft + F
を押すとシーンビューの位置にカメラが合いますので、その状態で"Export and Save"を押してください。