/*void LateUpdate() * { * if (!firstPageSet) * { * SetFirstPage(); * } * }*/ void SetFirstPage() { if (firstPageSet) { return; } firstPageSet = true; if (UsingScrollRect) { CenterScrollRectOnCurrentPage(true); PagedRectTimer.DelayedCall(0.01f, () => CenterScrollRectOnCurrentPage(true), this); PagedRectTimer.DelayedCall(0.05f, () => { UpdatePages(true, false); UpdateSeamlessPagePositions(); }, this); } UpdatePagination(); }
void Awake() { CurrentPage = DefaultPage; // This is to help maintain compatibility with earlier versions of PagedRect where ScrollRect was a standard Unity ScrollRect instead of a PagedRect_ScrollRect // (if there isn't a PagedRectScrollRect present, then the value of 'ScrollRect' will still be null, and UsingScrollRect will be false) if (ScrollRect == null) { ScrollRect = GetComponent <PagedRect_ScrollRect>(); } if (UsingScrollRect) { ScrollRect.horizontalNormalizedPosition = 0; ScrollRect.verticalNormalizedPosition = 0; ScrollRect.content.anchoredPosition = Vector2.zero; } if (Application.isPlaying) { InvalidateButtonPool(); } OptimizePagination(); InitializePaginationIcons(); PagedRectTimer.DelayedCall(0, SetFirstPage, this); }
public override void OnInspectorGUI() { if (DrawDefaultInspector()) { var page = (Page)target; var pagedRect = page.GetPagedRect(); if (pagedRect != null) { PagedRectTimer.DelayedCall(0f, () => page.NotifyPagedRectOfChange(), pagedRect); } } }
/// <summary> /// Initialise this Page object and attach it to its parent PagedRect. /// </summary> /// <param name="pagedRect"></param> public void Initialise(PagedRect pagedRect) { if (Initialised) { return; } initialPosition = this.transform.localPosition; Initialised = true; _pagedRect = pagedRect; UpdateDimensions(); if (Application.isPlaying) { if (!pagedRect.UsingScrollRect) { Animator = this.GetComponent <Animator>(); if (Animator == null) { // setup the animator for this page Animator = this.gameObject.AddComponent <Animator>(); } Animator.runtimeAnimatorController = Instantiate(pagedRect.AnimationControllerTemplate) as RuntimeAnimatorController; } else { Animator = this.GetComponent <Animator>(); if (Animator != null) { Animator.enabled = false; } } if (pagedRect.ShowPagePreviews) { PagedRectTimer.DelayedCall(0, () => { if (pagedRect.CurrentPage != this.PageNumber) { ShowOverlay(); } }, this); } } }
/// <summary> /// Remove a Page from this PagedRect, and optionally destroy it /// </summary> /// <param name="page"></param> /// <param name="destroyPageObject"></param> public void RemovePage(Page page, bool destroyPageObject = false) { if (Pages.Contains(page)) { page.ShowOnPagination = false; Pages.Remove(page); page.gameObject.SetActive(false); // If we remove the current page, move to the previous page if (page.PageNumber == CurrentPage) { var pageToMoveTo = Pages.OrderByDescending(p => p.PageNumber) .Where(p => p.PageNumber < CurrentPage) .FirstOrDefault(p => p.PageEnabled && p.ShowOnPagination); if (pageToMoveTo != null) { CurrentPage = pageToMoveTo.PageNumber; } else { CurrentPage = NumberOfPages; } //if (CurrentPage != 1) PreviousPage(); } if (destroyPageObject) { if (Application.isPlaying) { Destroy(page.gameObject); } else { DestroyImmediate(page.gameObject); } } else if (UsingScrollRect) { page.gameObject.SetActive(false); } UpdatePageNumbers(); this.isDirty = true; PagedRectTimer.DelayedCall(0, () => this.UpdatePages(true, true, true), this); } }
void OnValidate() { this.isDirty = true; if (!gameObject.activeInHierarchy) { return; } PagedRectTimer.DelayedCall(0, () => { if (this.isDirty) { UpdateDisplay(); } }, this); }
/// <summary> /// Add a new page to this PagedRect - this Page should already have been instantiated. /// </summary> /// <param name="page"></param> public void AddPage(Page page) { if (UsingScrollRect) { page.gameObject.SetActive(true); } page.transform.SetParent(Viewport.transform); page.transform.localPosition = Vector3.zero; page.transform.localScale = Vector3.one; // If we don't set these values, Unity seems to set them to non-zero values and our new page shows up off-screen var rectTransform = (RectTransform)page.transform; rectTransform.offsetMax = Vector2.zero; rectTransform.offsetMin = Vector2.zero; rectTransform.sizeDelta = Vector2.zero; page.ShowOnPagination = true; this.isDirty = true; if (CurrentPage == 0) { CurrentPage = 1; } UpdateDisplay(); if (UsingScrollRect) { // attempt to insert the new page in the correct position var previousPage = GetPageByNumber(page.PageNumber - 1, false, true); if (previousPage != null) { page.transform.SetSiblingIndex(previousPage.transform.GetSiblingIndex() + 1); Pages = Pages.OrderBy(p => p.transform.GetSiblingIndex()).ToList(); } CenterScrollRectOnCurrentPage(true); PagedRectTimer.DelayedCall(0, () => CenterScrollRectOnCurrentPage(true), this); } }
void OnEnable() { PagedRectTimer.DelayedCall(0, ViewportDimensionsChanged, this); }
/// <summary> /// Called by UpdateScrollRectPagePositions when ShowPagePreviews is true /// </summary> void UpdateSeamlessPagePositions_PagePreviews() { if (!Application.isPlaying) { return; } if (!LoopSeamlessly) { return; } // we need at least 3 pages for our positioning code to work properly, // so duplicate pages as necessary // This isn't working yet, but will hopefully be available in a later version of PagedRect /*if (NumberOfPages <= 3 || Pages.Any(p => p.IsDuplicate)) * { * SeamlessLooping_HandleDuplicatePages(); * }*/ // this will work differently to the regular method // instead of moving pages as we scroll past certain offsets at the start/end, we will instead move pages whenever we change the current page, // provided we're less than two pages from the start/end bool pageMoved = false; var pagePosition = GetPagePosition(CurrentPage); float oneOrMinusOne = 1; var pageSize = ScrollRect.horizontal ? m_otherPageSize.x : m_otherPageSize.y; // if we have enough pages, then move pages around earlier to make the experience more 'seamless' // (and avoid users seeing the actual page move). With fewer pages, this isn't an option var minPage = NumberOfPages >= 5 ? 2 : 1; if (pagePosition <= minPage) { var pageToMove = Pages.Last(); pageToMove.transform.SetAsFirstSibling(); oneOrMinusOne = -1; pageMoved = true; } else if (NumberOfPages - pagePosition <= minPage) { var pageToMove = Pages.First(); pageToMove.transform.SetAsLastSibling(); pageMoved = true; } if (pageMoved) { ScrollRect.ResetDragOffset = true; var directionVector = ScrollRect.GetDirectionVector(); var adjustment = (directionVector * (pageSize + SpaceBetweenPages) * oneOrMinusOne); ScrollRect.content.anchoredPosition += adjustment; UpdatePages(); // if we're already scrolling, stop, recalculate, and scroll from there if (scrollCoroutine != null) { scrollRectAnimation_InitialPosition += adjustment; scrollRectAnimation_DesiredPosition += adjustment; } PagedRectTimer.DelayedCall(0, () => Viewport.GetComponent <PagedRect_LayoutGroup>().SetLayoutHorizontal(), this); } }