2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】エディタでスクショを撮りやすくしよう!

Last updated at Posted at 2024-12-23

はじめに

Unityでゲーム開発をしていると、何かとスクリーンショットを撮る機会が多い。
実装した機能をPull Requestで共有する場合など、PC標準のスクショ機能を使うだけでも十分な場面はあるが、App Store提出などで決められた解像度のスクショが必要になることもある。
意外なことに、Unityの標準機能にはゲーム実行中に手っ取り早くスクショを撮る機能がない。
UnityRecorderを導入すれば撮るには撮れるが、どちらかというと動画やGIFを撮るのに向いているという印象で、使い勝手はあまり良くない。

そこで、ゲーム実行中にサクッとスクショが撮れるような拡張スクリプトを作ってみる。

環境

OS:macOS Sequoia 15.0
チップ:M2Pro
Unity:2022.3.52f1

実装

まずはエディタ拡張として、スクショを撮れるようにしてみる。

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

public class ScreenshotCapture
{
    //プロジェクト直下に生成
    private const string screenshotFolder = "Screenshots";
    
    [MenuItem("Tools/Capture Screenshot %#k")] // Cmd + Shift + K (Mac)
    private static void CaptureScreenshot()
    {
        if (!Directory.Exists(screenshotFolder))
        {
            Directory.CreateDirectory(screenshotFolder);
        }
        
        string timeStamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
        string fileName = $"{screenshotFolder}/screenshot_{timeStamp}.png";
        
        ScreenCapture.CaptureScreenshot(fileName);

        Debug.Log($"[Screenshot] Saved : {fileName}");
    }
}

これで、上部のメニューから Tools > Capture Screenshot を押すか、キーボードショートカット(今回はCmd + Shift + K)を使用することでスクショが撮影できるようになる。

Gameビューの解像度を変更することで、保存されるスクリーンショットの解像度も変更できる。

また、もちろんMonoBehaviour上から使用することもできる。

public class ScreenshotManager : MonoBehaviour
{
    [SerializeField] private KeyCode screenshotKey = KeyCode.K;

    private const string screenshotFolder = "Screenshots";

    void Update()
    {
        if (Input.GetKeyDown(screenshotKey))
        {
            CaptureScreenshot();
        }
    }

    private void CaptureScreenshot()
    {
        if (!Directory.Exists(screenshotFolder))
        {
            Directory.CreateDirectory(screenshotFolder);
        }

        string timeStamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
        string fileName = $"{screenshotFolder}/runtime_screenshot_{timeStamp}.png";

        ScreenCapture.CaptureScreenshot(fileName);
        Debug.Log($"[Screenshot] Saved : {fileName}");
    }
}

注意点

  • 起動している他のアプリケーションで使用してるショートカットと重複するとうまくいかない可能性があるので、ショートカットキーは慎重に選ぶ必要がある
  • Gameビューにフォーカスを当てておかないとうまく撮影できない
  • gitなどでバージョン管理している場合は、ScreenShotsディレクトリの肥大化に注意する

おわりに

このようなスクリプトを書けば、サクッとスクショが撮影できるようになる。
使いやすい形に拡張して、さらに便利にするのも良いだろう。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?