Exemplo n.º 1
0
        /// <summary>
        /// Init this instance.
        /// </summary>
        protected virtual void Init()
        {
            if (isInited)
            {
                return;
            }
            isInited = true;

            //ScrollRect.horizontal = false;
            //ScrollRect.vertical = false;

            var resizeListener = Utilites.GetOrAddComponent <ResizeListener>(ScrollRect);

            resizeListener.OnResize.AddListener(RecalculatePages);

            var contentResizeListener = Utilites.GetOrAddComponent <ResizeListener>(ScrollRect.content);

            contentResizeListener.OnResize.AddListener(RecalculatePages);

            var dragListener = Utilites.GetOrAddComponent <OnDragListener>(ScrollRect);

            dragListener.OnDragStartEvent.AddListener(OnScrollRectDragStart);
            dragListener.OnDragEvent.AddListener(OnScrollRectDrag);
            dragListener.OnDragEndEvent.AddListener(OnScrollRectDragEnd);

            ScrollRect.onValueChanged.AddListener(OnScrollRectValueChanged);

            var scroll_listener = Utilites.GetOrAddComponent <ScrollListener>(ScrollRect);

            scroll_listener.ScrollEvent.AddListener(ContainerScroll);

            if (DefaultPage != null)
            {
                SRDefaultPage = Utilites.GetOrAddComponent <ScrollRectPage>(DefaultPage);
                SRDefaultPage.gameObject.SetActive(false);
            }

            if (ActivePage != null)
            {
                SRActivePage = Utilites.GetOrAddComponent <ScrollRectPage>(ActivePage);
            }

            if (PrevPage != null)
            {
                SRPrevPage = Utilites.GetOrAddComponent <ScrollRectPage>(PrevPage);
                SRPrevPage.SetPage(0);
                SRPrevPage.OnPageSelect.AddListener(Prev);
            }
            if (NextPage != null)
            {
                SRNextPage = Utilites.GetOrAddComponent <ScrollRectPage>(NextPage);
                SRNextPage.OnPageSelect.AddListener(Next);
            }

            RecalculatePages();
            var page = currentPage;

            currentPage = -1;
            GoToPage(page);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates the page buttons.
        /// </summary>
        protected virtual void UpdatePageButtons()
        {
            if (SRDefaultPage == null)
            {
                return;
            }

            DefaultPages.RemoveAll(IsNullComponent);

            if (DefaultPages.Count == Pages)
            {
                return;
            }

            if (DefaultPages.Count < Pages)
            {
                DefaultPagesCache.RemoveAll(IsNullComponent);

                for (int i = DefaultPages.Count; i < Pages; i++)
                {
                    AddComponent(i);
                }

                if (SRNextPage != null)
                {
                    SRNextPage.SetPage(Pages - 1);
                    SRNextPage.transform.SetAsLastSibling();
                }
            }
            else
            {
                for (int i = Pages; i < DefaultPages.Count; i++)
                {
                    DefaultPages[i].gameObject.SetActive(false);
                    DefaultPagesCache.Add(DefaultPages[i]);
                }

                DefaultPages.RemoveRange(Pages, DefaultPages.Count - Pages);

                if (SRNextPage != null)
                {
                    SRNextPage.SetPage(Pages - 1);
                }
            }

            LayoutUtilites.UpdateLayout(DefaultPage.parent.GetComponent <LayoutGroup>());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Updates the page buttons.
        /// </summary>
        protected virtual void UpdatePageButtons()
        {
            if (SRDefaultPage == null)
            {
                return;
            }

            DefaultPages.RemoveAll(IsNullComponent);

            if (DefaultPages.Count == Pages)
            {
                return;
            }

            if (DefaultPages.Count < Pages)
            {
                DefaultPagesCache.RemoveAll(IsNullComponent);

                Enumerable.Range(DefaultPages.Count, Pages - DefaultPages.Count).ForEach(AddComponent);

                if (SRNextPage != null)
                {
                    SRNextPage.SetPage(Pages - 1);
                    SRNextPage.transform.SetAsLastSibling();
                }
            }
            else
            {
                var to_cache = DefaultPages.GetRange(Pages, DefaultPages.Count - Pages);                //.OrderByDescending<ScrollRectPage,int>(GetPageNumber);

                to_cache.ForEach(x => x.gameObject.SetActive(false));
                DefaultPagesCache.AddRange(to_cache);
                DefaultPages.RemoveRange(Pages, DefaultPages.Count - Pages);

                if (SRNextPage != null)
                {
                    SRNextPage.SetPage(Pages - 1);
                }
            }

            Utilites.UpdateLayout(DefaultPage.parent.GetComponent <LayoutGroup>());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Update objects.
        /// </summary>
        /// <param name="page">Page.</param>
        protected virtual void UpdateObjects(int page)
        {
            if (SRDefaultPage != null)
            {
                if ((currentPage >= 0) && (currentPage < DefaultPages.Count))
                {
                    DefaultPages[currentPage].gameObject.SetActive(true);
                }

                DefaultPages[page].gameObject.SetActive(false);
                SRActivePage.SetPage(page);
                SRActivePage.transform.SetSiblingIndex(DefaultPages[page].transform.GetSiblingIndex());
            }

            if (SRPrevPage != null)
            {
                SRPrevPage.gameObject.SetActive(page != 0);
            }

            if (SRNextPage != null)
            {
                SRNextPage.gameObject.SetActive(page != (Pages - 1));
            }
        }
        /// <summary>
        /// Update objects.
        /// </summary>
        /// <param name="page">Page.</param>
        protected virtual void UpdateObjects(int page)
        {
            if (SRDefaultPage != null)
            {
                foreach (var dp in DefaultPages)
                {
                    dp.gameObject.SetActive(true);
                }

                DefaultPages[page].gameObject.SetActive(false);
                SRActivePage.SetPage(page);
                SRActivePage.transform.SetSiblingIndex(DefaultPages[page].transform.GetSiblingIndex());
            }

            if (SRPrevPage != null)
            {
                SRPrevPage.gameObject.SetActive(page != 0);
            }

            if (SRNextPage != null)
            {
                SRNextPage.gameObject.SetActive(page != (Pages - 1));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Go to page.
        /// </summary>
        /// <param name="page">Page.</param>
        /// <param name="forceUpdate">If set to <c>true</c> force update.</param>
        protected virtual void GoToPage(int page, bool forceUpdate)
        {
            page = Mathf.Clamp(page, 0, Pages - 1);
            if ((currentPage == page) && (!forceUpdate))
            {
                return;
            }

            if (isAnimationRunning)
            {
                isAnimationRunning = false;
                if (currentAnimation != null)
                {
                    StopCoroutine(currentAnimation);
                }
            }

            var endPosition = GetPagePosition(page);

            if (IsHorizontal())
            {
                endPosition *= -1;
            }
            if (Animation)
            {
                isAnimationRunning = true;
                var startPosition = IsHorizontal() ? ScrollRect.content.anchoredPosition.x : ScrollRect.content.anchoredPosition.y;
                currentAnimation = RunAnimation(IsHorizontal(), startPosition, endPosition, UnscaledTime);
                StartCoroutine(currentAnimation);
            }
            else
            {
                if (IsHorizontal())
                {
                    ScrollRect.content.anchoredPosition = new Vector2(endPosition, ScrollRect.content.anchoredPosition.y);
                }
                else
                {
                    ScrollRect.content.anchoredPosition = new Vector2(ScrollRect.content.anchoredPosition.x, endPosition);
                }
            }

            if ((SRDefaultPage != null) && (currentPage != page))
            {
                if (currentPage >= 0)
                {
                    DefaultPages[currentPage].gameObject.SetActive(true);
                }
                DefaultPages[page].gameObject.SetActive(false);
                SRActivePage.SetPage(page);
                SRActivePage.transform.SetSiblingIndex(DefaultPages[page].transform.GetSiblingIndex());
            }
            if (SRPrevPage != null)
            {
                SRPrevPage.gameObject.SetActive(page != 0);
            }
            if (SRNextPage != null)
            {
                SRNextPage.gameObject.SetActive(page != (Pages - 1));
            }

            currentPage = page;

            OnPageSelect.Invoke(currentPage);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Start this instance.
        /// </summary>
        protected virtual void Start()
        {
            if (isStarted)
            {
                return;
            }
            isStarted = true;

            var resizeListener = ScrollRect.GetComponent <ResizeListener>();

            if (resizeListener == null)
            {
                resizeListener = ScrollRect.gameObject.AddComponent <ResizeListener>();
            }
            resizeListener.OnResize.AddListener(RecalculatePages);

            var contentResizeListener = ScrollRect.content.GetComponent <ResizeListener>();

            if (contentResizeListener == null)
            {
                contentResizeListener = ScrollRect.content.gameObject.AddComponent <ResizeListener>();
            }
            contentResizeListener.OnResize.AddListener(RecalculatePages);

            var dragListener = ScrollRect.GetComponent <OnDragListener>();

            if (dragListener == null)
            {
                dragListener = ScrollRect.gameObject.AddComponent <OnDragListener>();
            }
            dragListener.OnDragStartEvent.AddListener(OnScrollRectDragStart);
            dragListener.OnDragEvent.AddListener(OnScrollRectDrag);
            dragListener.OnDragEndEvent.AddListener(OnScrollRectDragEnd);

            ScrollRect.onValueChanged.AddListener(OnScrollRectValueChanged);

            if (DefaultPage != null)
            {
                SRDefaultPage = DefaultPage.GetComponent <ScrollRectPage>();
                if (SRDefaultPage == null)
                {
                    SRDefaultPage = DefaultPage.gameObject.AddComponent <ScrollRectPage>();
                }
                SRDefaultPage.gameObject.SetActive(false);
            }

            if (ActivePage != null)
            {
                SRActivePage = ActivePage.GetComponent <ScrollRectPage>();
                if (SRActivePage == null)
                {
                    SRActivePage = ActivePage.gameObject.AddComponent <ScrollRectPage>();
                }
            }

            if (PrevPage != null)
            {
                SRPrevPage = PrevPage.GetComponent <ScrollRectPage>();
                if (SRPrevPage == null)
                {
                    SRPrevPage = PrevPage.gameObject.AddComponent <ScrollRectPage>();
                }
                SRPrevPage.SetPage(0);
                SRPrevPage.OnPageSelect.AddListener(Prev);
            }
            if (NextPage != null)
            {
                SRNextPage = NextPage.GetComponent <ScrollRectPage>();
                if (SRNextPage == null)
                {
                    SRNextPage = NextPage.gameObject.AddComponent <ScrollRectPage>();
                }
                SRNextPage.OnPageSelect.AddListener(Next);
            }

            RecalculatePages();
            var page = currentPage;

            currentPage = -1;
            GoToPage(page);
        }