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>();

posted @   shenlei_blog  阅读(422)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示