Example #1
0
        /// <summary>
        /// Cache some values for faster reference later
        /// </summary>
        void Awake()
        {
            _scrollRectTransform = scrollRect.GetComponent <RectTransform>();
            _scrollRectMask      = scrollRect.GetComponent <UnityEngine.UI.Mask>();
            // TODO: Remove
            //_scrollBarRectTransform = scrollbar.GetComponent<RectTransform>();
            //_originalScrollBarSize = _scrollBarRectTransform.sizeDelta;
            _lastScrollRectSize = _ScrollRectSize;

            SetScrollBarVisibility(scrollbarDisplayMode == ScrollbarDisplayMode.Always);
        }
        protected virtual IEnumerator ScrollToBottomCoroutine()
        {
            if (scrollRect == null)
            {
                yield break;
            }
            yield return(null);

            var contentHeight    = contentPanel.rect.height;
            var scrollRectHeight = scrollRect.GetComponent <RectTransform>().rect.height;
            var needToScroll     = contentHeight > scrollRectHeight;

            if (needToScroll)
            {
                var ratio   = scrollRectHeight / contentHeight;
                var timeout = Time.time + 10f; // Avoid infinite loops by maxing out at 10 seconds.
                while (scrollRect.verticalNormalizedPosition > 0.01f && Time.time < timeout)
                {
                    var newPos = scrollRect.verticalNormalizedPosition - scrollSpeed * Time.deltaTime * ratio;
                    scrollRect.verticalNormalizedPosition = Mathf.Max(0, newPos);
                    yield return(null);
                }
            }
            scrollRect.verticalNormalizedPosition = 0;
            scrollCoroutine = null;
        }
        private IEnumerator CheckScrollbarAfterUIUpdate()
        {
            m_checking = true;
            yield return(null);

            scrollbar.gameObject.SetActive(scrollContent.rect.height > scrollRect.GetComponent <RectTransform>().rect.height);
            m_checking = false;
        }
        private IEnumerator CheckScrollbarAfterUIUpdate(bool useResetValue, float resetValue)
        {
            m_checking = true;
            yield return(null);

            scrollbar.gameObject.SetActive(scrollContent.rect.height > scrollRect.GetComponent <RectTransform>().rect.height);
            m_checking = false;
            yield return(null);

            if (useResetValue)
            {
                if (smoothScroll)
                {
                    var contentHeight = scrollContent.rect.height;
                    if (m_scrollRectTransform == null)
                    {
                        m_scrollRectTransform = scrollRect.GetComponent <RectTransform>();
                    }
                    var scrollRectHeight = m_scrollRectTransform.rect.height;
                    var needToScroll     = contentHeight > scrollRectHeight;
                    if (needToScroll)
                    {
                        var ratio   = scrollRectHeight / contentHeight;
                        var timeout = Time.time + 10f; // Avoid infinite loops by maxing out at 10 seconds.
                        while (scrollRect.verticalNormalizedPosition > 0.01f && Time.time < timeout)
                        {
                            var newPos = scrollRect.verticalNormalizedPosition - smoothScrollSpeed * Time.deltaTime * ratio;
                            scrollRect.verticalNormalizedPosition = Mathf.Max(0, newPos);
                            yield return(null);
                        }
                    }
                    scrollRect.verticalNormalizedPosition = 0;
                }
                else
                {
                    scrollbar.value = resetValue;
                    scrollRect.verticalNormalizedPosition = resetValue;
                }
            }
        }
        private IEnumerator CheckScrollbarAfterUIUpdate(bool useResetValue, float resetValue)
        {
            m_checking = true;
            yield return(null);

            scrollbar.gameObject.SetActive(scrollContent.rect.height > scrollRect.GetComponent <RectTransform>().rect.height);
            m_checking = false;
            yield return(null);

            if (useResetValue)
            {
                scrollbar.value = resetValue;
                scrollRect.verticalNormalizedPosition = resetValue;
            }
        }