在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