LoginSignup
1
1

More than 1 year has passed since last update.

【Unity】開発中いつでもカメラのスクリーンショットを撮る

Last updated at Posted at 2022-06-24

Cameraコンポーネントを拡張して非実行時でもゲームビューのスクリーンショットを撮れるようにします。
もちろん実行時でも撮れます。

Cameraコンポーネントから保存

後述するコードを追加すると"Export and Save"ボタンが表示されます。
"Export and Save"ボタンを押すことで指定した場所にpng画像が保存されます。
スクリーンショット 2022-06-24 143405.png

エクスポート結果例

ゲームビューが指定したパスにpng画像で保存されます。
ss_20220624141754.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"を押してください。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1