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
}
}
}
'Unity' 카테고리의 다른 글
[Unity] SelectionBase Attribute / 씬뷰에서 우선 선택될 GameObject 지정해주기 (0) | 2024.07.24 |
---|---|
[Unity] ScriptableObject 를 이용한 데이터 저장 및 참조 (0) | 2024.07.08 |
[Unity] GameObject 생성시 Position (0, 0, 0) 자동 초기화, Scene View Preferences (0) | 2024.06.27 |