目录
1.获取和设置父对象
获取父对象
print(this.transform.parent.name);
设置父对象 断绝父子关系
this.transform.parent=null;
设置父对象 认爸爸
this.transform.parent=GameObject.Find("Father").transform;
通过API设置
this.transform.SetParent(null);//断绝父子关系
this.transform.SetParent(GameObject.Find("Father").transform);//认爸爸
参数一:父亲
参数二:是否保留世界坐标的位置,角度,缩放信息(true or false)
this.transform.SetParent(GameObject.Find("Father").transform,false);
2.抛妻弃子
就是和自己所有的子类都没有关系了
this.transform.DetachChildren();
3.获取子对象
按照名字查找(失活对象无法查询)
print(this.transform.Find("Cube").name);
只能找到儿子,不能找到儿子的儿子!
遍历所有的子类
(失活的子类也算,但是孙子不会算数量)
print(this.transform.childCount);
//通过索引号可以得到对应的儿子,编号不能超过子类数量范围,否则会报错!
this.transform.GetChild(0);
for(int i=0;i<this.transform.childCount;i++)
{
print("儿子的名字:"+this.transform.GetChild(i).name);
}
4.儿子的操作
(这里一系列操作都是基于挂载的子对象Transform身上的)
//判断自己的父类
if(son.IsChildOf(this.transform))
{
print("是我的子类");
}
//得到自己作为子类的编号
print(son.GetSiblingIndex());
//设置自己为第一个子类
son.SetAsFirstSibling();
//设置自己为最后一个子类
son.SetAsLastSibling();
//设置为指定子类(无论是否超过范围,甚至写成负数都不会报错,自动生成最后一个子类)
son.SetSiblingIndex(1);