栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity 获取父类 / 子物体

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity 获取父类 / 子物体

Unity获取父类/子物体
  • 获取父类
  • 获取子物体

获取父类

当前对象是子物体Sphere,获取层级列表上一级父物体Cube:

	GameObject sphere;
    GameObject parentOfSphere;

    void Start()
    {
        sphere = GameObject.Find("Sphere");
        parentOfSphere = sphere.transform.parent.gameObject;
        Debug.Log(parentOfSphere.name);
    }

获取子物体
  1. 当前物体是父物体Cube,获取子物体Capsule
	GameObject Cube;
    void Start()
    {
        Cube = GameObject.Find("Cube");
       
        GameObject Second_childOfCube = Cube.transform.GetChild(1).gameObject;
        //index=1 Capsule是cube的第2个子物体,index从0开始
        Debug.Log("Second_childOfCube  " + Second_childOfCube.name);
    }

  1. 当前物体是子物体Sphere,获取子物体Capsule
    GameObject sphere;
    void Start()
    {     
        sphere = GameObject.Find("Sphere");
            
        GameObject SameLayer_OfSphere = sphere.transform.parent.GetChild(1).gameObject;
        //先获取Sphere的父物体,在去获取子物体
        Debug.Log("SameLayer_OfSphere  " + SameLayer_OfSphere.name);
    }


撒花~~加油!!!干文章

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号