using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class ScreenshotTaker : MonoBehaviour
{
// 스크린샷 파일이 저장될 디렉토리의 이름을 지정
public string screenshotDirectory = "Screenshots";
// 스크린샷 파일의 이름을 지정
public string screenshotFileName = "screenshot";
// 스크린샷을 찍을 때 사용할 키를 지정
public KeyCode screenshotKey = KeyCode.P;
void Update()
{
// 지정된 키를 눌렀을 때 스크린샷을 찍는 메서드 호출
if (Input.GetKeyDown(screenshotKey))
{
TakeScreenshot();
}
}
// 스크린샷을 찍는 메서드
void TakeScreenshot()
{
// 스크린샷 디렉토리의 전체 경로를 생성
string fullPath = System.IO.Path.Combine(Application.dataPath, screenshotDirectory);
// 디렉토리가 존재하지 않으면 디렉토리를 생성
if (!System.IO.Directory.Exists(fullPath))
{
System.IO.Directory.CreateDirectory(fullPath);
}
// 현재 시간을 기반으로 타임스탬프를 생성
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HHmmss");
// 스크린샷 파일의 전체 경로를 생성
string screenshotPath = System.IO.Path.Combine(fullPath, $"{screenshotFileName} {timestamp}.png");
// 스크린샷을 찍어서 파일로 저장
ScreenCapture.CaptureScreenshot(screenshotPath);
// 스크린샷이 저장되었다는 메시지를 콘솔에 출력
Debug.Log("Screenshot taken and saved as " + screenshotPath);
}
}
System.IO.Path.Combine : 두 개 이상의 경로 구성 요소를 결합하여 하나의 전체 경로를 생성
Application.dataPath : 프로젝트의 Assets 폴더의 경로
DateTime.Now.**ToString** : 현재 날짜와 시간의 DateTime 객체를 반환
P키로 스크린샷을 찍으면 저장된 파일의 전체 경로를 콘솔 출력
(Assets 폴더 하위로 새로운 폴더를 생성하여 파일이 저장됨
