unity3d:遍历查找,输出子物体在父物体下的路径

2023-08-24 14:49:20 浏览数 (3)

代码语言:javascript复制
 [MenuItem("SaveTransInfo/GetUIChildPath #`")]
    static void GetUIChildPath()
    {
        GameObject obj = Selection.activeGameObject;


        Transform trans = obj.transform;
        listParName.Clear();
        FindParName(trans);

        string parPath = "";
        for (int i = listParName.Count - 1; i >= 0; i--)
        {
            parPath  = listParName[i];
            if (i != 0)
            {
                parPath  = "/";
            }
        }

        Debug.Log(parPath);
         TextEditor te = new TextEditor();
        te.content = new GUIContent(parPath);
        te.SelectAll();
        te.Copy();
    }

    static List<string> listParName = new List<string>();
    public static  bool FindParName(Transform trans)
    {
        
        if (trans == null)
        {
            return false;
        }
        if (trans.GetComponent<UGUIPanel>() == null)
        {
            listParName.Add(trans.name);
            FindParName(trans.parent);
            return false;
        }
        return true;

    }

输出挂载UGUIPanel的父物体下某个子物体路径

0 人点赞