본문 바로가기
개발/Unity

Unity)Unity Hierarchy 순서 변경

by 테샤르 2020. 4. 20.

Unity Hierarchy 순서 변경

Unity에서는 기본적으로 Canvas를 기준으로 Hierarchy의 순서(위에서 밑으로) 계층으로 Draw 된다.

그렇기 때문에 Hierarchy의 순서를 변경해야하는 경우가 종종 있다. 스크립트로 처리할 경우는 다음과 같다.

 

 Index to 0
        m_IndexNumber = 0;
        //Set the Sibling Index
        transform.SetSiblingIndex(m_IndexNumber);
        //Output the Sibling Index to the console
        Debug.Log("Sibling Index : " + transform.GetSiblingIndex());
    }

    void OnGUI()
    {
        //Press this Button to increase the sibling index number of the GameObject
        if (GUI.Button(new Rect(0, 0, 200, 40), "Add Index Number"))
        {
            //Make sure the index number doesn't exceed the Sibling Index by more than 1
            if (m_IndexNumber <= transform.GetSiblingIndex())
            {
                //Increase the Index Number
                m_IndexNumber++;
            }
        }

        //Press this Button to decrease the sibling index number of the GameObject
        if (GUI.Button(new Rect(0, 40, 200, 40), "Minus Index Number"))
        {
            //Make sure the index number doesn't go below 0
            if (m_IndexNumber >= 1)
            {
                //Decrease the index number
                m_IndexNumber--;
            }
        }
        //Detect if any of the Buttons are being pressed
        if (GUI.changed)
        {
            //Update the Sibling Index of the GameObject
            transform.SetSiblingIndex(m_IndexNumber);
            Debug.Log("Sibling Index : " + transform.GetSiblingIndex());
        }
    }
}
반응형

 

Transform.SetSiblingIndex

 

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

transform.SetSiblingIndex(m_IndexNumber);

Transform.GetSiblingIndex

 

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

transform.GetSiblingIndex()

 

해당 트랜스폼을 기준으로 Hierarchy 순서를 가져오고 변경하는 코드이다.

 

 

 

반응형

댓글