From f89702ddc67cfa2910ced0a006925085f8c7d99d Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Sun, 12 Mar 2017 15:18:36 +0000 Subject: [PATCH] Minor VSS/HSS bugfixes and enhancements, resolving #114 --- Scripts/Layout/HorizontalScrollSnap.cs | 11 ++++ Scripts/Layout/ScrollSnapBase.cs | 91 ++++++++++++++------------ Scripts/Layout/VerticalScrollSnap.cs | 11 ++++ 3 files changed, 72 insertions(+), 41 deletions(-) diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index ed43336..bea5cf8 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -37,6 +37,7 @@ namespace UnityEngine.UI.Extensions _screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * Time.deltaTime); if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f) { + _screensContainer.localPosition = _lerp_target; _lerp = false; EndScreenChange(); } @@ -192,6 +193,16 @@ namespace UnityEngine.UI.Extensions } } + private void OnEnable() + { + InitialiseChildObjects(); + DistributePages(); + if (MaskArea) UpdateVisible(); + + if(JumpOnEnable) SetScrollContainerPosition(); + GoToScreen(StartingScreen); + } + #region Interfaces /// /// Release screen to swipe diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index 110b49c..49bc145 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -90,7 +90,10 @@ namespace UnityEngine.UI.Extensions } } - [Tooltip("(Experimental)\nBy default, child array objects will use the parent transform\nHowever you can disable this for some interesting effects")] + [Tooltip("By default the container will lerp to the start when enabled in the scene, this option overrides this and forces it to simply jump without lerping")] + public bool JumpOnEnable = false; + + [Tooltip("(Experimental)\nBy default, child array objects will use the parent transform\nHowever you can disable this for some interesting effects")] public bool UseParentTransform = true; [Tooltip("Scroll Snap children. (optional)\nEither place objects in the scene as children OR\nPrefabs in this array, NOT BOTH")] @@ -112,7 +115,7 @@ namespace UnityEngine.UI.Extensions public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } // Use this for initialization - void Awake() + void Awake() { _scroll_rect = gameObject.GetComponent(); @@ -127,20 +130,8 @@ namespace UnityEngine.UI.Extensions } _screensContainer = _scroll_rect.content; - if (ChildObjects != null && ChildObjects.Length > 0) - { - if (_screensContainer.transform.childCount > 0) - { - Debug.LogError("ScrollRect Content has children, this is not supported when using managed Child Objects\n Either remove the ScrollRect Content children or clear the ChildObjects array"); - return; - } - InitialiseChildObjectsFromArray(); - } - else - { - InitialiseChildObjectsFromScene(); - } + InitialiseChildObjects(); if (NextButton) NextButton.GetComponent /// 0 starting index of page to jump to - public void GoToScreen(int screenIndex) + public void GoToScreen(int screenIndex) { if (screenIndex <= _screens - 1 && screenIndex >= 0) { @@ -284,11 +293,11 @@ namespace UnityEngine.UI.Extensions /// /// Position to test, normally the Scroll Rect container Local position /// Closest Page number (zero indexed array value) - internal int GetPageforPosition(Vector3 pos) + internal int GetPageforPosition(Vector3 pos) { return _isVertical ? - -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) : - -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize); + -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) : + -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize); } /// @@ -296,11 +305,11 @@ namespace UnityEngine.UI.Extensions /// /// Position to test, normally the Scroll Rect container Local position /// True / False, is the position in the bounds of a page - internal bool IsRectSettledOnaPage(Vector3 pos) + internal bool IsRectSettledOnaPage(Vector3 pos) { return _isVertical ? - -((pos.y - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) : - -((pos.x - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize); + -((pos.y - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) : + -((pos.x - _scrollStartPosition) / _childSize) == -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize); } /// @@ -308,7 +317,7 @@ namespace UnityEngine.UI.Extensions /// /// Page that the position is required for (Zero indexed array value) /// Outputs the local position for the selected page - internal void GetPositionforPage(int page, ref Vector3 target) + internal void GetPositionforPage(int page, ref Vector3 target) { _childPos = -_childSize * page; if (_isVertical) @@ -324,7 +333,7 @@ namespace UnityEngine.UI.Extensions /// /// Updates the _Lerp target to the closest page and updates the pagination bullets. Each control's update loop will then handle the move. /// - internal void ScrollToClosestElement() + internal void ScrollToClosestElement() { _lerp = true; CurrentPage = GetPageforPosition(_screensContainer.localPosition); @@ -335,7 +344,7 @@ namespace UnityEngine.UI.Extensions /// /// notifies pagination indicator and navigation buttons of a screen change /// - internal void OnCurrentScreenChange(int currentScreen) + internal void OnCurrentScreenChange(int currentScreen) { ChangeBulletsInfo(currentScreen); ToggleNavigationButtons(currentScreen); @@ -345,14 +354,14 @@ namespace UnityEngine.UI.Extensions /// changes the bullets on the bottom of the page - pagination /// /// - private void ChangeBulletsInfo(int targetScreen) + private void ChangeBulletsInfo(int targetScreen) { if (Pagination) for (int i = 0; i < Pagination.transform.childCount; i++) { Pagination.transform.GetChild(i).GetComponent().isOn = (targetScreen == i) ? true - : false; + : false; } } @@ -360,7 +369,7 @@ namespace UnityEngine.UI.Extensions /// disables the page navigation buttons when at the first or last screen /// /// - private void ToggleNavigationButtons(int targetScreen) { + private void ToggleNavigationButtons(int targetScreen) { if (PrevButton) { PrevButton.GetComponent