using UnityEngine;
using UVC.Log;
namespace UVC.Extension
{
///
/// RenderTexture 확장 메서드를 제공하는 클래스입니다.
///
public static class RenderTextureEx
{
///
/// RenderTexture를 Texture2D로 변환합니다.
///
/// 변환할 RenderTexture 객체
/// 변환된 Texture2D 객체
///
/// 사용 예시:
///
/// // RenderTexture 생성
/// RenderTexture renderTexture = new RenderTexture(256, 256, 24);
///
/// // 카메라에서 renderTexture에 렌더링
/// Camera.main.targetTexture = renderTexture;
/// Camera.main.Render();
///
/// // RenderTexture를 Texture2D로 변환
/// Texture2D texture2D = renderTexture.ToTexture2D();
///
/// // 사용 후 정리
/// Camera.main.targetTexture = null;
/// Camera.main.ResetAspect();
///
///
public static Texture2D ToTexture2D(this RenderTexture renderTexture)
{
Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new UnityEngine.Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
return texture2D;
}
///
/// RenderTexture를 바이트 배열로 변환합니다.
///
/// 변환할 RenderTexture 객체
/// 이미지 형식 (기본값: PNG)
/// 바이트 배열
///
/// 사용 예시:
///
/// RenderTexture rt = new RenderTexture(256, 256, 24);
/// byte[] bytes = rt.ToBytes();
/// File.WriteAllBytes("screenshot.png", bytes);
///
///
public static byte[] ToBytes(this RenderTexture renderTexture, TextureFormat format = TextureFormat.RGBA32)
{
Texture2D texture2D = renderTexture.ToTexture2D();
byte[] bytes = texture2D.EncodeToPNG();
Object.Destroy(texture2D);
return bytes;
}
///
/// RenderTexture를 지정된 경로에 PNG 파일로 저장합니다.
///
/// 저장할 RenderTexture 객체
/// 저장할 파일 경로
/// 성공 여부
///
/// 사용 예시:
///
/// RenderTexture rt = new RenderTexture(256, 256, 24);
/// Camera.main.targetTexture = rt;
/// Camera.main.Render();
/// bool success = rt.SaveToPNG("Assets/Screenshots/screenshot.png");
/// Camera.main.targetTexture = null;
///
///
public static bool SaveToPNG(this RenderTexture renderTexture, string filePath)
{
try
{
byte[] bytes = renderTexture.ToBytes();
System.IO.File.WriteAllBytes(filePath, bytes);
return true;
}
catch (System.Exception e)
{
ULog.Error($"RenderTexture를 PNG로 저장하는 데 실패했습니다: {e.Message}", e);
return false;
}
}
///
/// RenderTexture의 크기를 조정합니다.
///
/// 원본 RenderTexture
/// 새 너비
/// 새 높이
/// 크기가 조정된 새로운 RenderTexture
///
/// 사용 예시:
///
/// RenderTexture originalRT = new RenderTexture(1024, 1024, 24);
/// RenderTexture resizedRT = originalRT.ResizeTLBR(512, 512);
/// // 사용 후 정리
/// resizedRT.Release();
/// originalRT.Release();
///
///
public static RenderTexture Resize(this RenderTexture renderTexture, int width, int height)
{
RenderTexture resized = new RenderTexture(width, height, renderTexture.depth);
Graphics.Blit(renderTexture, resized);
return resized;
}
}
}