Beispiel #1
0
    public int GetChildCount(GameObject parent, bool includeHide = true)
    {
        int count = 0;

        foreach (Transform child in parent.GetComponentsInChildren <Transform>(true))
        {
            if (child == null)
            {
                // 过滤已经销毁的嵌套子对象
                continue;
            }
            GameObject objtmp = child.gameObject;
            if (parent == objtmp)
            {
                continue;
            }

            if (!includeHide)
            {
                if (!objtmp.activeSelf)
                {
                    //过虑隐藏的
                    continue;
                }
            }

            {
                LayoutElement le = objtmp.GetComponent <LayoutElement>();
                if (le != null && le.ignoreLayout)
                {
                    continue;
                }
            }

            {
                LayOutElement le = objtmp.GetComponent <LayOutElement>();
                if (le != null && le.ignoreLayout)
                {
                    continue;
                }
            }


            if (objtmp.transform.parent != parent.transform)
            {
                //只找第一层子物体
                continue;
            }
            count++;
        }
        return(count);
    }
Beispiel #2
0
    public override void LayOut()
    {
        int idx = 0;
        int r = 0, c = 0;

        if (!Enable())
        {
            return;
        }
        base.LayOut();

        /*
         * foreach (Transform child in objMainWorld.transform)这种方式遍历子元素会漏掉部分子元素
         */

        //GetComponentsInChildren寻找的子对象也包括父对象自己本身和子对象的子对象
        foreach (Transform child in this.gameObject.GetComponentsInChildren <Transform>(true))
        {
            if (child == null)
            {
                // 过滤已经销毁的嵌套子对象
                Debug.Log("LayOut child is null idx=" + idx);
                continue;
            }
            GameObject objtmp = child.gameObject;
            if (this.gameObject == objtmp)
            {
                continue;
            }

            {
                LayoutElement le = objtmp.GetComponent <LayoutElement>();
                if (le != null && le.ignoreLayout)
                {
                    continue;
                }
            }

            {
                LayOutElement le = objtmp.GetComponent <LayOutElement>();
                if (le != null && le.ignoreLayout)
                {
                    continue;
                }
            }

            if (!enableHide)
            {
                if (!objtmp.activeSelf)
                {
                    //过虑隐藏的
                    continue;
                }
            }

            if (objtmp.transform.parent != this.gameObject.transform)
            {
                //只找第一层子物体
                continue;
            }

            //  LayoutElement
            r = idx / col;
            c = idx - r * col;

            //从顶部往底部显示
            if (dispLayVertical == DispLayVertical.TOP_TO_BOTTOM)
            {
                r = row - 1 - r;
            }

            //从右往左显示
            if (dispLayHorizontal == DispLayHorizontal.RIGHT_TO_LEFT)
            {
                c = col - 1 - c;
            }

            Vector2       pt     = GetItemPostion(child.gameObject, r, c);
            RectTransform rctran = child.gameObject.GetComponent <RectTransform>();
            if (rctran != null)
            {
                rctran.anchoredPosition = pt;
                Debug.Log("GetItemPostion:idx=" + idx + " r=" + r + " c=" + c + " pt=" + pt);
            }
            idx++;
        }
    }