From d101a574c832b0d3ef32d48015a9369ca54b3f8d Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Sat, 10 Dec 2016 13:36:28 +0000 Subject: [PATCH] Removed arrays from control and refactored, heavy perf inprovement Downside it made my "visible" improvement redundant and need to re-implement. --HG-- branch : develop_5.3 --- Scripts/Layout/HorizontalScrollSnap.cs | 37 +++-------- Scripts/Layout/ScrollSnapBase.cs | 86 +++++++++++++++++--------- Scripts/Layout/VerticalScrollSnap.cs | 33 +++------- 3 files changed, 75 insertions(+), 81 deletions(-) diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index bc52115..77a424c 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -24,6 +24,9 @@ namespace UnityEngine.UI.Extensions Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results"); } + //Should this derrive from ScrolRect? + isVertical = false; + _screensContainer = _scroll_rect.content; DistributePages(); @@ -37,10 +40,9 @@ namespace UnityEngine.UI.Extensions void Start() { - UpdateChildPositions(); _lerp = false; _currentScreen = StartingScreen - 1; - + _scrollStartPosition = _screensContainer.localPosition.x; _scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1); ChangeBulletsInfo(_currentScreen); @@ -72,7 +74,7 @@ namespace UnityEngine.UI.Extensions else if ((_scroll_rect.velocity.x > 0 && _scroll_rect.velocity.x > SwipeVelocityThreshold) || _scroll_rect.velocity.x < 0 && _scroll_rect.velocity.x < -SwipeVelocityThreshold) { - _currentScreen = GetPageforPosition(FindClosestFrom(_screensContainer.localPosition, _visiblePositions)); + _currentScreen = GetPageforPosition(_screensContainer.localPosition); if (_currentScreen != _previousScreen) { _previousScreen = _currentScreen; @@ -85,14 +87,16 @@ namespace UnityEngine.UI.Extensions } } - //used for changing between screen resolutions + //used for changing between screen resolutions private void DistributePages() { + _screens = _screensContainer.childCount; + int _offset = 0; float _dimension = 0; Rect panelDimensions = gameObject.GetComponent().rect; float currentXPosition = 0; - var pageStepValue = (int)panelDimensions.width * ((PageStep == 0) ? 3 : PageStep); + var pageStepValue = _childSize = (int)panelDimensions.width * ((PageStep == 0) ? 3 : PageStep); for (int i = 0; i < _screensContainer.transform.childCount; i++) @@ -111,26 +115,7 @@ namespace UnityEngine.UI.Extensions _screensContainer.GetComponent().offsetMax = new Vector2(_dimension, 0f); } - void UpdateChildPositions() - { - _screens = _screensContainer.childCount; - - _positions = new Vector3[_screens]; - - if (_screens > 0) - { - for (int i = 0; i < _screens; ++i) - { - _scroll_rect.horizontalNormalizedPosition = (float)i / (float)(_screens - 1); - _positions[i] = _screensContainer.localPosition; - } - } - - //debug visible - _visiblePositions = _positions; - } - - /// + /// /// Add a new child to this Scroll Snap and recalculate it's children /// /// GameObject to add to the ScrollSnap @@ -139,7 +124,6 @@ namespace UnityEngine.UI.Extensions _scroll_rect.horizontalNormalizedPosition = 0; GO.transform.SetParent(_screensContainer); DistributePages(); - UpdateChildPositions(); _scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1); } @@ -172,7 +156,6 @@ namespace UnityEngine.UI.Extensions } DistributePages(); - UpdateChildPositions(); if (_currentScreen > _screens - 1) { diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index d51d02a..9a02878 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -1,5 +1,5 @@ -using UnityEngine; -using System.Collections; +using UnityEngine; +using System.Collections; using UnityEngine.EventSystems; using System; using UnityEngine.Events; @@ -8,12 +8,22 @@ namespace UnityEngine.UI.Extensions { public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IPointerDownHandler, IPointerUpHandler { - internal Transform _screensContainer; + internal struct VisibleItem + { + public Vector3 Position; + public int OriginalPosition; + } + + internal RectTransform _screensContainer; + internal bool isVertical; internal int _screens = 1; - internal Vector3[] _positions; - internal Vector3[] _visiblePositions; + internal float _scrollStartPosition; + internal float _childSize; + private float _childPos; + //internal Vector3[] _positions; + //internal VisibleItem[] _visiblePositions; internal ScrollRect _scroll_rect; internal Vector3 _lerp_target; internal bool _lerp; @@ -24,6 +34,11 @@ namespace UnityEngine.UI.Extensions [Serializable] public class SelectionChangeEndEvent : UnityEvent { } + [Tooltip("The visible bounds area, controls which items are visible/enabled. *Note Should use a RectMask. (optional)")] + public RectTransform MaskArea; + [Tooltip("Pixel size to buffer arround Mask Area. (optional)")] + public float MaskBuffer; + [Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")] public GameObject Pagination; @@ -81,7 +96,8 @@ namespace UnityEngine.UI.Extensions _lerp = true; _currentScreen++; - _lerp_target = _positions[_currentScreen]; + GetPositionforPage(_currentScreen, ref _lerp_target); + // _lerp_target = _positions[_currentScreen]; ChangeBulletsInfo(_currentScreen); } @@ -96,7 +112,8 @@ namespace UnityEngine.UI.Extensions _lerp = true; _currentScreen--; - _lerp_target = _positions[_currentScreen]; + GetPositionforPage(_currentScreen, ref _lerp_target); +// _lerp_target = _positions[_currentScreen]; ChangeBulletsInfo(_currentScreen); } @@ -115,37 +132,60 @@ namespace UnityEngine.UI.Extensions _lerp = true; _currentScreen = screenIndex; - _lerp_target = _positions[_currentScreen]; + GetPositionforPage(_currentScreen, ref _lerp_target); + // _lerp_target = _positions[_currentScreen]; ChangeBulletsInfo(_currentScreen); } } //find the closest registered point to the releasing point - internal Vector3 FindClosestFrom(Vector3 start, Vector3[] positions) + internal Vector3 FindClosestFrom(Vector3 start, VisibleItem[] positions) { + Vector3 closestPosition = Vector3.zero; float closest = Mathf.Infinity; float distanceToTarget = 0; - for (int i = 0; i < _screens; i++) + for (int i = 0; i < positions.Length; i++) { - distanceToTarget = Vector3.Distance(start, positions[i]); + distanceToTarget = Vector3.Distance(start, positions[i].Position); if (distanceToTarget < closest) { closest = distanceToTarget; - closestPosition = positions[i]; + closestPosition = positions[i].Position; } } - + float close = -(int)Math.Round((start.y - _scrollStartPosition) / _childSize); return closestPosition; } - + + internal int GetPageforPosition(Vector3 pos) + { + return isVertical ? + -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) : + -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize); + } + + internal void GetPositionforPage(int page, ref Vector3 target) + { + _childPos = -_childSize * page; + if (isVertical) + { + target.y = _childPos + _scrollStartPosition; + } + else + { + target.x = _childPos + _scrollStartPosition; + } + } + internal void ScrollToClosestElement() { _lerp = true; - _lerp_target = FindClosestFrom(_screensContainer.localPosition, _visiblePositions); - _currentScreen = GetPageforPosition(_lerp_target); + //_lerp_target = FindClosestFrom(_screensContainer.localPosition, _visiblePositions); + _currentScreen = GetPageforPosition(_screensContainer.localPosition); + GetPositionforPage(_currentScreen, ref _lerp_target); ChangeBulletsInfo(_currentScreen); } @@ -161,18 +201,6 @@ namespace UnityEngine.UI.Extensions } } - internal int GetPageforPosition(Vector3 pos) - { - for (int i = 0; i < _positions.Length; i++) - { - if (_positions[i] == pos) - { - return i; - } - } - return 0; - } - void OnValidate() { var childCount = gameObject.GetComponent().content.childCount; @@ -228,5 +256,5 @@ namespace UnityEngine.UI.Extensions #endregion - } + } } \ No newline at end of file diff --git a/Scripts/Layout/VerticalScrollSnap.cs b/Scripts/Layout/VerticalScrollSnap.cs index e0fb8c0..a08c7a9 100644 --- a/Scripts/Layout/VerticalScrollSnap.cs +++ b/Scripts/Layout/VerticalScrollSnap.cs @@ -23,6 +23,9 @@ namespace UnityEngine.UI.Extensions Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results"); } + //Should this derrive from ScrolRect? + isVertical = true; + _screensContainer = _scroll_rect.content; DistributePages(); @@ -36,10 +39,9 @@ namespace UnityEngine.UI.Extensions void Start() { - UpdateChildPositions(); _lerp = false; _currentScreen = StartingScreen - 1; - + _scrollStartPosition = _screensContainer.localPosition.y; _scroll_rect.verticalNormalizedPosition = (float)(_currentScreen) / (float)(_screens - 1); ChangeBulletsInfo(_currentScreen); @@ -71,7 +73,7 @@ namespace UnityEngine.UI.Extensions else if ((_scroll_rect.velocity.y > 0 && _scroll_rect.velocity.y > SwipeVelocityThreshold) || _scroll_rect.velocity.y < 0 && _scroll_rect.velocity.y < -SwipeVelocityThreshold) { - _currentScreen = GetPageforPosition(FindClosestFrom(_screensContainer.localPosition, _visiblePositions)); + _currentScreen = GetPageforPosition(_screensContainer.localPosition); if (_currentScreen != _previousScreen) { _previousScreen = _currentScreen; @@ -87,11 +89,13 @@ namespace UnityEngine.UI.Extensions //used for changing between screen resolutions public void DistributePages() { + _screens = _screensContainer.childCount; + float _offset = 0; float _dimension = 0; Rect panelDimensions = gameObject.GetComponent().rect; float currentYPosition = 0; - var pageStepValue = (int)panelDimensions.height * ((PageStep == 0) ? 3 : PageStep); + var pageStepValue = _childSize = (int)panelDimensions.height * ((PageStep == 0) ? 3 : PageStep); for (int i = 0; i < _screensContainer.transform.childCount; i++) { @@ -109,25 +113,6 @@ namespace UnityEngine.UI.Extensions _screensContainer.GetComponent().offsetMax = new Vector2(0f, _dimension); } - void UpdateChildPositions() - { - _screens = _screensContainer.childCount; - - _positions = new Vector3[_screens]; - - if (_screens > 0) - { - for (int i = 0; i < _screens; ++i) - { - _scroll_rect.verticalNormalizedPosition = (float)i / (float)(_screens - 1); - _positions[i] = _screensContainer.localPosition; - } - } - - //debug visible - _visiblePositions = _positions; - } - /// /// Add a new child to this Scroll Snap and recalculate it's children /// @@ -137,7 +122,6 @@ namespace UnityEngine.UI.Extensions _scroll_rect.verticalNormalizedPosition = 0; GO.transform.SetParent(_screensContainer); DistributePages(); - UpdateChildPositions(); _scroll_rect.verticalNormalizedPosition = (float)(_currentScreen) / (_screens - 1); } @@ -169,7 +153,6 @@ namespace UnityEngine.UI.Extensions i++; } DistributePages(); - UpdateChildPositions(); if (_currentScreen > _screens - 1) {