본문 바로가기
개발/C#

유니티 C# 부모 자식 오브젝트 가져오기 GetChild 간단 사용법

by SPNK 2022. 8. 17.
반응형

주의

자식 오브젝트는 씬에서 활성화되어 있어야합니다.

 

  • 코드 작성 (GetComponentsInChildren 활용)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GetChild : MonoBehaviour
{
    public Transform[] objList;

    void Start()
    {
        objList = gameObject.GetComponentsInChildren<Transform>();

    }
}

 

  • 다른 방법 (자식 오브젝트를 여러개 가진 자식 오브젝트를 가져오는법)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[System.Serializable]
public class TransformPoint
{
    public Transform[] transformList;
}

public class GetChild : MonoBehaviour
{
    public Transform[] location;

    public List<TransformPoint> listPoint = new List<TransformPoint>();

    void Start()
    {
        for (int i = 0; i < location.Length; i++)
        {
            for (int j = 0; j < location[i].GetChild(0).childCount; j++)
            {
                TransformPoint point = new TransformPoint();
                point.transformList = new Transform[location[i].GetChild(0).childCount];

                for (int k = 0; k < 9; k++)
                {
                    point.transformList[k] = location[i].GetChild(j).GetChild(k);
                }

                listPoint.Add(point);
            }
        }

    }
}

참고할만한 글

 

 

유니티 C# 로컬 푸쉬 알림 Local Notification 간단 구현

에셋 다운로드 Simple Android Notifications Free | 기능 통합 | Unity Asset Store Use the Simple Android Notifications Free from Hippo on your next project. Find this integration tool & more on the Unity Asset Store. assetstore.unity.com 코드 작

parksh3641.tistory.com

 

 

유니티 C# 베터리 잔량 가져오기 Battery 간단 사용법

코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { private void Start() { float number = SystemInfo.batteryLevel; // 베터리 충전량 가져오기 (0

parksh3641.tistory.com

 

반응형

댓글