유니티 C# 스크린샷 찍고 다운로드 폴더에 이미지 저장 방법 간단 구현

반응형

유니티 C# 스크린샷 찍고 다운로드 폴더에 이미지 저장 방법 간단 구현

using UnityEngine;
using System.IO;

public class ScreenshotManager : MonoBehaviour
{
    public string screenshotFileName = "screenshot.png";

    void Update()
    {
        // 스페이스바를 눌렀을 때 스크린샷을 찍고 저장
        if (Input.GetKeyDown(KeyCode.Space))
        {
            TakeScreenshot();
        }
    }

    // 스크린샷을 찍고 저장하는 함수
    void TakeScreenshot()
    {
        string path = GetScreenshotPath(screenshotFileName);
        ScreenCapture.CaptureScreenshot(path);
        Debug.Log("Screenshot saved to: " + path);
    }

    // 다운로드 폴더 경로 가져오기
    string GetScreenshotPath(string fileName)
    {
        string folderPath = "";

        // 각 플랫폼별로 다운로드 폴더 경로 지정
        if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
        {
            folderPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "Downloads");
        }
        else if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
        {
            folderPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Downloads");
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            folderPath = "/storage/emulated/0/Download"; // Android의 다운로드 폴더
        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            folderPath = Application.persistentDataPath; // iOS에서는 다운로드 폴더 대신 이 경로 사용
        }

        // 폴더가 없으면 생성
        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
        }

        // 파일 전체 경로 반환
        return Path.Combine(folderPath, fileName);
    }
}
반응형