유니티 에디터에서 Scene의 화면을 빠르게 캡쳐할 수 있는 유니티 윈도우 에디터를 구현하여 배포합니다.

 

📺 미리 보기

 

GitHub - Bonnate/Unity-Editor-Screenshot-Capture: Unity Editor Window for capturing screenshots

Unity Editor Window for capturing screenshots. Contribute to Bonnate/Unity-Editor-Screenshot-Capture development by creating an account on GitHub.

github.com

 

💬 기능

  • 미리 정의된 해상도 중에서 선택하거나 사용자 지정 해상도를 설정합니다.
  • 선택한 해상도에 따라 종횡비를 자동으로 계산합니다.
  • 캡쳐된 스크린샷을 지정된 폴더에 PNG 파일로 저장합니다.
  • 새로 생성된 스크린샷을 표시하기 위해 Unity Editor의 프로젝트 폴더를 새로 고칩니다.
  • 현재 활성화 된 Scene의 비율에 맞게 스크린샷을 찍을 수 있습니다.

 

⚒️ 구현

· CaptureScene.cs

/*
 * CaptureScene - Unity Editor Window for capturing screenshots
 * Author: [Bonnate] https://bonnate.tistory.com/
 * Repository: [GitHub Repository URL]
 * License: None (Provided without any explicit license)
 *
 * Description:
 * This script defines a custom Unity Editor Window for capturing screenshots in the Unity Editor. 
 * It allows the user to select the desired resolution, specify custom resolutions, and capture the screenshot.
 * The captured screenshot is saved to a specified folder in the project and can be automatically opened.
 *
 * Instructions:
 * 1. Attach this script to an Editor folder in your Unity project.
 * 2. To open the CaptureScene window, go to Tools -> Bonnate -> Capture using Scene Camera.
 * 3. Adjust the resolution settings and choose whether to use the scene camera ratio.
 * 4. Click "Capture Screenshot" to capture the screenshot and save it to the specified folder.
 *
 * Note: This script is provided as-is and may be subject to certain limitations and issues. 
 * Please refer to the repository for the latest updates and to report any issues.
*/

#if UNITY_EDITOR

using UnityEngine;
using UnityEditor;
using System.IO;

public class CaptureScene : EditorWindow
{
    // Define optional resolution
    private enum Resolution
    {
        _HD = 1280,
        _FHD = 1920,
        _QHD = 2560,
        _4K = 3840,
        _8K = 7680,

        // Specify the resolution directly without using the specified resolution.
        Custom
    }

    private Resolution mSelectedResolution = Resolution._FHD;

    private string mAspectRationWidth;

    private string mAspectRatioHeight;

    private bool mIsUseSceneRatio;

    [MenuItem("Tools/Bonnate/Capture using Scene Camera")]

    private static void Init()
    {
        CaptureScene window = (CaptureScene)EditorWindow.GetWindow(typeof(CaptureScene));

        window.Show();
    }

    private void OnGUI()
    {
        // Display a dropdown list for selecting a resolution
        mSelectedResolution = (Resolution)EditorGUILayout.EnumPopup("Resolution", mSelectedResolution);

        if (mSelectedResolution == Resolution.Custom)
        {
            // Call the method to parse a custom resolution
            ParseCustomResolution();
        }
        else
        {
            // Call the method to show aspect ratio fields
            ShowAspectRatioFields();
        }

        // Display a toggle for enabling scene ratio
        mIsUseSceneRatio = EditorGUILayout.Toggle("Use Scene Ratio", mIsUseSceneRatio);

        // Add some space
        GUILayout.Space(10);

        // Display a button for capturing a screenshot
        if (GUILayout.Button("Capture Screenshot"))
        {
            // Call the method to capture a screenshot
            CaptureScreenshot();
        }

        // Add more space
        GUILayout.Space(20);

        // Begin a horizontal layout group
        GUILayout.BeginHorizontal();

        // Display a label with the text "Powered by: Bonnate" at the bottom of the window
        EditorGUILayout.LabelField("Powered by: Bonnate");

        // Create a button with the label "Github" that opens a URL when clicked
        if (GUILayout.Button("Github", GetHyperlinkLabelStyle()))
        {
            // Call the method to open the GitHub URL
            OpenURL("https://github.com/bonnate");
        }

        // Create a button with the label "Blog" that opens a URL when clicked
        if (GUILayout.Button("Blog", GetHyperlinkLabelStyle()))
        {
            // Call the method to open the Blog URL
            OpenURL("https://bonnate.tistory.com/");
        }

        // End the horizontal layout group
        GUILayout.EndHorizontal();
    }

    // Returns a GUI style for a hyperlink-like label
    private GUIStyle GetHyperlinkLabelStyle()
    {
        // Create a new GUIStyle based on the default label style
        GUIStyle style = new GUIStyle(GUI.skin.label);
        style.normal.textColor = new Color(0f, 0.5f, 1f); // Blue color
        style.stretchWidth = false;
        style.wordWrap = false;
        return style;
    }

    // Opens the provided URL with the default application
    private void OpenURL(string url)
    {
        EditorUtility.OpenWithDefaultApp(url);
    }

    // Parses and handles the custom resolution input
    private void ParseCustomResolution()
    {
        if (mSelectedResolution != Resolution.Custom)
        {
            return;
        }

        if (int.TryParse(mAspectRationWidth, out int width) && int.TryParse(mAspectRatioHeight, out int height))
        {
            // Valid custom resolution input
        }
        else
        {
            // Invalid custom resolution input, fallback to default values
            width = 1920;
            height = 1080;
        }

        mAspectRationWidth = EditorGUILayout.IntField("Aspect Ratio Width", width).ToString();

        if (mIsUseSceneRatio)
        {
            EditorGUILayout.LabelField("Aspect Ratio Height", ((int)(width / GetSceneAspectRatio())).ToString());
            mAspectRatioHeight = ((int)(width / GetSceneAspectRatio())).ToString();
        }
        else
        {
            mAspectRatioHeight = EditorGUILayout.IntField("Aspect Ratio Height", height).ToString();
        }
    }

    // Shows the aspect ratio fields based on the selected resolution
    private void ShowAspectRatioFields()
    {
        mAspectRationWidth = ((int)mSelectedResolution).ToString();
        mAspectRatioHeight = ((int)((float)mSelectedResolution * 0.5625f)).ToString();

        EditorGUILayout.LabelField("Aspect Ratio Width", mAspectRationWidth);

        if (mIsUseSceneRatio)
        {
            EditorGUILayout.LabelField("Aspect Ratio Height", ((int)(float.Parse(mAspectRationWidth) / GetSceneAspectRatio())).ToString());
            mAspectRatioHeight = ((int)(float.Parse(mAspectRationWidth) / GetSceneAspectRatio())).ToString();
        }
        else
        {
            EditorGUILayout.LabelField("Aspect Ratio Height", mAspectRatioHeight);
        }
    }

    // Returns the aspect ratio of the scene's camera
    public float GetSceneAspectRatio()
    {
        SceneView sceneView = SceneView.lastActiveSceneView;
        Camera camera = sceneView.camera;

        int width = camera.pixelWidth;
        int height = camera.pixelHeight;

        float aspectRatio = (float)width / height;
        return aspectRatio;
    }

    // Captures a screenshot with the specified resolution and saves it to a file
    private void CaptureScreenshot()
    {
        string timestamp = System.DateTime.Now.ToString("yyMMddHHmmssff");
        string folderPath = "Assets/Screenshots/";
        string fileName = "screenshot" + timestamp + ".png";
        string filePath = folderPath + fileName;

        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
        }

        int width;
        int height;

        if (mSelectedResolution == Resolution.Custom && int.TryParse(mAspectRationWidth, out width) && int.TryParse(mAspectRatioHeight, out height))
        {
            // Custom resolution
        }
        else
        {
            width = (int)mSelectedResolution;

            if (mIsUseSceneRatio)
            {
                height = int.Parse(mAspectRatioHeight);
            }
            else
            {
                height = (int)((float)width * (int.Parse(mAspectRatioHeight) / (float)int.Parse(mAspectRationWidth)));
            }
        }

        SceneView sceneView = SceneView.lastActiveSceneView;
        Camera camera = sceneView.camera;

        RenderTexture renderTexture = new RenderTexture(width, height, 24);
        Texture2D screenshotTexture = new Texture2D(width, height, TextureFormat.RGB24, false);

        RenderTexture currentRT = RenderTexture.active;
        RenderTexture.active = renderTexture;
        camera.targetTexture = renderTexture;
        camera.Render();

        screenshotTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        screenshotTexture.Apply();

        byte[] bytes = screenshotTexture.EncodeToPNG();
        System.IO.File.WriteAllBytes(filePath, bytes);

        RenderTexture.active = currentRT;
        camera.targetTexture = null;

        Debug.Log("Screenshot captured and saved to: " + filePath);

        AssetDatabase.Refresh();

        Object screenshotAsset = AssetDatabase.LoadAssetAtPath<Object>(filePath);
        Selection.activeObject = screenshotAsset;
    }

}

#endif

 

✅ 적용

  • 아래의 깃허브 링크에서 스크립트를 다운로드 받거나, 위 소스코드를 복사하여 스크립트를 작성합니다.
  • 이 스크립트 파일은 이미 EditorOnly로 작성되어있기에 Assets 폴더 내 아무곳에 둬도 상관 없습니다.
 

GitHub - Bonnate/Unity-Editor-Screenshot-Capture: Unity Editor Window for capturing screenshots

Unity Editor Window for capturing screenshots. Contribute to Bonnate/Unity-Editor-Screenshot-Capture development by creating an account on GitHub.

github.com

 

🕹️ Unity Affiliate

  • Unity Affiliate Program 파트너로서 아래의 배너를 통해 접속하신 경우 수수료를 받을 수 있습니다.
  • 아래 배너의 에셋들은 '실시간 무료 에셋 랭킹'을 나타냅니다.
bonnate