UnityAPI—GetComponent

public class API05GetComponent : MonoBehaviour {

    public GameObject target;

    // Use this for initialization
    void Start () {
        Cube cube = target.GetComponent<Cube>();
        Transform t = target.GetComponent<Transform>();
        Debug.Log(cube);
        Debug.Log(t);
        Debug.Log("---------------------------------");

        Cube[] cubes = target.GetComponents<Cube>();
        Debug.Log(cubes.Length);
        Debug.Log("---------------------------------");

        cubes = target.GetComponentsInChildren<Cube>();
        foreach (Cube c in cubes)
        {
            Debug.Log(c);
        }
        Debug.Log("---------------------------------");
        cubes = target.GetComponentsInParent<Cube>();
        foreach (Cube c in cubes)
        {
            Debug.Log(c);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_42459006/article/details/82077892