using UnityEngine;
namespace UVC.Util
{
///
/// 화면 캡처를 위한 유틸리티 클래스입니다.
/// 지정된 카메라 또는 화면 전체를 캡처하여 Texture2D로 반환합니다.
///
public static class ScreenCapture
{
///
/// 화면을 캡처하여 Texture2D로 반환합니다.
/// 대상 카메라가 RenderTexture를 사용하는 경우 해당 크기로 캡처하며,
/// 그렇지 않은 경우 화면 전체를 캡처합니다.
///
/// 캡처된 Texture2D 객체
///
///
/// // 화면 캡처 실행
/// Texture2D capturedImage = ScreenCapture.GetScreenShot(GetComponent());
/// if (capturedImage != null)
/// {
/// Debug.Log("화면 캡처 성공!");
/// // 캡처된 이미지를 파일로 저장하거나 UI에 표시 가능
/// }
/// else
/// {
/// Debug.LogError("화면 캡처 실패: 대상 카메라가 설정되지 않았습니다.");
/// }
///
///
public static Texture2D GetScreenShot(Camera camera)
{
if (camera == null) return null;
if (camera.targetTexture != null)
{
Texture2D screenShot = new Texture2D(camera.targetTexture.width, camera.targetTexture.height, TextureFormat.RGB24, false);
screenShot.wrapMode = TextureWrapMode.Clamp;
RenderTexture rt = camera.targetTexture;
camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, camera.targetTexture.width, camera.targetTexture.height), 0, 0);
screenShot.Apply(false);
RenderTexture.active = null;
return screenShot;
}
else
{
Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
screenShot.wrapMode = TextureWrapMode.Clamp;
RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);
camera.targetTexture = rt;
camera.Render();
camera.targetTexture = null;
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenShot.Apply(false);
RenderTexture.active = null;
return screenShot;
}
}
}
}