获取子物体(GetComponentInChildren) / 父物体(GetComponentInParent)的组件。 经过测试,GetComponentInChildren,会优先判断物体自身是否有目标组件,若有直接返回该组件,不便利子物体;若物体自身没有目标组件,遍历子物体,按照子物体顺序查找(比如:先判断第一个子物体,若没有获取到目标组件,再遍历第一个子物体的子物体(...
MonoBehavior直接提供了查找父子组件的方法GetComponent(s)/GetComponent(s)InParent和GetComponent(s)InChildren,因此直接调用即可。对于泛型方法,每个子对象只会找到一个组件,所以通常适用于子组件非常简单的场景。 1 2 3 4 5 6 var renderers = GetComponentsInChildren<Renderer>(); for (var i = 0; i < ren...
public Component GetComponentInParent (Type type); 参数 type 要查找的组件的类型。描述 返回GameObject 或其任何父项中类型为 type 的组件。向上递归,直到其找到有效组件为止。如果未找到组件,则返回 null。仅返回活动游戏对象上的组件。using UnityEngine; using System.Collections;public class GetComponentInParent...
Gets a reference to a component of type T on the same GameObject as the component specified, or any parent of the GameObject.
public Component GetComponentInParent(Type t); Parameters t The type of Component to retrieve. Returns Component A component of the matching type, if found. Description Returns the component of Type type in the GameObject or any of its parents. Recurses upwards until it finds a valid componen...
GetComponentInChildren:获取游戏对象或其子对象上指定类型的第一个组件 GetComponents:获取游戏对象上指定类型的所有组件 AddComponent:为游戏对象添加指定组件 SendMessage:调用游戏对象上所有MonoBehaviour的指定名称方法 SendMessageUpwards:调用游戏对象及其所有父对象上所有MonoBehaviour的指定名称方法 ...
GameObject.GetComponentsInParent\<T\>() GameObject.GetComponentInChildren\<T\>() GameObject.GetComponentsInChildren\<T\>() 后话 前言 在unity的实际项目开发中,往往都要获取游戏内的各种物体,比如玩家(Player),怪物(Monster),或者其他,那有什么方法可以快速获取呢,下面就来介绍unity获取游戏对象的几个方法 ...
GetComponent 获取游戏对象的组件,脚本 GetComponentInChildren 返回此游戏对象或者它的所有子对象上(深度优先)的类型为type的组件。 GetComponentInParent 从父对象查找组件。 GetComponents 返回该游戏对象所有type类型的组件列表。 GetComponentsInChildren *返回此游戏对象与其子对象所有type类型的组件。* GetComponentsIn...
InventroyManager.Instance.currentDrag.originalHolder = GetComponentInParent<SlotHolder>(); InventroyManager.Instance.currentDrag.originalParent = (RectTransform)transform.parent;//为了使物品不被物品栏遮挡,将其设置为一个层级更高的画布的子物体transform.SetParent(InventroyManager.Instance.DragCanvas.transform,tru...
这是因为物体body和它的父子物体都有Transform组件,而Rigidbody2D组件却是物体body所没有的。GetComponentInChildren和GetComponentInParent会先从挂载脚本的物体本身开始寻找符合要求的组件(不太理解为什么要这么设计) 1.2三种GetComponents 我们给body,body的父物体player、hold以及body的子物体inhand、outhand分别挂载两个Te...