一、获取物体
1按名称获取(若没有重名物体)
GameObject node = GameObject.Find("旋翼");
路径获取
GameObejct node = GameObject.Find("无人机/旋翼");
2 引用获取
添加一个变量,再检查器中引用目标
public GameObject wingNode;
二、父子物体
场景中的层级关系/父子关系,是由Transform维持的。
获取父级:
Transfrom parent = this.transform.parent;
获取父级节点:
GameObject parentNode = this.transfrom.parent.gameObject;
获取子级,有几种方式:
1 froeach 遍历
foreach(Transform child in transform){
Debug.Log("子物体:"+ child.name)
}
2 GetChild(),按索引获取
例如,获取第0个子项,
Transform aa = this.transform.GetChild(0);
3 transform.Find(), 按名字查找子项
Transform aa = this.transform.Find("aa");
Transform bb = this.transform.Find("aabb);
Transform cc = this.transform.Find("bb/cc");
其中cc是bb的子项
三、物体的操作
设置新的父级,
this.transform.SetParent(other);
设为一级节点,
this.transform.SetParent(null);
其中,parent为null表示一级节点(没有父级)
GameObject.setActive(),显示/隐藏
例如,
Transform child = this.transform.Find("aa");
if(child.gameObject.activeSelf){
child.gameObject.SetActive(false);
}
else{
child.gameObejct.SetActive(true);
}