/// <summary> /// Shrink specified elements to fit. /// </summary> /// <param name="size">Size.</param> /// <param name="elements">Elements.</param> /// <param name="spacing">Spacing.</param> /// <param name="axis">Axis to fit.</param> protected static void ShrinkToFit(float size, List <List <LayoutElementInfo> > elements, float spacing, RectTransform.Axis axis) { var sizes = axis == RectTransform.Axis.Horizontal ? SizesInfo.GetWidths(elements) : SizesInfo.GetHeights(elements); var free_space = size - sizes.TotalPreferred - ((elements.Count - 1) * spacing); if (free_space > 0f) { return; } var per_flexible = free_space / sizes.TotalFlexible; var minPrefLerp = 0f; if (sizes.TotalMin != sizes.TotalPreferred) { minPrefLerp = Mathf.Clamp01((size - sizes.TotalMin - ((elements.Count - 1) * spacing)) / (sizes.TotalPreferred - sizes.TotalMin)); } for (int i = 0; i < elements.Count; i++) { var element_size = Mathf.Lerp(sizes.Sizes[i].Min, sizes.Sizes[i].Preferred, minPrefLerp) + (per_flexible * sizes.Sizes[i].Flexible); var row = elements[i]; for (int j = 0; j < row.Count; j++) { row[j].SetSize(axis, element_size); } } }
/// <summary> /// Gets the sizes info. /// </summary> /// <returns>The sizes info.</returns> /// <param name="sizes">Sizes.</param> public static SizesInfo GetSizesInfo(Size[] sizes) { var result = new SizesInfo() { Sizes = sizes }; for (int i = 0; i < sizes.Length; i++) { result.TotalMin += sizes[i].Min; result.TotalPreferred += sizes[i].Preferred; result.TotalFlexible += sizes[i].Flexible; } if (result.TotalFlexible == 0f) { for (int i = 0; i < sizes.Length; i++) { sizes[i].Flexible = 1f; } result.TotalFlexible += sizes.Length; } return(result); }
/// <summary> /// Determines whether the specified size is equal to the current size. /// </summary> /// <param name="other">The size to compare with the current size.</param> /// <returns><c>true</c> if the specified size is equal to the current size; otherwise, <c>false</c>.</returns> public bool Equals(SizesInfo other) { if (TotalMin != other.TotalMin) { return(false); } if (TotalPreferred != other.TotalPreferred) { return(false); } if (TotalFlexible != other.TotalFlexible) { return(false); } return(Sizes == other.Sizes); }