0
点赞
收藏
分享

微信扫一扫

【Unity】关于GetComponentInChildren和GetComponentInParent

朱小落 2022-06-30 阅读 77

获取子物体(GetComponentInChildren) / 父物体(GetComponentInParent)的组件。

经过测试,GetComponentInChildren,会优先判断物体自身是否有目标组件,若有直接返回该组件,不便利子物体;若物体自身没有目标组件,遍历子物体,按照子物体顺序查找(比如:先判断第一个子物体,若没有获取到目标组件,再遍历第一个子物体的子物体(目标物体孙物体),然后再判断目标物体的第二个子物体,以此递归查找)。

GetComponentInParent 同理。

【Unity】关于GetComponentInChildren和GetComponentInParent_unity【Unity】关于GetComponentInChildren和GetComponentInParent_递归_02

延伸:GetComponentsInChildren,会获取包含物体自身、节点下所有子物体、孙物体的目标组件,也是递归:

【Unity】关于GetComponentInChildren和GetComponentInParent_unity_03


举报

相关推荐

0 条评论