유니티에서 간단하게 모든 자식들 접근하기
1. 선택한 게임 오브젝트 바로 아래에 있는 자식만 iterate하고 싶은 경우
| foreach(Transform child in transform) |
| { |
| Debug.Log(child.name); |
| } |
2. 선택한 게임 오브젝트 아래에 있는 모든 자식을 iterate하고 싶은 경우
| Transform[] allChildren = GetComponentsInChildren<Transform>(); |
| foreach(Transform child in allChildren) |
| { |
| |
| |
| if(child.name == transform.name) |
| return; |
| |
| Debug.Log(child.name); |
| } |
GetComponentsInChildren의 결과는 내 오브젝트 자신도 포함해서 반환하므로 자기 자신일 경우는 무시해줘야 한다.
출처
Unity Answer를 참고했습니다.
How can I access the children of a Transform? - Unity Answers
answers.unity.com
댓글을 사용할 수 없습니다.