在unity3d中寻找某个父物体中的一个子物体


直接上代码吧,废话不多说。

 

using UnityEngine;
using System.Collections;

public class FindChildObject : MonoBehaviour {

    //要找子物体的那个物体
    public Transform parent;
    //想找的子物体的名字
    public string childName;

    // Use this for initialization
    void Start () {

        GetTransform(parent, childName);
    }
    
    // Update is called once per frame
    void Update () {
    
    }

    Transform GetTransform(Transform check, string name)
    {
        foreach (Transform t in check.GetComponentsInChildren<Transform>())
        {
            if (t.name == name) 
            {
                //要做的事
                Debug.Log(t.name);
                return t;    
            }    
        }
        return null;
    }
}

 

 当然,你还可以直接就用transform.Find()。方法去找。

 

注意:transform.Find()是可以找到隐藏的物体的,但是GameObject.Find()是找不到应经隐藏的的物体的。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM