ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` 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; } } ```