Unity寻找子物体的几种方法

admin4年前unity基础API3412

    1: transform.Find()寻找子物体


   transform.Find(“子物体名字”)



2: GameObject.FindGameObjectWithTag() 通过标签获取物体 针对全局


3:transform.GetChild() 通过序号查找子物体


Debug.Log(transform.GetChild(0));

Debug.Log(transform.GetChild(1));

Debug.Log(transform.GetChild(0).GetChild(0));


4:查找子物体的组件

//查找全部

GetComponentsInChildren

    Rigidbody rigidbody =    GetComponentInChildren<Rigidbody>();

//查找一个

GetComponentInChildren


    
        Rigidbody[] rigidbodys = GetComponentsInChildren<Rigidbody>();
        foreach (var child in rigidbodys) {
            print("子物体名:"+child.name);
        }


相关文章

相机跟随角色移动镜头缩放方法

这种方法最简单可以实现相机跟随角色平移1:计算相机和目标物体的距离  offset = this.transform.position - ...

unity 新的输入系统input system 使用

unity 新的输入系统input system 使用

1:首先在包管理器里安装 搜索"input system"2: 创建input acionts3:生成代码  勾选后点击右小角应用就可以自动生成相关代...

Unity 插值运算

在unity3d中经常用线性插值函数Lerp()来在两者之间插值,两者之间可以是两个材质之间、两个向量之间、两个浮点数之间、两个颜色之间,其函数原型如下: 1.Material.Lerp&nbs...

Unity 读取Asset目录下物体

读取方法:AssetDatabase.LoadAssetAtPath使用方法path = "Assets/CustomizableAnimeGirl/Models/Mat...

Unity 手机触摸Touch事件

1:判断是否触摸如果大于0那就是最少一个手指触摸了屏幕 以此类推 如果=2就是2个手指 最多10个if(Input.touchCount >0)2:判断单...

利用射线获取被点击的物体信息

首先被点击的物体需要加上碰撞体Collider监听点击代码:鼠标点击:if (Input.GetMouseButtonDown(0))     &n...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。