[Unity] 하이러키 순서 바꾸는 함수들

jh Seo·2022년 9월 21일
1

유니티

목록 보기
20/42

개요

인벤토리 UI시스템을 구현하는 중, 아이템 드래그 부분을 구현중이였다.

아이템을 드래그한다면 드래그 중에 해당 아이템이 제일 앞에 위치해야하는데
어떤 방법을 사용하는지 궁금했다.

transform.SetAsLastSibling()

하이러키의 local transform list에서 해당 오브젝트 순위를 맨 밑으로 보낸다.
-> 제일 앞으로 나온다.

  • 예시
using UnityEngine;
using System.Collections;
using UnityEngine.UI; //Required when using UI Elements.
using UnityEngine.EventSystems; // Required when using event data.

public class ExampleClass : MonoBehaviour, IPointerDownHandler
{
    public RectTransform panelRectTransform;

    //Invoked when the mouse pointer goes down on a UI element.
    public void OnPointerDown(PointerEventData data)
    {
        // Puts the panel to the front as it is now the last UI element to be drawn.
        panelRectTransform.SetAsLastSibling();
    }
}

IPointerDownHandler인터페이스의 OnPointerDown함수를 이용해 마우스를 클릭했을 때,
panelRectTransform을 해당 local transform list에서 제일 앞으로 보내는 예시다.

transform.SetAsFirstSibling()

위의 함수와는 다르게 local transform list에서 해당 오브젝트 순위를 맨 앞으로 보낸다.
->local transform list에서 제일 뒤로 가서 가려진다.

  • 예시
using UnityEngine;
using System.Collections;
using UnityEngine.UI; //Required when using UI Elements.
using UnityEngine.EventSystems; // Required when using event data.

public class ExampleClass : MonoBehaviour, IPointerDownHandler
{
    public RectTransform panelRectTransform;

    //Invoked when the mouse pointer goes down on a UI element.
    public void OnPointerDown(PointerEventData data)
    {
        // Puts the panel to the back as it is now the first UI element to be drawn.
        panelRectTransform.SetAsFirstSibling();
    }
}

이 예제에선 panelRectTransform이 제일 앞으로 가
local tranform list에 다른 오브젝트가 있다면 다른 오브젝트들에 가려진다.

transform.SetSiblingIndex(int index)

해당 오브젝트의 local transform list에서의 순위를 index로 정하는 함수이다

transform.GetSiblingIndex()

해당 오브젝트의 local transform list에서의 순위를 int형으로 return 하는 함수다.

생각

item을 클릭했을 때, 해당 item의 순서를 GetSiblingIndex를 이용해 저장 후,

드래그 중엔 transform.SetAsLastSibling()을 이용해
모든 아이템 아이콘 중에 순서를 맨 뒤로 세팅하는 식으로 구현을 하였고,

드래그가 끝냈을 때 저장된 원래 순서로 SetSiblingIndex를 이용해 바꿔주었다.

profile
코딩 창고!

0개의 댓글