namespace EasyLayoutNS { using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// /// LayoutElementInfo. /// Correctly works with multiple resizes during one frame. /// public class LayoutElementInfo { /// /// GameObject active state. /// public bool Active; /// /// RectTransform. /// public RectTransform Rect { get; protected set; } /// /// Width. /// public float Width { get; protected set; } /// /// Height. /// public float Height { get; protected set; } /// /// Size on main axis. /// public float AxisSize { get { return Layout.IsHorizontal ? Width : Height; } } /// /// Size on sub axis. /// public float SubAxisSize { get { return Layout.IsHorizontal ? Height : Width; } } /// /// Minimal width. /// public float MinWidth { get; protected set; } /// /// Minimal height. /// public float MinHeight { get; protected set; } /// /// Preferred width. /// public float PreferredWidth { get; protected set; } /// /// Preferred height. /// public float PreferredHeight { get; protected set; } /// /// Flexible width. /// public float FlexibleWidth { get; protected set; } /// /// Flexible height. /// public float FlexibleHeight { get; protected set; } /// /// Current layout. /// protected EasyLayout Layout; /// /// Scale. /// protected Vector3 Scale; /// /// Is width changed? /// public bool ChangedWidth { get; protected set; } /// /// New width. /// protected float newWidth; /// /// New width. /// public float NewWidth { get { return newWidth; } set { if (newWidth != value) { newWidth = value; Width = value * Scale.x; ChangedWidth = true; } } } /// /// Is height changed? /// public bool ChangedHeight { get; protected set; } /// /// New height. /// protected float newHeight; /// /// New height. /// public float NewHeight { get { return newHeight; } set { if (newHeight != value) { newHeight = value; Height = value * Scale.y; ChangedHeight = true; } } } /// /// Is pivot changed? /// public bool ChangedPivot { get; protected set; } /// /// Pivot. /// public Vector2 Pivot { get; protected set; } /// /// New pivot. /// protected Vector2 newPivot; /// /// New pivot. /// public Vector2 NewPivot { get { return newPivot; } set { if (newPivot != value) { newPivot = value; Pivot = value; ChangedPivot = true; } } } /// /// Position of the top left corner. /// public Vector2 PositionTopLeft { get { return new Vector2( PositionPivot.x - (Width * NewPivot.x), PositionPivot.y + (Height * (1f - NewPivot.y))); } set { PositionPivot = new Vector2( value.x + (Width * NewPivot.x), value.y - (Height * (1f - NewPivot.y))); } } /// /// Position with pivot. /// public Vector2 PositionPivot; /// /// Is position changed? /// public bool IsPositionChanged { get { var new_pos = PositionPivot; return (Rect.localPosition.x != new_pos.x) || (Rect.localPosition.y != new_pos.y); } } /// /// Is rotation changed? /// public bool ChangedRotation { get; protected set; } /// /// New EulerAngles. /// protected Vector3 newEulerAngles = Vector3.zero; /// /// New EulerAngles.Z. /// public float NewEulerAnglesZ { get { return newEulerAngles.z; } set { if (newEulerAngles.z != value) { newEulerAngles.z = value; ChangedRotation = true; } } } /// /// New EulerAngles. /// public Vector3 NewEulerAngles { get { return newEulerAngles; } set { if (newEulerAngles != value) { newEulerAngles = value; ChangedRotation = true; } } } /// /// Row. /// public int Row; /// /// Column. /// public int Column; /// /// Set element. /// /// RectTransform. /// Is gameobject active? /// Current layout. public void SetElement(RectTransform rectTransform, bool active, EasyLayout layout) { Rect = rectTransform; Active = active; Layout = layout; Scale = rectTransform.localScale; Width = rectTransform.rect.width * Scale.x; Height = rectTransform.rect.height * Scale.y; Pivot = rectTransform.pivot; newWidth = rectTransform.rect.width; newHeight = rectTransform.rect.height; newPivot = rectTransform.pivot; newEulerAngles = rectTransform.localEulerAngles; ChangedWidth = false; ChangedHeight = false; if (Layout.ChildrenWidth != ChildrenSize.DoNothing || Layout.ChildrenHeight != ChildrenSize.DoNothing) { RefreshLayoutElements(); } var widths = GetWidthValues(); MinWidth = widths.Min; PreferredWidth = widths.Preferred; FlexibleWidth = widths.Flexible; var heights = GetHeightValues(); MinHeight = heights.Min; PreferredHeight = heights.Preferred; FlexibleHeight = heights.Flexible; Row = -1; Column = -1; } /// /// Get widths values. /// /// Widths values. protected Size GetWidthValues() { if (Rect == null) { return default(Size); } if (Rect.gameObject.activeInHierarchy) { return new Size() { Min = Mathf.Max(0f, LayoutUtility.GetMinWidth(Rect)), Preferred = Mathf.Max(0f, LayoutUtility.GetPreferredWidth(Rect)), Flexible = Mathf.Max(0f, LayoutUtility.GetFlexibleWidth(Rect)), }; } return GetLayoutWidths(); } /// /// Get heights values. /// /// Height values. protected Size GetHeightValues() { if (Rect == null) { return default(Size); } if (Rect.gameObject.activeInHierarchy) { return new Size() { Min = Mathf.Max(0f, LayoutUtility.GetMinHeight(Rect)), Preferred = Mathf.Max(0f, LayoutUtility.GetPreferredHeight(Rect)), Flexible = Mathf.Max(0f, LayoutUtility.GetFlexibleHeight(Rect)), }; } return GetLayoutHeights(); } /// /// Set size. /// /// Axis. /// New size. public void SetSize(RectTransform.Axis axis, float size) { if (axis == RectTransform.Axis.Horizontal) { NewWidth = size; } else { NewHeight = size; } } #if UNITY_4_6 || UNITY_4_7 /// /// Components list. /// protected List Components = new List(); #endif /// /// All ILayoutElements Of current gameobject. /// protected List LayoutElements = new List(); /// /// Get widths from LayoutElements. /// /// Widths values. protected Size GetLayoutWidths() { var max_priority = MaxPriority(LayoutElements); var result = default(Size); foreach (var elem in LayoutElements) { if (elem.layoutPriority == max_priority) { result.Min = Mathf.Max(result.Min, elem.minWidth); result.Preferred = Mathf.Max(result.Preferred, Mathf.Max(elem.preferredWidth, elem.minWidth)); result.Flexible = Mathf.Max(result.Flexible, elem.flexibleWidth); } } return result; } /// /// Refresh LayoutElements list. /// protected void RefreshLayoutElements() { LayoutElements.Clear(); #if UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER Rect.GetComponents(LayoutElements); #else Components.Clear(); Rect.GetComponents(typeof(ILayoutElement), Components); for (int i = 0; i < Components.Count; i++) { LayoutElements.Add(Components[i] as ILayoutElement); } #endif } /// /// Get heights from LayoutElements. /// /// Heights values. protected Size GetLayoutHeights() { var max_priority = MaxPriority(LayoutElements); var result = default(Size); for (int i = 0; i < LayoutElements.Count; i++) { var elem = LayoutElements[i]; if (elem.layoutPriority == max_priority) { result.Min = Mathf.Max(result.Min, elem.minHeight); result.Preferred = Mathf.Max(result.Preferred, Mathf.Max(elem.preferredHeight, elem.minHeight)); result.Flexible = Mathf.Max(result.Flexible, elem.flexibleHeight); } } return result; } /// /// Get maximum priority from LayoutElements. /// /// LayoutElements list. /// Maximum priority. protected static int MaxPriority(List elements) { if (elements.Count == 0) { return 0; } int result = elements[0].layoutPriority; for (int i = 1; i < elements.Count; i++) { result = Mathf.Max(result, elements[i].layoutPriority); } return result; } } }