Esempio n. 1
0
        /// <summary>
        /// Update item size.
        /// </summary>
        /// <param name="item">Item.</param>
        protected static void UpdateItemSize(AccordionItem item)
        {
            LayoutUtilites.UpdateLayoutsRecursive(item.ContentObjectRect.transform);

            item.ContentObjectWidth = LayoutUtilites.IsWidthControlled(item.ContentObjectRect)
                                ? LayoutUtility.GetPreferredWidth(item.ContentObjectRect)
                                : item.ContentObjectRect.rect.width;

            item.ContentObjectHeight = LayoutUtilites.IsHeightControlled(item.ContentObjectRect)
                                ? LayoutUtility.GetPreferredHeight(item.ContentObjectRect)
                                : item.ContentObjectRect.rect.height;
        }
Esempio n. 2
0
        /// <summary>
        /// Collapse animation.
        /// </summary>
        /// <returns>Animation.</returns>
        /// <param name="rectTransform">RectTransform.</param>
        /// <param name="time">Time.</param>
        /// <param name="isHorizontal">Is Horizontal animated width changes; otherwise height.</param>
        /// <param name="unscaledTime">Use unscaled time.</param>
        /// <param name="actionAfter">Action to run after animation.</param>
        public static IEnumerator Collapse(RectTransform rectTransform, float time = 0.5f, bool isHorizontal = false, bool unscaledTime = false, Action actionAfter = null)
        {
            var layoutElement = Utilites.GetOrAddComponent <LayoutElement>(rectTransform);
            var size          = isHorizontal
                                ? (LayoutUtilites.IsWidthControlled(rectTransform) ? LayoutUtility.GetPreferredWidth(rectTransform) : rectTransform.rect.width)
                                : (LayoutUtilites.IsHeightControlled(rectTransform) ? LayoutUtility.GetPreferredHeight(rectTransform) : rectTransform.rect.height);

            var end_time = Utilites.GetTime(unscaledTime) + time;

            while (Utilites.GetTime(unscaledTime) <= end_time)
            {
                var t        = 1 - ((end_time - Utilites.GetTime(unscaledTime)) / time);
                var new_size = Mathf.Lerp(size, 0, t);
                if (isHorizontal)
                {
                    rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, new_size);
                    layoutElement.preferredWidth = new_size;
                }
                else
                {
                    rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, new_size);
                    layoutElement.preferredHeight = new_size;
                }

                yield return(null);
            }

            // return size back for future use
            if (isHorizontal)
            {
                rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size);
                layoutElement.preferredWidth = size;
            }
            else
            {
                rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size);
                layoutElement.preferredHeight = size;
            }

            if (actionAfter != null)
            {
                actionAfter();
            }
        }