个人感觉第一种和第二种比较简洁 第三种没有用字符串的+拼接使用了StringBulider效率会高点
public static string GetHierarchyPath(Transform trans)
{
if (!trans.parent)
{
return trans.name;
}
return GetHierarchyPath(trans.parent) + "/" + trans.name;
}
public static string GetHierarchyPath2(GameObject go)
{
var parent = go.transform.parent;
return parent == null ? go.name : GetHierarchyPath2(parent.gameObject) + "/" + go.name;
}
static bool GetHierarchyPath3(GameObject go,ref StringBuilder sb)
{
if (go != null)
{
if (go.transform.parent != null)
{
if(GetHierarchyPath3(go.transform.parent.gameObject, ref sb))
{
sb.Append(go.name);sb.Append("/");
return true;
}
}
else
{
sb.Append(go.name);sb.Append("/");
return true;
}
}
return false;
}