Unity寻找子物体的几种方法

admin3年前unity基础API3221

    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:MovePosion移动的新的位置,传入的参数是最新的位置 一般是旧的位置加上变化的参数2:velocity这个方法是瞬间给物体一个恒定的速度,将物体提升至该速度 3:addF...

Unity 创建2D地图技巧

Unity 创建2D地图技巧

1:瓦片尺寸的设置这里的每个单位像素要和图片的真实的宽高一样,否则绘制的时候出现缩放2: 地图碰撞添加需要给地图添加碰撞体如图然后还需要加上如下碰撞体最后需要勾选上 TileMap Coll...

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 插值运算

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

Unity 手机触摸Touch事件

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

发表评论    

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