Unity寻找子物体的几种方法

admin3年前unity基础API3290

    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:首先使用UI做个摇杆的模型 两个图片嵌套,外侧是边界,中间图片是摇杆2:使用inputSYSTEM创建事件3:玩家物体上绑定脚本using System.Collections...

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

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

MoveTowards、Lerp、Slerp

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

Unity场景跳转方法

实现跳转不到的场景,需要先把场景添加到Build中首先引入命名空间using UnityEngine.SceneManagement;然后使用方法跳转 SceneManager.L...

Unity3d KeyCode 键盘各种键值详情

 Backspace     退格键  Delete      De...

发表评论    

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