您的位置:首页 > 移动开发 > Unity3D

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

2014-12-23 17:10 435 查看
<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>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: