private void Awake()
 {
     rectTransform   = GetComponent <RectTransform>();
     layoutGroup     = GetComponent <LayoutGroup>();
     layoutGroupType = layoutGroup.GetType() == typeof(VerticalLayoutGroup)
         ? LayoutGroupType.Vertical
         : LayoutGroupType.Horizontal;
 }
        private void Update()
        {
            if (rectTransform == null)
            {
                rectTransform = GetComponent <RectTransform>();
            }

            if (layoutGroup == null)
            {
                layoutGroup     = GetComponent <LayoutGroup>();
                layoutGroupType = layoutGroup.GetType() == typeof(VerticalLayoutGroup)
                    ? LayoutGroupType.Vertical
                    : LayoutGroupType.Horizontal;
            }

            Transform containerTransform;
            var       childCount = (containerTransform = transform).childCount;

            if (childCount == 0)
            {
                return;
            }

            childCount = 0;
            for (var i = 0; i < containerTransform.childCount; i++)
            {
                if (containerTransform.GetChild(i).gameObject.activeInHierarchy)
                {
                    childCount++;
                }
            }

            var firstChildRectTransform = containerTransform.GetChild(0).GetComponent <RectTransform>();
            var childSizeDelta          = firstChildRectTransform.sizeDelta;

            var padding   = layoutGroup.padding;
            var sizeDelta = rectTransform.sizeDelta;

            switch (layoutGroupType)
            {
            case LayoutGroupType.Vertical:
                sizeDelta.y  = childCount * (childSizeDelta.y + padding.top + padding.bottom + spacing);
                sizeDelta.y *= 0.5f;
                break;

            case LayoutGroupType.Horizontal:
                sizeDelta.x = childCount * (childSizeDelta.x + padding.left + padding.right);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            rectTransform.sizeDelta = sizeDelta;
        }
Example #3
0
 void Start()
 {
     layoutGroup   = GetComponent <LayoutGroup>();
     rectTransform = GetComponent <RectTransform>();
     // Save type of layout group so it does not have to be checked again later
     if (layoutGroup.GetType() == typeof(HorizontalLayoutGroup))
     {
         layoutGroupType       = LayoutGroupType.Horizontal;
         horizontalLayoutGroup = (HorizontalLayoutGroup)layoutGroup;
     }
     else if (layoutGroup.GetType() == typeof(VerticalLayoutGroup))
     {
         layoutGroupType     = LayoutGroupType.Vertical;
         verticalLayoutGroup = (VerticalLayoutGroup)layoutGroup;
     }
     else if (layoutGroup.GetType() == typeof(GridLayoutGroup))
     {
         layoutGroupType = LayoutGroupType.Grid;
         gridLayoutGroup = (GridLayoutGroup)layoutGroup;
     }
 }
        public void calculateLayout(LayoutGroup layout)
        {
            m_Padding = layout.padding != null ? layout.padding : new RectOffset();
            if (layout.GetType() == typeof(HorizontalLayoutGroup))
            {
                m_Spacing.x = ((HorizontalLayoutGroup)layout).spacing;
                m_Spacing.y = 0;
                this.onHorizontalLayoutGroup((HorizontalLayoutGroup)layout);
            }
            else if (layout.GetType() == typeof(VerticalLayoutGroup))
            {
                m_Spacing.x = 0;
                m_Spacing.y = ((VerticalLayoutGroup)layout).spacing;
                this.onVerticalLayoutGroup((VerticalLayoutGroup)layout);
            }
            else if (layout.GetType() == typeof(GridLayoutGroup))
            {
                m_Spacing = ((GridLayoutGroup)layout).spacing;
                this.onGridLayoutGroup((GridLayoutGroup)layout);
            }

            this.init();
        }
Example #5
0
        public UnityLayout(LayoutGroup p_layout)
        {
            m_transform = p_layout.transform;

#if UNITY_EDITOR
            if (m_transform.GetComponent <ContentSizeFitter>() == null)
            {
                Debug.LogWarning(String.Format("The {0} on '{1}' does not have a ContentSizeFitter component attached. You might want to check on that.", p_layout.GetType().Name, m_transform.name));
            }
#endif
        }