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
반응형
'프로그램 > 유니티 스크립트 소스' 카테고리의 다른 글
[유니티 스크립트 소스] Excel (3) | 2021.02.02 |
---|---|
[유니티 스크립트 소스] 시스템 정보 (0) | 2021.02.02 |
[유니티 스크립트 소스] 배열에서 최소값, 최소값 얻기 (0) | 2020.04.14 |
[유니티 스크립트 소스] 마우스(Mouse) 관련 (0) | 2020.03.24 |
[유니티 스크립트 소스] 마우스로 게임오브젝트 Drag로 이동시키기 (1) | 2020.03.15 |