Unity寻找子物体的几种方法

admin4年前unity基础API3469

    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);
        }


相关文章

unity 新的输入系统input system 使用

unity 新的输入系统input system 使用

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

MoveTowards、Lerp、Slerp

1:MoveTowards MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta);current:当前位置...

Unity 读取Asset目录下物体

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

Unity移动操作物体

if (Input.touchCount == 1)         {  ...

UI组件绑定事件的三种方法

1:在界面中托转事件2:给物体通过代码添加onClick方法n_Start.onClick.AddListener(OnStartButtonClick);//监听点击事件不同的物体事件名字不同 如o...

发表评论    

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