unity获取对象

       unity提供了多种获取某个对象的方法,

       1. GameObject.Find("name");

       2. GameObject.FindGameObjectWithTag("tag");

       3. GameObject.FindWithTag("tag");

      find方法都是全场景寻找,算是unity里最耗事的一种方法,通过tag寻找次之,unity内置了tag系统,应当说是较常见的方法之一,如果需要全场景寻找,FindWithTag会是不错的选择。

      4.transform.GetComponent<T>,transform.GetComponentInChildren<T>,transform.GetComponents<T>

       这是基于当前脚本,通过类型Class来获取某个类,

     5.transform.Find("name");

     也可通过名字获取子对象,

       另外也可选择在实例化对象时保存引用,这是最快的方法。

      

posted @ 2014-12-04 16:00  sevenmoons  阅读(2196)  评论(0)    收藏  举报