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
release
Simon Jackson 2016-12-10 13:36:28 +00:00
parent 5cbfc70e30
commit d101a574c8
3 changed files with 75 additions and 81 deletions

View File

@ -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"); 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; _screensContainer = _scroll_rect.content;
DistributePages(); DistributePages();
@ -37,10 +40,9 @@ namespace UnityEngine.UI.Extensions
void Start() void Start()
{ {
UpdateChildPositions();
_lerp = false; _lerp = false;
_currentScreen = StartingScreen - 1; _currentScreen = StartingScreen - 1;
_scrollStartPosition = _screensContainer.localPosition.x;
_scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1); _scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1);
ChangeBulletsInfo(_currentScreen); ChangeBulletsInfo(_currentScreen);
@ -72,7 +74,7 @@ namespace UnityEngine.UI.Extensions
else if ((_scroll_rect.velocity.x > 0 && _scroll_rect.velocity.x > SwipeVelocityThreshold) || else if ((_scroll_rect.velocity.x > 0 && _scroll_rect.velocity.x > SwipeVelocityThreshold) ||
_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) if (_currentScreen != _previousScreen)
{ {
_previousScreen = _currentScreen; _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() private void DistributePages()
{ {
_screens = _screensContainer.childCount;
int _offset = 0; int _offset = 0;
float _dimension = 0; float _dimension = 0;
Rect panelDimensions = gameObject.GetComponent<RectTransform>().rect; Rect panelDimensions = gameObject.GetComponent<RectTransform>().rect;
float currentXPosition = 0; 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++) for (int i = 0; i < _screensContainer.transform.childCount; i++)
@ -111,26 +115,7 @@ namespace UnityEngine.UI.Extensions
_screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(_dimension, 0f); _screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(_dimension, 0f);
} }
void UpdateChildPositions() /// <summary>
{
_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;
}
/// <summary>
/// Add a new child to this Scroll Snap and recalculate it's children /// Add a new child to this Scroll Snap and recalculate it's children
/// </summary> /// </summary>
/// <param name="GO">GameObject to add to the ScrollSnap</param> /// <param name="GO">GameObject to add to the ScrollSnap</param>
@ -139,7 +124,6 @@ namespace UnityEngine.UI.Extensions
_scroll_rect.horizontalNormalizedPosition = 0; _scroll_rect.horizontalNormalizedPosition = 0;
GO.transform.SetParent(_screensContainer); GO.transform.SetParent(_screensContainer);
DistributePages(); DistributePages();
UpdateChildPositions();
_scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1); _scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1);
} }
@ -172,7 +156,6 @@ namespace UnityEngine.UI.Extensions
} }
DistributePages(); DistributePages();
UpdateChildPositions();
if (_currentScreen > _screens - 1) if (_currentScreen > _screens - 1)
{ {

View File

@ -1,5 +1,5 @@
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using System; using System;
using UnityEngine.Events; using UnityEngine.Events;
@ -8,12 +8,22 @@ namespace UnityEngine.UI.Extensions
{ {
public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IPointerDownHandler, IPointerUpHandler 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 int _screens = 1;
internal Vector3[] _positions; internal float _scrollStartPosition;
internal Vector3[] _visiblePositions; internal float _childSize;
private float _childPos;
//internal Vector3[] _positions;
//internal VisibleItem[] _visiblePositions;
internal ScrollRect _scroll_rect; internal ScrollRect _scroll_rect;
internal Vector3 _lerp_target; internal Vector3 _lerp_target;
internal bool _lerp; internal bool _lerp;
@ -24,6 +34,11 @@ namespace UnityEngine.UI.Extensions
[Serializable] [Serializable]
public class SelectionChangeEndEvent : UnityEvent { } 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)")] [Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")]
public GameObject Pagination; public GameObject Pagination;
@ -81,7 +96,8 @@ namespace UnityEngine.UI.Extensions
_lerp = true; _lerp = true;
_currentScreen++; _currentScreen++;
_lerp_target = _positions[_currentScreen]; GetPositionforPage(_currentScreen, ref _lerp_target);
// _lerp_target = _positions[_currentScreen];
ChangeBulletsInfo(_currentScreen); ChangeBulletsInfo(_currentScreen);
} }
@ -96,7 +112,8 @@ namespace UnityEngine.UI.Extensions
_lerp = true; _lerp = true;
_currentScreen--; _currentScreen--;
_lerp_target = _positions[_currentScreen]; GetPositionforPage(_currentScreen, ref _lerp_target);
// _lerp_target = _positions[_currentScreen];
ChangeBulletsInfo(_currentScreen); ChangeBulletsInfo(_currentScreen);
} }
@ -115,37 +132,60 @@ namespace UnityEngine.UI.Extensions
_lerp = true; _lerp = true;
_currentScreen = screenIndex; _currentScreen = screenIndex;
_lerp_target = _positions[_currentScreen]; GetPositionforPage(_currentScreen, ref _lerp_target);
// _lerp_target = _positions[_currentScreen];
ChangeBulletsInfo(_currentScreen); ChangeBulletsInfo(_currentScreen);
} }
} }
//find the closest registered point to the releasing point //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; Vector3 closestPosition = Vector3.zero;
float closest = Mathf.Infinity; float closest = Mathf.Infinity;
float distanceToTarget = 0; 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) if (distanceToTarget < closest)
{ {
closest = distanceToTarget; closest = distanceToTarget;
closestPosition = positions[i]; closestPosition = positions[i].Position;
} }
} }
float close = -(int)Math.Round((start.y - _scrollStartPosition) / _childSize);
return closestPosition; 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() internal void ScrollToClosestElement()
{ {
_lerp = true; _lerp = true;
_lerp_target = FindClosestFrom(_screensContainer.localPosition, _visiblePositions); //_lerp_target = FindClosestFrom(_screensContainer.localPosition, _visiblePositions);
_currentScreen = GetPageforPosition(_lerp_target); _currentScreen = GetPageforPosition(_screensContainer.localPosition);
GetPositionforPage(_currentScreen, ref _lerp_target);
ChangeBulletsInfo(_currentScreen); 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() void OnValidate()
{ {
var childCount = gameObject.GetComponent<ScrollRect>().content.childCount; var childCount = gameObject.GetComponent<ScrollRect>().content.childCount;
@ -228,5 +256,5 @@ namespace UnityEngine.UI.Extensions
#endregion #endregion
} }
} }

View File

@ -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"); 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; _screensContainer = _scroll_rect.content;
DistributePages(); DistributePages();
@ -36,10 +39,9 @@ namespace UnityEngine.UI.Extensions
void Start() void Start()
{ {
UpdateChildPositions();
_lerp = false; _lerp = false;
_currentScreen = StartingScreen - 1; _currentScreen = StartingScreen - 1;
_scrollStartPosition = _screensContainer.localPosition.y;
_scroll_rect.verticalNormalizedPosition = (float)(_currentScreen) / (float)(_screens - 1); _scroll_rect.verticalNormalizedPosition = (float)(_currentScreen) / (float)(_screens - 1);
ChangeBulletsInfo(_currentScreen); ChangeBulletsInfo(_currentScreen);
@ -71,7 +73,7 @@ namespace UnityEngine.UI.Extensions
else if ((_scroll_rect.velocity.y > 0 && _scroll_rect.velocity.y > SwipeVelocityThreshold) || else if ((_scroll_rect.velocity.y > 0 && _scroll_rect.velocity.y > SwipeVelocityThreshold) ||
_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) if (_currentScreen != _previousScreen)
{ {
_previousScreen = _currentScreen; _previousScreen = _currentScreen;
@ -87,11 +89,13 @@ namespace UnityEngine.UI.Extensions
//used for changing between screen resolutions //used for changing between screen resolutions
public void DistributePages() public void DistributePages()
{ {
_screens = _screensContainer.childCount;
float _offset = 0; float _offset = 0;
float _dimension = 0; float _dimension = 0;
Rect panelDimensions = gameObject.GetComponent<RectTransform>().rect; Rect panelDimensions = gameObject.GetComponent<RectTransform>().rect;
float currentYPosition = 0; 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++) for (int i = 0; i < _screensContainer.transform.childCount; i++)
{ {
@ -109,25 +113,6 @@ namespace UnityEngine.UI.Extensions
_screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(0f, _dimension); _screensContainer.GetComponent<RectTransform>().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;
}
/// <summary> /// <summary>
/// Add a new child to this Scroll Snap and recalculate it's children /// Add a new child to this Scroll Snap and recalculate it's children
/// </summary> /// </summary>
@ -137,7 +122,6 @@ namespace UnityEngine.UI.Extensions
_scroll_rect.verticalNormalizedPosition = 0; _scroll_rect.verticalNormalizedPosition = 0;
GO.transform.SetParent(_screensContainer); GO.transform.SetParent(_screensContainer);
DistributePages(); DistributePages();
UpdateChildPositions();
_scroll_rect.verticalNormalizedPosition = (float)(_currentScreen) / (_screens - 1); _scroll_rect.verticalNormalizedPosition = (float)(_currentScreen) / (_screens - 1);
} }
@ -169,7 +153,6 @@ namespace UnityEngine.UI.Extensions
i++; i++;
} }
DistributePages(); DistributePages();
UpdateChildPositions();
if (_currentScreen > _screens - 1) if (_currentScreen > _screens - 1)
{ {