Unity学习笔记(四)-----------脚本、parent、GetComponent、GetComponentInChildren

来源:互联网 发布:2017年8月房地产数据 编辑:程序博客网 时间:2024/05/18 07:45

1.脚本挂到GameObject上,gameobject变量可以获得该脚本依附的对象,同理transform等信息也可以获得


2.在脚本中使用gameObject.transform.parent,可以获得你所依附的对象的父对象,如果再想往上一级获取,可以gameObject.transform.parent.parent


3.GetComponent<T>():获取对象中指定类型的控件(脚本)

   GetComponentInChildren<T>():获取对象的子对象中指定类型的控件,注意的是假如父对象拥有该控件,首先获取的是父对象的控件(它寻找的对象也包括父对象的)

   GetComponents<T>()、GetComponentsInChildren<T>()也是一样的,只不过是获取数组而已,也是要注意GetComponentsInChildren,寻找的对象也包括父对象的


4.GetComponentsInChildren获取的是脚本所依附的对象的所有子对象,包括子对象的子对象(GetComponentInChildren同理)



转载自Mr_Jis的博客

0 0
原创粉丝点击