namespace EasyLayoutNS { using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Serialization; using UnityEngine.UI; /// /// Allow to control the maximum preferred sizes of the LayoutGroup. /// [RequireComponent(typeof(LayoutGroup))] public class LayoutElementMax : UIBehaviour, ILayoutElement, ILayoutIgnorer { LayoutGroup layoutGroup; /// /// Layout Group. /// protected LayoutGroup Layout { get { if (layoutGroup == null) { layoutGroup = GetComponent(); } return layoutGroup; } } [SerializeField] [FormerlySerializedAs("_ignoreLayout")] bool pignoreLayout = false; [SerializeField] [FormerlySerializedAs("_layoutPriority")] int playoutPriority = 1; [SerializeField] float maxWidth = -1f; [SerializeField] float maxHeight = -1f; /// /// Maximum preferred height for the Layout Group. /// public virtual float MaxHeight { get { return maxHeight; } set { if (maxHeight != value) { maxHeight = value; SetDirty(); } } } /// /// Maximum preferred width for the Layout Group. /// public virtual float MaxWidth { get { return maxWidth; } set { if (maxWidth != value) { maxWidth = value; SetDirty(); } } } /// /// Should this RectTransform be ignored by the layout system? /// /// /// Setting this property to true will make a parent layout group component not consider this RectTransform part of the group. The RectTransform can then be manually positioned despite being a child GameObject of a layout group. /// public bool ignoreLayout { get { return pignoreLayout; } set { if (pignoreLayout != value) { pignoreLayout = value; SetDirty(); } } } /// /// The Priority of layout this element has. /// public int layoutPriority { get { return playoutPriority; } set { playoutPriority = value; } } /// /// The minimum height this layout element may be allocated. /// public float minHeight { get { return -1; } } /// /// The minimum width this layout element may be allocated. /// public float minWidth { get { return -1; } } /// /// The preferred width this layout element should be allocated if there is sufficient space. The preferredWidth can be set to -1 to remove the size. /// public float preferredHeight { get { return Mathf.Min(Layout.preferredHeight, MaxHeight); } } /// /// The preferred width this layout element should be allocated if there is sufficient space. The preferredWidth can be set to -1 to remove the size. /// public float preferredWidth { get { return Mathf.Min(Layout.preferredWidth, MaxWidth); } } /// /// The extra relative height this layout element should be allocated if there is additional available space. /// public float flexibleHeight { get { return -1; } } /// /// The extra relative width this layout element should be allocated if there is additional available space. /// public float flexibleWidth { get { return -1; } } /// /// Calculates the layout input horizontal. /// public void CalculateLayoutInputHorizontal() { } /// /// Calculates the layout input vertical. /// public void CalculateLayoutInputVertical() { } /// /// Process instance enable event. /// protected override void OnEnable() { base.OnEnable(); SetDirty(); } /// /// Process transform parent changed event. /// protected override void OnTransformParentChanged() { SetDirty(); } /// /// Process instance disable event. /// protected override void OnDisable() { SetDirty(); base.OnDisable(); } /// /// Process property animation event. /// protected override void OnDidApplyAnimationProperties() { SetDirty(); } /// /// Process before transform parent changed event. /// protected override void OnBeforeTransformParentChanged() { SetDirty(); } /// /// Mark the LayoutElement as dirty. /// /// /// This will make the auto layout system process this element on the next layout pass. This method should be called by the LayoutElement whenever a change is made that potentially affects the layout. /// protected void SetDirty() { if (!IsActive()) { return; } LayoutRebuilder.MarkLayoutForRebuild(transform as RectTransform); } #if UNITY_EDITOR /// /// Process validation event. /// protected override void OnValidate() { SetDirty(); } #endif } }