Transform.Find

Deklarasyon

public Transform Find(string n);


Parametreler

n Bulunacak çocuğun adı.

Dönüş Değeri

Transform Bulunan çocuk transformu. Eşleşen isimde bir çocuk bulunamazsa null döner.

Açıklama

n isminde bir çocuğu bulur ve döndürür.

Eğer n isimde bir çocuk bulunamazsa, null döner. Eğer n bir '/' karakteri içeriyorsa, hiyerarşide bir yol adı gibi Transform'a erişir.

Not: Find, bir Transform hiyerarşisinde özyinelemeli bir iniş gerçekleştirmez.
Not: Find, devre dışı bırakılmış bir GameObject'in transformunu bulabilir.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public GameObject player;
    public Transform gun;
    public Transform ammo;

    // Bir butona tıklandığında çağrılır.
    public void Example()
    {
        // "Silah" adlı çocuğu bulur ve atar.
        gun = player.transform.Find("Gun");

        // Eğer çocuk bulunduysa.
        if (gun != null)
        {
            // "Mermi" adlı çocuğu, "şarjör" adlı GameObject'in (şarjör, "silah"ın bir çocuğudur) bulur.
            ammo = gun.transform.Find("magazine/ammo");
        }
        else
        {
            Debug.Log("No child with the name 'Gun' attached to the player");
        }
    }
}


Find, Transform hiyerarşisinde özyinelemeli bir iniş gerçekleştirmez. Find, yalnızca adlandırılmış bir Transform aramak için verilen çocuk listesini arar. Aşağıdaki örnek, GameObject'ler için Find aramasının sonucunu gösterir. Her GameObject'in adı Find'de kullanılır. Bu nedenle, hiyerarşinin aynı seviyesindeki iki GameObject bulunur ve rapor edilir.


Üç çocuğu olan bir GameObject. Find() üçüncü çocuğu bulamaz.

using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        Transform result;

        for (int i = 1; i < 4; i++)
        {
            string sphr;
            sphr = "sphere" + i.ToString();
            result = gameObject.transform.Find(sphr);

            if (result)
            {
                Debug.Log("Found: " + sphr);
            }
            else
            {
                // Find() sphere3'ü bulamaz
                Debug.Log("Did not find: " + sphr);
                // Ancak '/' karakteri veya GetChild() kullanarak erişebiliriz
                Transform newresult;
                newresult = gameObject.transform.Find("sphere2/sphere3");

                if (newresult)
                {
                    Debug.Log("But now found:" + sphr);
                }
            }
        }
    }
}


Bu blog Unity Docs'un Türkçeye çevrilmiş halidir.

Yorumlar

Bu blogdaki popüler yayınlar

Important Classes - Vectors

RequireComponent

Important Classes - GameObject