🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` using UnityEngine; using System.Collections; /// <summary> /// /// </summary> public class TransformHelper : MonoBehaviour { //调用 private void OnGUI() { if (GUILayout.Button("未知层级下查找")) { var childTF=TransformHelper.GetChild(this.transform, "level3"); childTF.GetComponent<MeshRenderer>().material.color = Color.red; } } /// <summary> /// 在层级未知的情况下查找子物体 /// </summary> /// <param name="parentTF">父物体变换组件</param> /// <param name="childName">子物体名称</param> /// <returns></returns> public static Transform GetChild(Transform parentTF, string childName) { // 在子物体查找 Transform childTF = parentTF.Find(childName); if (childTF != null) return childTF; //将问题交由子物体 int count = parentTF.childCount; for (int i = 0; i < count; i++) { childTF = GetChild(parentTF.GetChild(i), childName); if (childTF != null) return childTF; } return null; } } ```