프로그램/유니티 스크립트 소스

[유니티 스크립트 소스] 화면 캡처 png, jpg, tga, exr 파일로 저장

코더 제로 2020. 5. 8. 03:52
728x90
반응형

1. 네임스페이스

using System; 
using System.IO; 

2. 소스

2.1 화면 캡처 후 jpg 파일로 저장

IEnumerator SaveScreeJpg(string filePath)
{
    yield return new WaitForEndOfFrame();

    Texture2D texture = new Texture2D(Screen.width, Screen.height);
    texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    texture.Apply();
    byte[] bytes = texture.EncodeToJPG();
    File.WriteAllBytes(filePath, bytes);
    DestroyImmediate(texture);
}

 

2.2 화면 캡처 후 tga 파일로 저장

IEnumerator SaveScreenTga(string filePath)
{
    yield return new WaitForEndOfFrame();

    Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    texture.Apply();
    byte[] bytes = texture.EncodeToTGA();
    File.WriteAllBytes(filePath, bytes);
    DestroyImmediate(texture);
}

 

2.3 화면 캡처 후 exr 파일로 저장

IEnumerator SaveScreeExr(string filePath)
{
    yield return new WaitForEndOfFrame();

    Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBAFloat, false);
    texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    texture.Apply();
    byte[] bytes = texture.EncodeToEXR(Texture2D.EXRFlags.CompressZIP);
    File.WriteAllBytes(filePath, bytes);
    DestroyImmediate(texture);
}

 

3. 데모

using System.Collections;
using UnityEngine;
using System;
using System.IO;

public class ScreenCaptureImages : MonoBehaviour
{
    public string m_Path = @"D:\Capture\Images\";
    public string m_FilePrefix = "CoderZero";
    private string m_FilePath;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            m_FilePath = m_Path + m_FilePrefix + DateTime.Now.ToString("yyyyMMddhhmmss") + ".png";
            ScreenCapture.CaptureScreenshot(m_FilePath);
        }

        if (Input.GetKeyDown(KeyCode.J))
        {
            m_FilePath = m_Path + m_FilePrefix + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
            StartCoroutine(SaveScreeJpg(m_FilePath));
        }

        if (Input.GetKeyDown(KeyCode.T))
        {
            m_FilePath = m_Path + m_FilePrefix + DateTime.Now.ToString("yyyyMMddhhmmss") + ".tga";
            StartCoroutine(SaveScreenTga(m_FilePath));
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            m_FilePath = m_Path + m_FilePrefix + DateTime.Now.ToString("yyyyMMddhhmmss") + ".exr";
            StartCoroutine(SaveScreeExr(m_FilePath));
        }
    }

    IEnumerator SaveScreeJpg(string filePath)
    {
        yield return new WaitForEndOfFrame();

        Texture2D texture = new Texture2D(Screen.width, Screen.height);
        texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        texture.Apply();
        byte[] bytes = texture.EncodeToJPG();
        File.WriteAllBytes(filePath, bytes);
        DestroyImmediate(texture);
    }

    IEnumerator SaveScreenTga(string filePath)
    {
        yield return new WaitForEndOfFrame();

        Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        texture.Apply();
        byte[] bytes = texture.EncodeToTGA();
        File.WriteAllBytes(filePath, bytes);
        DestroyImmediate(texture);
    }

    IEnumerator SaveScreeExr(string filePath)
    {
        yield return new WaitForEndOfFrame();

        Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBAFloat, false);
        texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        texture.Apply();
        byte[] bytes = texture.EncodeToEXR(Texture2D.EXRFlags.CompressZIP);
        File.WriteAllBytes(filePath, bytes);
        DestroyImmediate(texture);
    }
}

 

728x90
반응형