6156人加入学习
(22人评价)
Unity常用API方法与类详细讲解 - 知识理论篇

制作完成于2022年3月8日,基于Unity2020.3

价格 免费

通过一个物体找到另一个物体的组件

1(通过物体找到组件),先申明一个物体

物体.GetComponent<组件>获取组件 返回值是组件类型的

2(通过外物体的组件查找外物体的另一个组件)

spriteRenderer sr=grisGo.GetComponent<SpriteRenderer>();

组件类型=sr.GetComponent<sr物体所含有的组件>

 

 

 

[展开全文]

Find component in children:自上而下遍历子物体,获得第一个有该类型的物体的组件

[展开全文]

通过组件查找组件 

1.通过父游戏物体查找子游戏物体的组件

①public GameObject enemygo;//先找到父游戏对象

②enemygo.GetComponentInChildren<BoxCollider>();//获取子游戏对象(一个子游戏对象)的组件。

③enemygo.GetComponentsInChildren<BoxCollider>();//获取子游戏对象(多个子游戏对象)的组件,结果输出的是数组。

获取父对象的方法

enemygo.GetComponentInParent<BoxCollider>();

通过组件查找组件

SpriteRenderer sr=girsGo.GetComponent<SpriteRenderer>();

sr.GetComponent<Transform>();

this.GetComponent<Trensform>();

 

[展开全文]

1.查找子级组件

public GameObject enemyGos;

enemyGos.GetComponentInChildren<BoxCollider>();

enemyGos.GetComponentsInChildren<BoxCollider>();

2.查找父级组件

enemyGos.GetComponentInParent<BoxCollider>();

enemyGos.GetComponentsInParent<BoxCollider>();

 

[展开全文]

除了GetComponent还有其他一些衍生的获取组件的API

public GameObject enemyGos;

//使用的是单数时,只会获取其中一个碰撞器enemyGos.GetComponentInChildren<enemyGos>

[展开全文]

授课教师

SiKi学院老师

课程特色

视频(111)
图文(1)
下载资料(2)