유니티 엔진 (Unity Engine)

[Unity] 화면 캡쳐(Screen Capture) 관련 자료 메모

원소랑 2024. 3. 7. 19:08
728x90

화면을 캡쳐가 필요한 일이 종종 있다. 간단한 사례로,

1. Screen Shot 을 파일로 남기기 위해

2. 게임 화면을 캡쳐한 뒤 가공해서 활용하기 위해

 

필요한 기능이 디테일하게 조금씩 달라질 수 있음.

화면 캡쳐를 위한 두 가집 방향성을 제시.

 

1. ScreenCapture 클래스 정적 메소드 사용

아래 함수를 활용해서 현재 화면을 바로 캡쳐할 수 있다. 아래 샘플 코드는 Texture 타입으로 반환하는데, 다른 함수도 있으니 참고.

Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();

https://docs.unity3d.com/ScriptReference/ScreenCapture.html

 

Unity - Scripting API: ScreenCapture

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

이 때, 이 함수는 현재 화면을 그대로 캡쳐하기 때문에 GUI 등의 내용도 모두 한 번에 캡쳐된다. 말 그대로 스크린샷 파일을 남기고 싶을 때 간단하게 활용할 수 있음.

Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();
targetRenderer.material.mainTexture = texture;

 

 

2. 카메라의 Render() 함수로 RenderTexture 에 그리기

호출 시점의 카메라 화면을 그리는 방법. Camera 의 targetTexture 를 RenderTexture로 설정하고, 직접 Camera.Render() 함수를 호출. 이렇게 카메라의 targetTexture 를 얻어와서 RenderTexture.active 에 셋팅하고 Texture2D.ReadPixels 로 읽어어온다.

RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
Camera.main.targetTexture = renderTexture;
Camera.main.Render();

Camera.main.targetTexture = null;

RenderTexture.active = renderTexture;

Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();

targetRenderer.material.mainTexture = texture;
RenderTexture.active = null;

 


게임 개발에 필수적인 내용을 담는 명서들을 소개합니다.

 

<유니티 교과서 개정6판>(유니티 최신 버전)
https://link.coupang.com/a/be3P0t

 

유니티 교과서 개정6판

COUPANG

www.coupang.com

 

<대마왕의 유니티 URP 셰이더 그래프 스타트업>

https://link.coupang.com/a/bs8qyC

 

대마왕의 유니티 URP 셰이더 그래프 스타트업

COUPANG

www.coupang.com


<리얼-타임 렌더링(REAL-TIME RENDERING) 4/e>
https://link.coupang.com/a/8VWas

 

리얼-타임 렌더링 4/e

COUPANG

www.coupang.com

 

<이득우의 게임 수학:39가지 예제로 배운다! 메타버스를 구성하는 게임 수학의 모든 것>
https://link.coupang.com/a/9BqLd

 

이득우의 게임 수학:39가지 예제로 배운다! 메타버스를 구성하는 게임 수학의 모든 것

COUPANG

www.coupang.com

 

유니티 에셋 스토어 링크
https://assetstore.unity.com?aid=1011lvz7h

 

에셋스토어

여러분의 작업에 필요한 베스트 에셋을 찾아보세요. 유니티 에셋스토어가 2D, 3D 모델, SDK, 템플릿, 툴 등 여러분의 콘텐츠 제작에 날개를 달아줄 다양한 에셋을 제공합니다.

assetstore.unity.com

(링크를 통해 도서/에셋 구입시 일정액의 수수료를 지급받습니다.)


 

728x90
반응형