[Unity] Game Scene 스크린샷 찍기 / Mac, Window 저장된 폴더 열기

F1을 누르면 스크린샷이 찍히고, F2를 누르면 스크린샷이 저장되는 폴더가 열리도록 하였다.

 

Mac과 Window 에서 각각 파인더, 익스플로러가 열리게끔 알아서 잘 딱 깔끔하게 처리해주는 유니티 기능은 없기 때문에

if define을 사용해 에디터의 환경 별로 따로 구현을 해주어야한다. 

 

using UnityEngine;
using System.IO;

public class ScreenShot : MonoBehaviour
{
	private string Path { get; set; }

    void Start()
    {
        Path = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, @"../Screenshots/"));
    }
    
    void Update()
    {
        // 스크린샷 찍기.
        if(Input.GetKeyDown(KeyCode.F1))
        {
	        // 해당 경로에 폴더가 없다면 새로 생성한다.
            if (!Directory.Exists(Path))        
                Directory.CreateDirectory(Path);

            var fileName = $"{System.DateTime.Now.ToString("MM-dd_HH-mm-ss")}.png";
            ScreenCapture.CaptureScreenshot(System.IO.Path.Combine(Path, fileName), 1);
        }

        // 스크린샷 저장된 폴더 열기.
        if (Input.GetKeyDown(KeyCode.F2))
        {
#if UNITY_IOS
            if (Directory.Exists(Path))
            {                                                    
                System.Diagnostics.Process.Start("open", Path);
            }
#else
			string winPath = Path.Replace("/", "\\");
			
			if (Directory.Exists(winPath)) 
			{
				System.Diagnostics.Process.Start("explorer.exe", $"/root, {winPath}");
			}
#endif
        }
    }
}