unity 获取节点下所有子节点路径

  [MenuItem("Util/PrintChildNode")]
    public static void PrintChildNodeM()
    {
        GameObject obj = Selection.activeGameObject;
        string str = "";
        TransformSort(obj.transform, "", ref str);
         Debug.LogWarning(str);
    }

    static void TransformSort(Transform transform, string gap, ref string str)
    {
        foreach (Transform item in transform)
        {
            var path = GetHierarchyRelation(item);
            str += "\"" + path + "\", ";
            TransformSort(item, gap + "   ", ref str);
        }
    }

    static string GetHierarchyRelation(Transform trans)
    {
        StringBuilder hierarchyStrBuilder = new StringBuilder();
        hierarchyStrBuilder.Append(trans.name);
        string separator = "/";
        Transform currentTrans = trans;
        while (currentTrans.parent != null)
        {
            hierarchyStrBuilder.Insert(0, separator);
            hierarchyStrBuilder.Insert(0, currentTrans.parent.name);
            currentTrans = currentTrans.parent;
        }
        string hierarchyStr = hierarchyStrBuilder.ToString();
        return hierarchyStr;
    }

你可能感兴趣的:(unity 获取节点下所有子节点路径)