using UnityEngine; using UnityEngine.UI; using System.Collections; using System; namespace UIWidgets { /// /// Layout bridge to Horizontal or Vertical Layout Group. /// public class StandardLayoutBridge : ILayoutBridge { bool isHorizontal; /// /// Gets or sets a value indicating whether this instance is horizontal. /// /// true if this instance is horizontal; otherwise, false. public bool IsHorizontal { get { return isHorizontal; } set { throw new NotSupportedException("HorizontalLayoutGroup Or VerticalLayoutGroup direction cannot be change in runtime."); } } /// /// Gets or sets a value indicating whether this update content size fitter. /// /// true if update content size fitter; otherwise, false. public bool UpdateContentSizeFitter { get; set; } HorizontalOrVerticalLayoutGroup Layout; RectTransform DefaultItem; LayoutElement FirstFiller; LayoutElement LastFiller; ContentSizeFitter fitter; /// /// Initializes a new instance of the class. /// /// Layout. /// Default item. /// Update ContentSizeFitter on direction change. public StandardLayoutBridge(HorizontalOrVerticalLayoutGroup layout, RectTransform defaultItem, bool updateContentSizeFitter=true) { Utilites.UpdateLayout(layout); Layout = layout; DefaultItem = defaultItem; UpdateContentSizeFitter = updateContentSizeFitter; isHorizontal = layout is HorizontalLayoutGroup; var firstFillerGO = new GameObject("FirstFiller"); var firstFillerTransform = firstFillerGO.transform as RectTransform; if (firstFillerTransform==null) { firstFillerTransform = firstFillerGO.AddComponent(); } firstFillerTransform.SetParent(Layout.transform, false); firstFillerTransform.localScale = Vector3.one; FirstFiller = firstFillerGO.AddComponent(); var lastFillerGO = new GameObject("LastFiller"); var lastFillerTransform = lastFillerGO.transform as RectTransform; if (lastFillerTransform==null) { lastFillerTransform = lastFillerGO.AddComponent(); } lastFillerTransform.SetParent(Layout.transform, false); lastFillerTransform.localScale = Vector3.one; LastFiller = lastFillerGO.AddComponent(); var size = GetItemSize(); if (IsHorizontal) { firstFillerTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y); lastFillerTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y); } else { firstFillerTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x); lastFillerTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x); } fitter = Layout.GetComponent(); } /// /// Updates the layout. /// public void UpdateLayout() { Utilites.UpdateLayout(Layout); if (fitter!=null) { fitter.SetLayoutHorizontal(); fitter.SetLayoutVertical(); } } /// /// Sets the filler. /// /// First. /// Last. public void SetFiller(float first, float last) { if (FirstFiller!=null) { if (first==0f) { FirstFiller.gameObject.SetActive(false); } else { FirstFiller.gameObject.SetActive(true); FirstFiller.transform.SetAsFirstSibling(); if (IsHorizontal) { FirstFiller.preferredWidth = first; } else { FirstFiller.preferredHeight = first; } } } if (LastFiller!=null) { if (last==0f) { LastFiller.gameObject.SetActive(false); } else { LastFiller.gameObject.SetActive(true); if (IsHorizontal) { LastFiller.preferredWidth = last; } else { LastFiller.preferredHeight = last; } LastFiller.transform.SetAsLastSibling(); } } } /// /// Gets the size of the item. /// /// The item size. public Vector2 GetItemSize() { return new Vector2(LayoutUtility.GetPreferredWidth(DefaultItem), LayoutUtility.GetPreferredHeight(DefaultItem)); } /// /// Gets the top or left margin. /// /// The margin. public float GetMargin() { return IsHorizontal ? Layout.padding.left : Layout.padding.top; } /// /// Gets the full margin. /// /// The full margin. public float GetFullMargin() { return IsHorizontal ? Layout.padding.horizontal : Layout.padding.vertical; } /// /// Gets the spacing. /// /// The spacing. public float GetSpacing() { return Layout.spacing; } } }