unity3D只获取孙物体

Unity只获取孙物体

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GetChildren : MonoBehaviour
{
    
    
    public static Transform[] waypoint;
    // Start is called before the first frame update
    void Start()
    {
    
    
        int count = 0;
        foreach (Transform t in GetComponentInChildren<Transform>())
        {
    
    
            count += t.childCount;
        }
        waypoint = new Transform[count];
        count = 0;
        foreach(Transform t in GetComponentInChildren<Transform>())
        {
    
    
            for(int i = 0; i < t.childCount; i++)
            {
    
    
                waypoint[i + count] = t.GetChild(i);
            }
            count += t.childCount;
        }
        for(int i = 0; i < count; i++)
        {
    
    
            Debug.Log(waypoint[i].name);
        }
    }
}

把脚本挂载到父物体上就可以了。

猜你喜欢

转载自blog.csdn.net/weixin_44099953/article/details/127928991