当前位置:网站首页>How to get all child objects under an object in Unity
How to get all child objects under an object in Unity
2022-08-08 07:04:00 【LCF_CSharp】
UnityThe method to get all sub-objects under an object
方法1(Get all child objects,regardless of child objectsSetActive是否为true):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public List<GameObject> CH = new List<GameObject>();//A list of stored objects
public Material Dissolve;
// Start is called before the first frame update
void Start()
{
FindChild(this.gameObject);//Find all child objects under the node
DoDissolve();
}
void FindChild(GameObject child)
{
//利用for循环 Get all child objects under the object
for (int c = 0; c < child.transform.childCount; c++)
{
//If there are sub-objects under the sub-object Just pass in the child object for callback lookup until the object has no children
if (child.transform.GetChild(c).childCount > 0)
{
FindChild(child.transform.GetChild(c).gameObject);
}
CH.Add(child.transform.GetChild(c).gameObject);
}
}
}
After getting all child objects,可通过listList batches of objects;
Components such as adding or removing its children,Determine whether its child object has a component and so on.
方法二(推荐):
transform.GetComponentsInChildren<Transform>(); //无法获取SetActive为false的子物体
transform.GetComponentsInChildren<Transform>(true); //Get all child objects,无论SetActive是否为true
该方法为Unity内置的API,All components of the corresponding type under the object will be found;
(注意:This method will find itselfTransform)
边栏推荐
猜你喜欢
fpga卷积神经网络加速器,FPGA卷积神经网络综述
[Unity] 自定义日志系统 解决Unity Log的痛点
文件常用操作 IO流原理及分类
Variational Inference with Normalizing Flows
【图形学】13 UnityShader语义(一)
【图形学】16 光照模型(一、理论与公式)
Flutter 实现一款简单的音乐播放器
NVIDIA CUDA 高度并行处理器编程(九):并行模式:稀疏矩阵-向量乘法
Meta-Learning and in-context Learning
Rose essential oil market research: the current market output value exceeds 2.3 billion yuan, and the market demand gap is about 10%
随机推荐
P03 线程安全 synchronized Lock
Leetcode题目分享以及讲解
C语言实现冒泡排序及对冒泡排序的优化处理
Flutter学习开发资源整理与分享
关于Unity 按键事件响应错误触发UI事件的问题解决
leetcode daily question 8.6 (continuously updated)
深度神经网络的训练过程,深度神经网络训练方法
CV代码细节总结(一)
Yii2中MongoDB的使用方法-CURD
[GWCTF 2019] I have a database 1
yii2使用多个数据库的使用方法
P23 传值和传引用
PyTorch向量变换基本操作
Electronic payment market status quo of the study: 2022 volume is expected to increase to 314.1 billion yuan
MongoDB配置文件.conf配置项介绍
C# Unicode(万国码)文字转换
Leetcode topic sharing and explanation
NVIDIA CUDA 高度并行处理器编程(九):并行模式:稀疏矩阵-向量乘法
动手从零实现一个多层感知机(前馈神经网络)
rhcsa——第三天