using System.Collections; using System.Collections.Generic; using UnityEngine.EventSystems; using UnityEngine.Pool; namespace UnityEngine.UI { [DisallowMultipleComponent] [ExecuteAlways] [RequireComponent(typeof(RectTransform))] /// /// Abstract base class to use for layout groups. /// public abstract class LayoutGroup : UIBehaviour, ILayoutElement, ILayoutGroup { [SerializeField] protected RectOffset m_Padding = new RectOffset(); /// /// The padding to add around the child layout elements. /// public RectOffset padding { get { return m_Padding; } set { SetProperty(ref m_Padding, value); } } [SerializeField] protected TextAnchor m_ChildAlignment = TextAnchor.UpperLeft; /// /// The alignment to use for the child layout elements in the layout group. /// /// /// If a layout element does not specify a flexible width or height, its child elements many not use the available space within the layout group. In this case, use the alignment settings to specify how to align child elements within their layout group. /// public TextAnchor childAlignment { get { return m_ChildAlignment; } set { SetProperty(ref m_ChildAlignment, value); } } [System.NonSerialized] private RectTransform m_Rect; protected RectTransform rectTransform { get { if (m_Rect == null) m_Rect = GetComponent(); return m_Rect; } } protected DrivenRectTransformTracker m_Tracker; private Vector2 m_TotalMinSize = Vector2.zero; private Vector2 m_TotalPreferredSize = Vector2.zero; private Vector2 m_TotalFlexibleSize = Vector2.zero; [System.NonSerialized] private List m_RectChildren = new List(); protected List rectChildren { get { return m_RectChildren; } } public virtual void CalculateLayoutInputHorizontal() { m_RectChildren.Clear(); var toIgnoreList = ListPool.Get(); for (int i = 0; i < rectTransform.childCount; i++) { var rect = rectTransform.GetChild(i) as RectTransform; if (rect == null || !rect.gameObject.activeInHierarchy) continue; rect.GetComponents(typeof(ILayoutIgnorer), toIgnoreList); if (toIgnoreList.Count == 0) { m_RectChildren.Add(rect); continue; } for (int j = 0; j < toIgnoreList.Count; j++) { var ignorer = (ILayoutIgnorer)toIgnoreList[j]; if (!ignorer.ignoreLayout) { m_RectChildren.Add(rect); break; } } } ListPool.Release(toIgnoreList); m_Tracker.Clear(); } public abstract void CalculateLayoutInputVertical(); /// /// See LayoutElement.minWidth /// public virtual float minWidth { get { return GetTotalMinSize(0); } } /// /// See LayoutElement.preferredWidth /// public virtual float preferredWidth { get { return GetTotalPreferredSize(0); } } /// /// See LayoutElement.flexibleWidth /// public virtual float flexibleWidth { get { return GetTotalFlexibleSize(0); } } /// /// See LayoutElement.minHeight /// public virtual float minHeight { get { return GetTotalMinSize(1); } } /// /// See LayoutElement.preferredHeight /// public virtual float preferredHeight { get { return GetTotalPreferredSize(1); } } /// /// See LayoutElement.flexibleHeight /// public virtual float flexibleHeight { get { return GetTotalFlexibleSize(1); } } /// /// See LayoutElement.layoutPriority /// public virtual int layoutPriority { get { return 0; } } // ILayoutController Interface public abstract void SetLayoutHorizontal(); public abstract void SetLayoutVertical(); // Implementation protected LayoutGroup() { if (m_Padding == null) m_Padding = new RectOffset(); } protected override void OnEnable() { base.OnEnable(); SetDirty(); } protected override void OnDisable() { m_Tracker.Clear(); LayoutRebuilder.MarkLayoutForRebuild(rectTransform); base.OnDisable(); } /// /// Callback for when properties have been changed by animation. /// protected override void OnDidApplyAnimationProperties() { SetDirty(); } /// /// The min size for the layout group on the given axis. /// /// The axis index. 0 is horizontal and 1 is vertical. /// The min size protected float GetTotalMinSize(int axis) { return m_TotalMinSize[axis]; } /// /// The preferred size for the layout group on the given axis. /// /// The axis index. 0 is horizontal and 1 is vertical. /// The preferred size. protected float GetTotalPreferredSize(int axis) { return m_TotalPreferredSize[axis]; } /// /// The flexible size for the layout group on the given axis. /// /// The axis index. 0 is horizontal and 1 is vertical. /// The flexible size protected float GetTotalFlexibleSize(int axis) { return m_TotalFlexibleSize[axis]; } /// /// Returns the calculated position of the first child layout element along the given axis. /// /// The axis index. 0 is horizontal and 1 is vertical. /// The total space required on the given axis for all the layout elements including spacing and excluding padding. /// The position of the first child along the given axis. protected float GetStartOffset(int axis, float requiredSpaceWithoutPadding) { float requiredSpace = requiredSpaceWithoutPadding + (axis == 0 ? padding.horizontal : padding.vertical); float availableSpace = rectTransform.rect.size[axis]; float surplusSpace = availableSpace - requiredSpace; float alignmentOnAxis = GetAlignmentOnAxis(axis); return (axis == 0 ? padding.left : padding.top) + surplusSpace * alignmentOnAxis; } /// /// Returns the alignment on the specified axis as a fraction where 0 is left/top, 0.5 is middle, and 1 is right/bottom. /// /// The axis to get alignment along. 0 is horizontal and 1 is vertical. /// The alignment as a fraction where 0 is left/top, 0.5 is middle, and 1 is right/bottom. protected float GetAlignmentOnAxis(int axis) { if (axis == 0) return ((int)childAlignment % 3) * 0.5f; else return ((int)childAlignment / 3) * 0.5f; } /// /// Used to set the calculated layout properties for the given axis. /// /// The min size for the layout group. /// The preferred size for the layout group. /// The flexible size for the layout group. /// The axis to set sizes for. 0 is horizontal and 1 is vertical. protected void SetLayoutInputForAxis(float totalMin, float totalPreferred, float totalFlexible, int axis) { m_TotalMinSize[axis] = totalMin; m_TotalPreferredSize[axis] = totalPreferred; m_TotalFlexibleSize[axis] = totalFlexible; } /// /// Set the position and size of a child layout element along the given axis. /// /// The RectTransform of the child layout element. /// The axis to set the position and size along. 0 is horizontal and 1 is vertical. /// The position from the left side or top. protected void SetChildAlongAxis(RectTransform rect, int axis, float pos) { if (rect == null) return; SetChildAlongAxisWithScale(rect, axis, pos, 1.0f); } /// /// Set the position and size of a child layout element along the given axis. /// /// The RectTransform of the child layout element. /// The axis to set the position and size along. 0 is horizontal and 1 is vertical. /// The position from the left side or top. protected void SetChildAlongAxisWithScale(RectTransform rect, int axis, float pos, float scaleFactor) { if (rect == null) return; m_Tracker.Add(this, rect, DrivenTransformProperties.Anchors | (axis == 0 ? DrivenTransformProperties.AnchoredPositionX : DrivenTransformProperties.AnchoredPositionY)); // Inlined rect.SetInsetAndSizeFromParentEdge(...) and refactored code in order to multiply desired size by scaleFactor. // sizeDelta must stay the same but the size used in the calculation of the position must be scaled by the scaleFactor. rect.anchorMin = Vector2.up; rect.anchorMax = Vector2.up; Vector2 anchoredPosition = rect.anchoredPosition; anchoredPosition[axis] = (axis == 0) ? (pos + rect.sizeDelta[axis] * rect.pivot[axis] * scaleFactor) : (-pos - rect.sizeDelta[axis] * (1f - rect.pivot[axis]) * scaleFactor); rect.anchoredPosition = anchoredPosition; } /// /// Set the position and size of a child layout element along the given axis. /// /// The RectTransform of the child layout element. /// The axis to set the position and size along. 0 is horizontal and 1 is vertical. /// The position from the left side or top. /// The size. protected void SetChildAlongAxis(RectTransform rect, int axis, float pos, float size) { if (rect == null) return; SetChildAlongAxisWithScale(rect, axis, pos, size, 1.0f); } /// /// Set the position and size of a child layout element along the given axis. /// /// The RectTransform of the child layout element. /// The axis to set the position and size along. 0 is horizontal and 1 is vertical. /// The position from the left side or top. /// The size. protected void SetChildAlongAxisWithScale(RectTransform rect, int axis, float pos, float size, float scaleFactor) { if (rect == null) return; m_Tracker.Add(this, rect, DrivenTransformProperties.Anchors | (axis == 0 ? (DrivenTransformProperties.AnchoredPositionX | DrivenTransformProperties.SizeDeltaX) : (DrivenTransformProperties.AnchoredPositionY | DrivenTransformProperties.SizeDeltaY) ) ); // Inlined rect.SetInsetAndSizeFromParentEdge(...) and refactored code in order to multiply desired size by scaleFactor. // sizeDelta must stay the same but the size used in the calculation of the position must be scaled by the scaleFactor. rect.anchorMin = Vector2.up; rect.anchorMax = Vector2.up; Vector2 sizeDelta = rect.sizeDelta; sizeDelta[axis] = size; rect.sizeDelta = sizeDelta; Vector2 anchoredPosition = rect.anchoredPosition; anchoredPosition[axis] = (axis == 0) ? (pos + size * rect.pivot[axis] * scaleFactor) : (-pos - size * (1f - rect.pivot[axis]) * scaleFactor); rect.anchoredPosition = anchoredPosition; } private bool isRootLayoutGroup { get { Transform parent = transform.parent; if (parent == null) return true; return transform.parent.GetComponent(typeof(ILayoutGroup)) == null; } } protected override void OnRectTransformDimensionsChange() { base.OnRectTransformDimensionsChange(); if (isRootLayoutGroup) SetDirty(); } protected virtual void OnTransformChildrenChanged() { SetDirty(); } /// /// Helper method used to set a given property if it has changed. /// /// A reference to the member value. /// The new value. protected void SetProperty(ref T currentValue, T newValue) { if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue))) return; currentValue = newValue; SetDirty(); } /// /// Mark the LayoutGroup as dirty. /// protected void SetDirty() { if (!IsActive()) return; if (!CanvasUpdateRegistry.IsRebuildingLayout()) LayoutRebuilder.MarkLayoutForRebuild(rectTransform); else StartCoroutine(DelayedSetDirty(rectTransform)); } IEnumerator DelayedSetDirty(RectTransform rectTransform) { yield return null; LayoutRebuilder.MarkLayoutForRebuild(rectTransform); } #if UNITY_EDITOR protected override void OnValidate() { SetDirty(); } #endif } }