/// <summary>
 ///   <para>Returns the preferred size of the layout element.</para>
 /// </summary>
 /// <param name="rect">The RectTransform of the layout element to query.</param>
 /// <param name="axis">The axis to query. This can be 0 or 1.</param>
 public static float GetPreferredSize(RectTransform rect, int axis)
 {
     if (axis == 0)
     {
         return(LayoutUtility.GetPreferredWidth(rect));
     }
     return(LayoutUtility.GetPreferredHeight(rect));
 }
Exemple #2
0
 //lay size cua item + spacing
 protected float GetSize(RectTransform item)
 {
     if (scrollType == ScrollType.Horizontal)
     {
         return(LayoutUtility.GetPreferredWidth(item) + itemSpacing);
     }
     else
     {
         return(LayoutUtility.GetPreferredHeight(item) + itemSpacing);
     }
 }
        protected override float GetSize(RectTransform item)
        {
            float size = contentSpacing;

            if (m_GridLayout != null)
            {
                size += m_GridLayout.cellSize.x;
            }
            else
            {
                size += LayoutUtility.GetPreferredWidth(item);
            }
            return(size);
        }
        public static float GetPreferredSize(RectTransform rect, int axis)
        {
            float result;

            if (axis == 0)
            {
                result = LayoutUtility.GetPreferredWidth(rect);
            }
            else
            {
                result = LayoutUtility.GetPreferredHeight(rect);
            }
            return(result);
        }
        //------------------------------------------------------------------------------------------------------
        public override void CalculateLayoutInputHorizontal()
        {
            base.CalculateLayoutInputHorizontal();

            InitializeLayout();

            float totalMinWidth       = padding.horizontal;
            float totalPreferredWidth = padding.horizontal + totalColumnWidth + spacing.x * (columns - 1);

            SetLayoutInputForAxis(totalMinWidth, totalPreferredWidth, -1, 0);

            // Stretch if there is a layout element specifying extra space and child force expand width is on
            float extraWidth = LayoutUtility.GetPreferredWidth(rectTransform) - totalPreferredWidth;

            if (extraWidth > 0 && childForceExpandWidth)
            {
                // Don't expand column if all cells in column override expansion to false
                bool[] expandColumn    = new bool[columns];
                int    columnsToExpand = 0;
                for (int c = 0; c < columns; c++)
                {
                    expandColumn [c] = false;
                    for (int r = 0; r < rows; r++)
                    {
                        int index = GetCellIndexAtGridRef(c, r);
                        if (index < rectChildren.Count)
                        {
                            var child       = rectChildren [index];
                            var cellOptions = child.GetComponent <VariableGridCell> ();
                            if (cellOptions == null || !cellOptions.overrideForceExpandWidth || cellOptions.forceExpandWidth)
                            {
                                expandColumn [c] = true;
                                columnsToExpand++;
                                break;
                            }
                        }
                    }
                }

                // Give extra space equally - for future version could also make option to give extra space proportionally
                for (int c = 0; c < columns; c++)
                {
                    if (expandColumn[c])
                    {
                        columnWidths [c] += extraWidth / columnsToExpand;
                    }
                }
            }
        }
Exemple #6
0
        protected override float GetSize(RectTransform item)
        {
            float size = contentSpacing;

            if (m_GridLayout != null)
            {
                size += m_GridLayout.cellSize.x;
            }
            else
            {
                // 计算item宽度
                if (item.gameObject.activeInHierarchy && item.gameObject.activeSelf)
                {
                    size += LayoutUtility.GetPreferredWidth(item);
                }
                // item不活跃状态下计算高度会始终返回0,这里直接返回item宽度
                else
                {
                    size += item.sizeDelta.x;
                }
            }
            return(size);
        }
        //------------------------------------------------------------------------------------------------------
        private void InitializeLayout()
        {
            columns = (constraint == Constraint.FixedColumnCount) ? Mathf.Min(constraintCount, rectChildren.Count) : Mathf.CeilToInt((float)rectChildren.Count / (float)constraintCount);
            rows    = (constraint == Constraint.FixedRowCount) ? Mathf.Min(constraintCount, rectChildren.Count) : Mathf.CeilToInt((float)rectChildren.Count / (float)constraintCount);

            cellIndexAtGridRef = new int[columns, rows];
            cellColumn         = new int[rectChildren.Count];
            cellRow            = new int[rectChildren.Count];
            cellPreferredSizes = new Vector2[rectChildren.Count];
            columnWidths       = new float[columns];
            rowHeights         = new float[rows];
            totalColumnWidth   = 0;
            totalRowHeight     = 0;
            for (int a = 0; a < columns; a++)
            {
                for (int b = 0; b < rows; b++)
                {
                    cellIndexAtGridRef [a, b] = -1;
                }
            }

            int cOrigin = 0;
            int rOrigin = 0;
            int cNext   = 1;
            int rNext   = 1;

            if (startCorner == Corner.UpperRight || startCorner == Corner.LowerRight)
            {
                cOrigin = columns - 1;
                cNext   = -1;
            }
            if (startCorner == Corner.LowerLeft || startCorner == Corner.LowerRight)
            {
                rOrigin = rows - 1;
                rNext   = -1;
            }
            int c = cOrigin;
            int r = rOrigin;

            for (int cell = 0; cell < rectChildren.Count; cell++)
            {
                cellIndexAtGridRef [c, r] = cell;
                cellColumn [cell]         = c;
                cellRow [cell]            = r;
                cellPreferredSizes [cell] = new Vector2(LayoutUtility.GetPreferredWidth(rectChildren[cell]), LayoutUtility.GetPreferredHeight(rectChildren[cell]));
                columnWidths [c]          = Mathf.Max(columnWidths [c], cellPreferredSizes [cell].x);
                rowHeights [r]            = Mathf.Max(rowHeights [r], cellPreferredSizes [cell].y);

                // next
                if (startAxis == Axis.Horizontal)
                {
                    c += cNext;
                    if (c < 0 || c >= columns)
                    {
                        c  = cOrigin;
                        r += rNext;
                    }
                }
                else
                {
                    r += rNext;
                    if (r < 0 || r >= rows)
                    {
                        r  = rOrigin;
                        c += cNext;
                    }
                }
            }

            for (int col = 0; col < columns; col++)
            {
                totalColumnWidth += columnWidths[col];
            }
            for (int row = 0; row < rows; row++)
            {
                totalRowHeight += rowHeights[row];
            }
        }
Exemple #8
0
 protected override float GetSize(RectTransform item)
 {
     return(LayoutUtility.GetPreferredWidth(item) + ContentSpacing);
 }
Exemple #9
0
        //------------------------------------------------------------------------------------------------------
        public override void CalculateLayoutInputHorizontal()
        {
            base.CalculateLayoutInputHorizontal();

            int columns = GetColumnCount();
            int rows    = GetRowCount();

            // Calc it based on all cells in that column
            // Cycle through all cells and store max values for column
            colMinWidths       = new float[columns];
            colPreferredWidths = new float[columns];
//			colFlexibleWidths = new float[columns];
            for (int c = 0; c < columns; c++)
            {
                colMinWidths [c]       = 0;
                colPreferredWidths [c] = 0;
//				colFlexibleWidths [c] = 0;
            }

            for (int i = 0; i < rectChildren.Count; i++)
            {
                var child = rectChildren [i];
                int col   = GetCellColumn(i, columns, rows);
                colMinWidths[col]       = Mathf.Max(colMinWidths[col], LayoutUtility.GetMinWidth(child));
                colPreferredWidths[col] = Mathf.Max(colPreferredWidths[col], LayoutUtility.GetPreferredWidth(child));
//				colFlexibleWidths[col] = Mathf.Max (colFlexibleWidths[col], LayoutUtility.GetFlexibleWidth (child));
            }

            float totalMinWidth       = padding.horizontal;
            float totalPreferredWidth = padding.horizontal;

//			float totalFlexibleWidth = 0;
            for (int c = 0; c < columns; c++)
            {
                totalMinWidth       += colMinWidths [c] + spacing.x;
                totalPreferredWidth += colPreferredWidths [c] + spacing.x;
//				totalFlexibleWidth += colFlexibleWidths [c] + spacing.x;
            }
            totalMinWidth       -= spacing.x;
            totalPreferredWidth -= spacing.x;
//			totalFlexibleWidth -= spacing.x;

            SetLayoutInputForAxis(totalMinWidth, totalPreferredWidth, -1, 0);

            float prefW      = LayoutUtility.GetPreferredWidth(rectTransform);
            float extraSpace = prefW - totalPreferredWidth;

            // Stretch if there is a layout element specifying extra space
            if (extraSpace > 0)
            {
                // Give extra space equally
                bool[] expandColumn    = new bool[columns];
                int    columnsToExpand = 0;

                for (int c = 0; c < columns; c++)
                {
                    expandColumn [c] = false;
                    for (int r = 0; r < rows; r++)
                    {
                        int index = GetCellIndexAtGridRef(c, r, columns, rows);
                        if (index < rectChildren.Count)
                        {
                            var child = rectChildren [index];
                            var cell  = child.GetComponent <VGridCell> ();
                            if (cell == null || cell.doNotExpandWidth == false)
                            {
                                expandColumn [c] = true;
                                columnsToExpand++;
                                break;
                            }
                        }
                    }
                }

                for (int c = 0; c < columns; c++)
                {
                    if (expandColumn[c])
                    {
                        colPreferredWidths [c] += extraSpace / columnsToExpand;
                    }
                }
            }

//			Debug.Log (string.Format ("min {0} pref {1}", LayoutUtility.GetMinWidth(rectTransform), LayoutUtility.GetPreferredWidth(rectTransform)));
        }