본문 바로가기
개발/Unity

Unity 코드) GameObject 전체 경로 찾기(부모 오브젝트 포함) (GameObject : Path)

by 테샤르 2023. 12. 13.

GameObject 전체 경로 찾기(부모 오브젝트 포함)  (GameObject : Path)

 

간단하게 특정 오브젝트(GameObject)를 기준으로 부모오브젝트를 찾는 코드이다.

 

    public string GetGameObjectPath(GameObject obj)
    {
        string path = null;
        if (obj) //오브젝트가 존재하면
        {
            path = obj.name;

            while (obj.transform.parent) //부모가 존재하면
            {
                obj = obj.transform.parent.gameObject;
                path = obj.name + '/' + path; //경로 추가
            }
        }
        return path; //경로 반환
    }

 

 

반응형

 

Unity 의 GameObject의 구조는 Root 를 기준으로 하위에 추가되는 구조이기 때문에 가능한 코드이다.

 

<예시>

 

결과 : ContentSizeFitter/GameObject

 

Unity Transform : [링크]

 

Unity - Scripting API: Transform

Every object in a Scene has a Transform. It's used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically. This is the hierarchy seen

docs.unity3d.com

 

★☆☆☆☆

 

반응형

댓글