유니티/2D

유니티 : 렌더러 우선순위 정하기 (2D)

근본넘치는개발자 2024. 8. 26. 17:30

 

3D에선 환경 특성상 어떤 물체가 앞에 있고, 뒤에 있는지 한눈에 파악하기 쉽습니다.

하지만 2D는 그렇지 않죠. 이미지가 겹치는 것처럼 보일 겁니다.

 

이미지가 겹치는 경우

특정 이미지를 앞에 있는 것처럼 보이게 하고 싶다면 어떻게 해야 할까요? 

 

오늘은 이에 대해 알아보고자 합니다. 

 

여러 가지 방법이 있겠지만 그중 몇 가지 방법을 정리하고 간단하게 설명해 보겠습니다.

 

https://docs.unity3d.com/6000.0/Documentation/Manual/2DSorting.html

 

Unity - Manual: 2D Sorting

2D Sorting Overview Unity sorts Renderers according to a priority order that depends on their types and usages. You can specify the render order of Renderers through their Render Queue. In general, there are two main queues: the Opaque queue and the Transp

docs.unity3d.com

 

 

1. 카메라 기능을 활용하기

Camera 컴포넌트는 기본적으로 Projection 설정에 따라 렌더러를 정렬합니다.

Perspective Orthographic이 있는 걸 확인 하실 수 있습니다.

 

 

TransparencySortMode 활용하기

Edit -> projectSetting -> Graphisc -> Camera Setting -> Transparency Sort Mode에서 추가로 설정이 가능합니다.

 

https://docs.unity3d.com/kr/current/ScriptReference/TransparencySortMode.html

 

TransparencySortMode - Unity 스크립팅 API

Transparent object sorting mode of a Camera.

docs.unity3d.com

 

 

참고한 블로그 입니다.

https://jjong-ga.tistory.com/100

 

 

이 외에도 Z축을 활용하는 방법도 있습니다.

z축의 position 값을 조절하여 보이는 위치를 조절할 수 있습니다.

Z값이 작을수록 앞쪽에 위치한 것처럼 보입니다.

 

 

2. Layer , Tag , Sorting Layer 활용하기

 

GameObject 아래에 Tag 혹은 Layers를 통해 Add를 눌러 설정할 수도 있고, 

 

 

Edit->Project Setting -> Tags and Layers로 들어가서 설정할 수도 있습니다.

 

각각의 차이에 관해 설명하겠습니다. 

 

태그

프로젝트에서 객체를 식별하기 위해 사용하는 값입니다.

player, MainCamera 등 여러 값이 들어 있는 걸 확인 할 수 있습니다.

이를 이용하여 렌더링 우선순위를 지정할 수 있습니다.

주의할 점은 태그에 이름을 지정한 후에는 바꿀 수 없습니다. 

 

Layers

일반적으로 특정 특성을 공유하는 객체 그룹을 만드는 방법으로 사용합니다.

레이캐스팅이나 렌더링과 같은 작업을 해당 객체 그룹에만 적용되도록 하는 데 사용할 수도 있습니다. 

Builtin Layer 로 표시된 Layers는 Unity에서 사용하는 기본 레이어로, 편집할 수 없습니다. 

User Layer 로 표시된 레이어는 사용자 정의할 수 있습니다.

(단 31번까지만 가능합니다. Built레이어를 제외하면 27개 추가 가능)

 

Sorting Layer

Sorting Layer도 Layers와 유사하게 영역군의 객체를 하나의 레이어로 묶어 관리하기 위한 용도로 사용되지만,

Layers처럼 추가적인 기능은 없고, 렌더링 순서만 관여하는 거 같습니다. 

 

Sprite Renderer에서 Order in Layer 사용하기

 

Sorting Layer 내에서 개체의 정렬 순서를 결정하기 위해 사용되는 값입니다.

숫자가 낮을수록 뒤에 있는 것이며, 숫자가 높을수록 앞에 있다고 이해하시면 됩니다.

 

UI에선 어떨까요?

 

UI에선 Hierarchy의 순서를 따라서 렌더링이 결정됩니다.

 

Hierarchy 아래에 있는 물체(RED)가 가장 앞에 오는 걸 확인 할 수 있었습니다.

 

 

코드로 UI 순서를 바꿀 수는 없을까요? 당연히 가능합니다.

이를 위한 API가 있습니다.

 

Transform.SetAsLastSibling : 순위를 끝으로 변경 -> 맨 앞에 보임 

Transform.SetAsFirstSibling : 순위를 맨 처음으로 변경 -> 맨 뒤에 보임 

Transform.SetSiblingIndex(int index):  원하는 값을 통해 순위 조절

Transform.GetSiblingIndex() : 해당 물체의 순위값을 반환 

 

 

https://docs.unity3d.com/ScriptReference/Transform.SetAsLastSibling.html

 

Unity - Scripting API: Transform.SetAsLastSibling

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

 

https://docs.unity3d.com/ScriptReference/Transform.SetAsFirstSibling.html

 

Unity - Scripting API: Transform.SetAsFirstSibling

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

 

https://docs.unity3d.com/ScriptReference/Transform.SetSiblingIndex.html

 

Unity - Scripting API: Transform.SetSiblingIndex

Use this to change the sibling index of the GameObject. If a GameObject shares a parent with other GameObjects and are on the same level (i.e. they share the same direct parent), these GameObjects are known as siblings. The sibling index shows where each G

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/Transform.GetSiblingIndex.html

 

Unity - Scripting API: Transform.GetSiblingIndex

Use this to return the sibling index of the GameObject. If a GameObject shares a parent with other GameObjects and are on the same level (i.e. they share the same direct parent), these GameObjects are known as siblings. The sibling index shows where each G

docs.unity3d.com

 

 

오늘은 렌더링 순서에 대해 알아보았습니다.

 

혹시나 잘못된 부분이 있다면 댓글로 남겨주시면 감사하겠습니다. 

언제든 환영입니다.