Unity通过层级关系获取子物体

来源:互联网 发布:人工智能 军民融合 编辑:程序博客网 时间:2024/05/20 01:11
<span style="font-size:18px;"></span>
<span style="font-size:18px;">获取模型相对于跟节点的层级目录,比如一个角色模型的手相对于角色根节点的层级关系,该方法返回的 为一个路径 </span>
<span style="font-size:18px;"></span>
<pre name="code" class="csharp"><span style="font-size:18px;">// 一般做捡起武器,或者生成特效时,为了获取精准位置,    //需要找到角色模型的子物体(手),通过层级目录可以    //找到模型上每个子物体,下面方法为获取一个子物体相对于跟物体的层级目录    // LeftShoulder/Arm/Hand/Finger 返回这样的层级目录    public static string GetGameObjectPath(GameObject obj)    {        string path = "/" + obj.name;        while (obj.transform.parent != null)        {            obj = obj.transform.parent.gameObject;            path = "/" + obj.name + path;        }        return path;    }    //通过层级关系查找子物体    // LeftShoulder/Arm/Hand/Finger    public Transform FindObj(string path)    {        return transform.Find(path);    }</span>


                                             
0 0
原创粉丝点击