标签:dcom gen collect HERE orm transform stat tran 获取
简单直接调用脚本类就OK
using System.Collections.Generic; using UnityEditor; using UnityEngine; /// <summary> /// 寻找子物体和其组件 /// </summary> public static class FindChildAndComponent { /// <summary> /// 查找指定父物体的指定名称的子物体 /// </summary> /// <param name="parentTransform">父物体</param> /// <param name="childName">子物体名称</param> /// <returns>子物体</returns> public static UnityEngine.GameObject FindChild(Transform parentTransform, string childName) { Transform child = parentTransform.Find(childName); if (child) return child.gameObject; UnityEngine.GameObject go = null; for (int i = 0; i < parentTransform.childCount; i++) { child = parentTransform.GetChild(i); go = FindChild(child, childName); if (go) return go; } return null; } /// <summary> /// 查找指定父物体的指定名称的子物体的指定组件 /// </summary> /// <typeparam name="T">组件类型</typeparam> /// <param name="parentTransform">父物体</param> /// <param name="childName">子物体名称</param> /// <returns>组件</returns> public static T FindChild<T>(Transform parentTransform, string childName) where T : Component { UnityEngine.GameObject child = FindChild(parentTransform, childName); if (child) return child.GetComponent<T>(); return null; } /// <summary> /// 获取指定父物体及其子物体上所有指定类型的组件 /// </summary> /// <typeparam name="T">指定类型</typeparam> /// <param name="parentTransform">父物体</param> /// <returns>组件集合</returns> public static List<T> FindChild<T>(Transform parentTransform) where T : Component { List<T> list = new List<T>(); T componet = parentTransform.GetComponent<T>(); if (componet) list.Add(componet); if (parentTransform.childCount > 0) { for (int i = 0; i < parentTransform.childCount; i++) { var temp = FindChild<T>(parentTransform.GetChild(i)); if (temp.Count > 0) list.AddRange(temp); } } return list; } }
标签:dcom gen collect HERE orm transform stat tran 获取
原文地址:https://www.cnblogs.com/VR-1024/p/9815423.html