遍历的话用foreach会分配一个Enumerator,最好换成for循环配合childCount和GetChild(int index)。另外Uni...
“”可以用来指代挂载脚本的物体本身,“..”可以用来指代挂载脚本的物体的父物体,“../..”则是父物体的父物体 也可以通过Transform.parent获取父物体,通过Transform.root获取最上一级的父物体,通过Transform.Getchild()按照子物体的序号获取子物体 以上为个人学习笔记,欢迎指错...
int childCount = transform.childCount; for (int i = 0; i < childCount; i++) { GameObject childGameObject = transform.GetChild(i).gameObject; // TODO: 对子对象进行处理 } 1. 2. 3. 4. 5. 6. 需要注意的是,Transform.GetChild() 方法只能获取直接子对象,如果需要获取更深层级的对象,可以使...
然后Transform.gameObject; ③任意Component: a).Compontent有个公开的成员变量GameObject 二、找组件: ①GameObject: 获取到GameObject–>拿到成员transform–>利用Transform中的方法查找组件 ②Component: a).GetComponent() b).GetComponentInChildren c).GetComponentInParent d).GetCompontents e).GetComponentsInChildre...
GetChild()是查询直接子节点(儿子节点),而Find()则是使用路径遍历所有节点; // This returns the GameObject named Hand. hand = GameObject.Find("Hand"); // This returns the GameObject named Hand. // Hand must not have a parent in the Hierarchy view. ...
SetActive 调用此方法,传入bool参数(true/false)可以使当前物体显示或者隐藏 AddComponent 为游戏对象添加组件,脚本 CompareTag 调用此方法,传入字符串参数(“TagName”)当前游戏对象的tag值是否为参数TagName,名字一样返回True,反之False。 GetComponent 获取游戏对象的组件,脚本 GetComponentInChildren 返回此游戏对象或者它的...
GetComponentInChildrenGets a reference to a component of type T on the same GameObject as the component specified, or any child of the GameObject. GetComponentIndexGets the index of the component on its parent GameObject. GetComponentInParentGets a reference to a component of type T on the same...
FindFinds a child by name n and returns it. GetChildReturns a transform child by index. GetLocalPositionAndRotationGets the position and rotation of the Transform component in local space (that is, relative to its parent transform). GetPositionAndRotationGets the position and rotation of the Tra...
//Make child of this object, so we don't clutter up the //scene hierarchy more than necessary. tile.transform.parent = transform; //Also we don't want these to be drawn while doing refraction/reflection passes, //so we'll add the to the water layer for easy filtering. ...
BoxCollider collider = gameObject.GetComponentInChildren<BoxCollider>();从自己开始往子物体中找,获取组件。如果自己有,选中自己的;自己没有,选中子物体的 collider.enabled = false;设置组件的激活状态 添加组件BoxCollider boxCollider = gameObject.AddComponent<BoxCollider>(); ...