쫑가 과정

2D sorting / 2d 오브젝트 렌더러 우선순위 정하기. 본문

Unity2D/구현

2D sorting / 2d 오브젝트 렌더러 우선순위 정하기.

쫑가 2022. 2. 16. 19:27

https://docs.unity3d.com/kr/current/Manual/2DSorting.html

 

2D 정렬 - Unity 매뉴얼

Unity는 타입과 용도에 기반한 우선 순위에 따라 렌더러를 정렬합니다. 렌더러 대기열을 통해 렌더러의 렌더 순서를 지정할 수 있습니다. 일반적으로 두 가지 메인 대기열, 즉 불투명 대기열과 투

docs.unity3d.com

쉬운 방법으로 해보자.

1. Sorting Group


https://docs.unity3d.com/kr/current/Manual/class-SortingGroup.html

 

정렬 그룹 - Unity 매뉴얼

정렬 그룹 을 사용하면 그룹 게임 오브젝트를 스프라이트 렌더러와 함께 그룹화하고, 해당 스프라이트를 렌더링하는 순서를 제어할 수 있습니다. Unity는 스프라이트 렌더러를 동일한 정렬 그룹

docs.unity3d.com

 

사용 방법.

1. Inspector - Add Component - Sorting Group
2. Inspector - Sprite Renderer - Addtional Settings

1. Sorting Layer 설정

Sorting Layer설정 내 해당 위치를 기준으로 순서를 결정하고, 리스트에 표시되는 순서로 Sorting Layer를 렌더링.

정렬 레이어 설정에 대한 자세한 내용은 태그 및 레이어를 참조.

위에서 아래로 우선순위가 결정된다.

Ground > Seleted > Default > Player > Tree

캐릭터의 Sorting Layer는 Default, 나무는 그보다 아래에 새로 만든 Tree로 설정.

2. Order in Layer 설정

Sorting Layer 내에서 이 정렬 그룹의 렌더링 순서를 설정.

값이 가장 낮은 렌더러를 렌더링 대기열에 먼저 넣어 값이 높은 렌더러보다 먼저 표시되도록 만듦.

캐릭터의 Order in Layer 1,나무는 0으로 설정

2. 카메라까지의 거리


1. Edit -> Project Setting -> Graphics -> Camera Settings 아래에서 Transparent Sort.

선택한 카메라 모드(Perspective 또는 Orthographic)에 맞게 카메라의 TransparencySortMode를 자동으로 설정.

구현 목표에 따라 Sort Mode를 설정한다.

 

나무 뒤에 플레이어가 보이고 싶지 않다.

Transparency Sort Mode를 Custom Axis로 변경하고 Y축을 렌더링 하고자 Axis를 Y축 1로 설정한다.

중요! 정상적으로 적용되기 위해서는 Sorting Layer가 같아야 한다. 저는 둘 다 Default로 설정

 

나무 뒤로 숨을 수 있게 되었지만 뭔가 잘 숨어지지 않는다.

Y축을 우선으로 렌더링해서 플레이어의 Sort Point인 Center가 나무의 Sort Point인 Center보다 Y축이 커지면 우선순위가 된다. (Y축이 커진 쪽이 우선 순위)

1. Sprite Sort Point

기본적으로 스프라이트는 Sort Point는 Center로 설정되어 있다.
Unity는 정렬하는 동안 렌더링 순서를 결정하기 위해 카메라의 Transform position과 Sprite의 center 사이의 거리를 측정한다.
 
다른 옵션은 Sprite의 Sort Point를 World Space의 Pivot 위치로 설정하는 것.
Sprite의 Sprite Renderer 속성 설정에서 Pivot 옵션을 선택하고 Sprite Editor에서 Sprite의 Pivot 위치를 편집.
 

2. Pivot 설정

Sprite 파일에서 설정해야 함.
Sprite Editor
파란 동그란 놈이 Pivot이다 끌어서 옮기거나 옆 설정창에서 설정가능하다.

나무와 플레이어의 Pivot을 하단 중앙으로 설정하고 다시 테스트.

만족스럽게 설정됐다.

2. 스크립팅 API를 통해 카메라의 TransparencySortMode를 설정.

https://docs.unity3d.com/2020.3/Documentation/ScriptReference/TransparencySortMode.html

 

Unity - Scripting API: TransparencySortMode

By default, perspective cameras sort objects based on distance from camera position to the object center; and orthographic cameras sort based on distance along the view direction. If you're making a 2D game with a perspective camera, you might want to use

docs.unity3d.com

public class MouseInput : MonoBehaviour
{
    public Camera mainCamera;

    private void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            if(mainCamera.transparencySortMode == TransparencySortMode.CustomAxis)
            {
                mainCamera.transparencySortMode = TransparencySortMode.Default;
                Debug.Log(mainCamera.transparencySortMode);
            }
            else
            {
                mainCamera.transparencySortMode = TransparencySortMode.CustomAxis;
                Debug.Log(mainCamera.transparencySortMode);
            }           
        }
    }

'Unity2D > 구현' 카테고리의 다른 글

적 AI. 플레이어 따라가기.  (0) 2022.02.03
2d 클릭 2) 더블 클릭  (0) 2022.01.26
2d 클릭 1) 오브젝트 클릭  (0) 2022.01.24
Comments