Unity GameObject && GetComponent
- 我们在unity中用脚本绑定UI对象的时候,一般可以使用GameObject.Find()的方式或者GetComponent这两种方式来获取
 
具体区别如下:
//GameObject.Find的方式查找目标的路径是从Assert的相对路径来查找的,不用将脚本挂载到要查找的对象也可以实现查找,GameObject查找返回的是对象的Inspector的完整属性,如果对象下面挂载了其他对象,也一并包含(子对象)
mplaneTemplate = GameObject.Find("Plane/planeTemplate").gameObject;
Debug.Log(mplaneTemplate.name);
Debug.Log(mplaneTemplate.transform.localPosition);  //print (0,0,0);  面板中的坐标,面板中的坐标即相对于父节点位移坐标
Debug.Log(mplaneTemplate.transform.Position);       //print 世界坐标
//GetComponet必须挂载在对象下面的脚本,且获取对象的一部分属性,其他对象的脚本是无法访问当前对象的属性的
private BoxCollider Box;
Box = GetComponent<BoxCollider>();
                
            
        
浙公网安备 33010602011771号