protected override float GetSize(RectTransform item)
        {
            if (ConstantSize && _cachedSize > 0f)
            {
                return(_cachedSize);
            }

            float size = contentSpacing;

            if (m_GridLayout != null)
            {
                size += m_GridLayout.cellSize.y;
            }
            else
            {
                // For some reason, this f***s up for my kind of objects
                if (UseLayoutSize)
                {
                    size += LayoutUtility.GetPreferredHeight(item);
                }
                else
                {
                    size += item.sizeDelta.y;
                }
                //Debug.Log("Size of " + item.gameObject.name + " is " + size);
            }
            _cachedSize = size;
            return(size);
        }
 float GetCellSize(RectTransform item)
 {
     if (mLayout != null)
     {
         return(mLayout.cellSize.y);
     }
     return(LayoutUtility.GetPreferredHeight(item));
 }
 /// <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));
 }
Beispiel #4
0
        //------------------------------------------------------------------------------------------------------
        public override void CalculateLayoutInputVertical()
        {
            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
            rowMinHeights       = new float[rows];
            rowPreferredHeights = new float[rows];
//			rowFlexibleHeights = new float[rows];
            for (int r = 0; r < rows; r++)
            {
                rowMinHeights [r]       = 0;
                rowPreferredHeights [r] = 0;
//				rowFlexibleHeights [r] = 0;
            }

            for (int i = 0; i < rectChildren.Count; i++)
            {
                var child = rectChildren [i];
                int row   = GetCellRow(i, columns, rows);
                rowMinHeights[row]       = Mathf.Max(rowMinHeights[row], LayoutUtility.GetMinHeight(child));
                rowPreferredHeights[row] = Mathf.Max(rowPreferredHeights[row], LayoutUtility.GetPreferredHeight(child));
//				rowFlexibleHeights[row] = Mathf.Max (rowFlexibleHeights[row], LayoutUtility.GetFlexibleHeight (child));
            }

            float totalMinHeight       = padding.vertical;
            float totalPreferredHeight = padding.vertical;

            //			float totalFlexibleHeight = 0;
            for (int r = 0; r < rows; r++)
            {
                totalMinHeight       += rowMinHeights [r] + spacing.y;
                totalPreferredHeight += rowPreferredHeights [r] + spacing.y;
                //				totalFlexibleHeight += rowFlexibleHeights [r] + spacing.y;
            }
            totalMinHeight       -= spacing.y;
            totalPreferredHeight -= spacing.y;
            //			totalFlexibleHeight -= spacing.y;

            SetLayoutInputForAxis(totalMinHeight, totalPreferredHeight, -1, 1);

            float prefH      = LayoutUtility.GetPreferredHeight(rectTransform);
            float extraSpace = prefH - totalPreferredHeight;

            // Stretch if there is a layout element specifying extra space
            if (extraSpace > 0)
            {
                // Give extra space equally
                for (int r = 0; r < rows; r++)
                {
                    rowPreferredHeights [r] += extraSpace / rows;
                }
            }
        }
Beispiel #5
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);
     }
 }
        //------------------------------------------------------------------------------------------------------
        public override void CalculateLayoutInputVertical()
        {
            float totalMinHeight       = padding.vertical;
            float totalPreferredHeight = padding.vertical + totalRowHeight + spacing.y * (rows - 1);

            SetLayoutInputForAxis(totalMinHeight, totalPreferredHeight, -1, 1);

            // Stretch if there is a layout element specifying extra space and child force expand height is on
            float extraHeight = LayoutUtility.GetPreferredHeight(rectTransform) - totalPreferredHeight;

            if (extraHeight > 0 && childForceExpandHeight)
            {
                // Don't expand column if all cells in column override expansion to false
                bool[] expandRow    = new bool[rows];
                int    rowsToExpand = 0;
                for (int r = 0; r < rows; r++)
                {
                    expandRow [r] = false;
                    for (int c = 0; c < columns; c++)
                    {
                        int index = GetCellIndexAtGridRef(c, r);
                        if (index >= 0 && index < rectChildren.Count)
                        {
                            var child       = rectChildren [index];
                            var cellOptions = child.GetComponent <VariableGridCell> ();
                            if (cellOptions == null || !cellOptions.overrideForceExpandHeight || cellOptions.forceExpandHeight)
                            {
                                expandRow [r] = true;
                                rowsToExpand++;
                                break;
                            }
                        }
                        else
                        {
                            expandRow [r] = true;
                            rowsToExpand++;
                            break;
                        }
                    }
                }

                // Give extra space equally
                for (int r = 0; r < rows; r++)
                {
                    if (expandRow [r])
                    {
                        rowHeights [r] += extraHeight / rowsToExpand;
                    }
                }
            }
        }
Beispiel #7
0
        protected override float GetSize(RectTransform item)
        {
            float size = contentSpacing;

            if (m_GridLayout != null)
            {
                size += m_GridLayout.cellSize.y;
            }
            else
            {
                size += LayoutUtility.GetPreferredHeight(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);
        }
        protected float GetExpandedHeight()
        {
            if (this.m_LayoutElement == null)
            {
                return(this.m_MinHeight);
            }

            float originalPrefH = this.m_LayoutElement.preferredHeight;

            this.m_LayoutElement.preferredHeight = -1f;
            float h = LayoutUtility.GetPreferredHeight(this.m_RectTransform);

            this.m_LayoutElement.preferredHeight = originalPrefH;

            return(h);
        }
        protected float GetExpandedHeight()
        {
            if (this.m_LayoutElement == null)
            {
                return(this.m_MinHeight);
            }

            float originalPrefH = this.m_LayoutElement.preferredHeight;

            this.m_LayoutElement.preferredHeight = -1f;
            //The RectTransform's sizeDelta is updated at the second frame by default.If you don't force it to update,you'll get (0,0) at the first frame(On the Start)
            LayoutRebuilder.ForceRebuildLayoutImmediate(this.m_RectTransform);
            float h = LayoutUtility.GetPreferredHeight(this.m_RectTransform);

            this.m_LayoutElement.preferredHeight = originalPrefH;

            return(h);
        }
Beispiel #11
0
        protected override float GetSize(RectTransform item)
        {
            float size = contentSpacing;

            if (m_GridLayout != null)
            {
                size += m_GridLayout.cellSize.y;
            }
            else
            {
                // 计算item高度
                if (item.gameObject.activeInHierarchy && item.gameObject.activeSelf)
                {
                    size += LayoutUtility.GetPreferredHeight(item);
                }
                // item不活跃状态下计算高度会始终返回0,这里直接返回item高度
                else
                {
                    size += item.sizeDelta.y;
                }
            }
            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];
            }
        }
Beispiel #13
0
 protected override float GetSize(RectTransform item)
 {
     return(LayoutUtility.GetPreferredHeight(item) + base.contentSpacing);
 }