ThreeKingdomsSlingshot/Assets/Scripts/Runtime/CaptureUtil.cs

33 lines
1.2 KiB
C#
Raw Permalink Normal View History

using System.Collections;
using System.IO;
using UnityEngine;
namespace TKS
{
/// <summary>카메라를 고정 해상도 렌더 텍스처로 찍어 PNG 로 저장(실측 검증용).</summary>
public static class CaptureUtil
{
public static IEnumerator Capture(Camera cam, int width, int height, string path)
{
var rt = new RenderTexture(width, height, 24);
var prevTarget = cam.targetTexture;
cam.targetTexture = rt;
// 한 프레임 기다려 캔버스가 새 해상도로 배치되게 한 뒤 직접 렌더
yield return null;
cam.Render();
var prevActive = RenderTexture.active;
RenderTexture.active = rt;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
RenderTexture.active = prevActive;
cam.targetTexture = prevTarget;
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllBytes(path, tex.EncodeToPNG());
Object.Destroy(tex);
rt.Release();
Object.Destroy(rt);
}
}
}