一、根据名称获取子物体 Transform childTF = this.transform.Find("子物体名称") ; 二、根据路径和名称获取后代物体 Transform childTF = this.transform.Find("子物体名称/子物体名称") ; 第二个可以用来查找孙代或重孙代物体,但是必须在保证路径不会发生改变的条件下使用,不然会出问题。 三、也可以使用索引来...
获取子对象组件:GetComponent只能获取当前GameObject上的组件。如果你需要获取子GameObject上的组件,你需要先使用Transform.Find或Transform.GetChild等方法获取子GameObject的引用,然后在其上调用GetComponent。 使用RequireComponent:如果你知道某个脚本总是需要另一个组件,可以使用RequireComponent属性来自动添加这个依赖项。这样,当...
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. hand = GameObject.Find("/Hand"); 1. 2...
Component[]coms=GetComponents<Component>(); 4. 获取后代物体的指定类型组件,(从自身开始找) varcoms=this.GetComponentsInChildren<MeshRenderer>();varcom1=this.GetComponentInChildren<MeshRenderer>(); 5. 获取父类物体的指定类型组件,(从自身开始找) MeshRenderer[]coms=this.GetComponentsInParent<MeshRenderer...
c).GetComponentInParent d).GetCompontents e).GetComponentsInChildren f).GetComponentsInParent g).FindObjectOfType<>()依据组件类型 h).FindObjectsOfType<>() ③Transform: 已知层级:在他的直接孩子中查找 a).Find(string name) b).FindChild(string name) ...
SetActive 调用此方法,传入bool参数(true/false)可以使当前物体显示或者隐藏 AddComponent 为游戏对象添加组件,脚本 CompareTag 调用此方法,传入字符串参数(“TagName”)当前游戏对象的tag值是否为参数TagName,名字一样返回True,反之False。 GetComponent 获取游戏对象的组件,脚本 GetComponentInChildren 返回此游戏对象或者它的...
//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. ...
GetComponents Gets references to all components of type T on the same GameObject as the component specified. GetComponentsInChildren Gets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject. GetComponentsInParent Gets references to...
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...
this.transform.GetChild(i); } } GameObject /* GameObject 类 if (GUILayout.Button("添加光组件")) { //创建物体 GameObject ao = new GameObject(); //添加组件 Light light = ao.AddComponent<Light>(); //设置光的颜色 light.color = Color.blue; ...