Experimental ScrollSnap base with support for the UI Infinite scroll component

* Does not yet work with buttons
* GoTo screen not tested

New features only activate with a UI Infinite Scroll attached.
release
Simon (Darkside) Jackson 2017-10-19 13:11:09 +01:00
parent 1f46a4ca5f
commit 54488e1089
1 changed files with 43 additions and 11 deletions

View File

@ -31,6 +31,9 @@ namespace UnityEngine.UI.Extensions
internal int _previousPage; internal int _previousPage;
internal int _halfNoVisibleItems; internal int _halfNoVisibleItems;
internal bool _moveStarted; internal bool _moveStarted;
internal bool _isInfinate; // Is a UI Infinate scroller attached to the control
internal int _infiniteWindow; // The infinite window the control is in
internal float _infiniteOffset; // How much to offset a repositioning
private int _bottomItem, _topItem; private int _bottomItem, _topItem;
[Serializable] [Serializable]
@ -85,6 +88,25 @@ namespace UnityEngine.UI.Extensions
internal set internal set
{ {
if (_isInfinate)
{
//Work out which infinate window we are in
_infiniteWindow = value / _screensContainer.childCount;
//Invert the value if negative and differentiate from Window 0
_infiniteWindow = value < 0 ? (-_infiniteWindow) + 1 : _infiniteWindow;
//Multiplying values by zero doesn't help, so start at Window 1
//_infiniteWindow += 1;
//Calculate the page within the child count range
value = value % _screensContainer.childCount;
if (value < 0)
{
value = _screensContainer.childCount + value;
}
else if (value > _screensContainer.childCount - 1)
{
value = value - _screensContainer.childCount;
}
}
if ((value != _currentPage && value >= 0 && value < _screensContainer.childCount) || (value == 0 && _screensContainer.childCount == 0)) if ((value != _currentPage && value >= 0 && value < _screensContainer.childCount) || (value == 0 && _screensContainer.childCount == 0))
{ {
_previousPage = _currentPage; _previousPage = _currentPage;
@ -156,6 +178,8 @@ namespace UnityEngine.UI.Extensions
if (PrevButton) if (PrevButton)
PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); }); PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); });
_isInfinate = GetComponent<UI_InfiniteScroll>() != null;
} }
internal void InitialiseChildObjects() internal void InitialiseChildObjects()
@ -263,7 +287,7 @@ namespace UnityEngine.UI.Extensions
//Function for switching screens with buttons //Function for switching screens with buttons
public void NextScreen() public void NextScreen()
{ {
if (_currentPage < _screens - 1) if (_currentPage < _screens - 1 || _isInfinate)
{ {
if (!_lerp) StartScreenChange(); if (!_lerp) StartScreenChange();
@ -272,12 +296,13 @@ namespace UnityEngine.UI.Extensions
GetPositionforPage(_currentPage, ref _lerp_target); GetPositionforPage(_currentPage, ref _lerp_target);
ScreenChange(); ScreenChange();
} }
} }
//Function for switching screens with buttons //Function for switching screens with buttons
public void PreviousScreen() public void PreviousScreen()
{ {
if (_currentPage > 0) if (_currentPage > 0 || _isInfinate)
{ {
if (!_lerp) StartScreenChange(); if (!_lerp) StartScreenChange();
@ -340,11 +365,14 @@ namespace UnityEngine.UI.Extensions
_childPos = -_childSize * page; _childPos = -_childSize * page;
if (_isVertical) if (_isVertical)
{ {
target.y = _childPos + _scrollStartPosition; _infiniteOffset = _screensContainer.localPosition.y < 0 ? -_screensContainer.sizeDelta.y * _infiniteWindow : _screensContainer.sizeDelta.y * _infiniteWindow;
target.y = _childPos + _scrollStartPosition + _infiniteOffset;
} }
else else
{ {
target.x = _childPos + _scrollStartPosition; _infiniteOffset = _screensContainer.localPosition.x < 0 ? -_screensContainer.sizeDelta.x * _infiniteWindow : _screensContainer.sizeDelta.x * _infiniteWindow;
_infiniteOffset = _infiniteOffset == 0 ? 0 : _infiniteOffset < 0 ? _infiniteOffset - _childSize * _infiniteWindow : _infiniteOffset + _childSize * _infiniteWindow;
target.x = _childPos + _scrollStartPosition + _infiniteOffset;
} }
} }
@ -388,6 +416,9 @@ namespace UnityEngine.UI.Extensions
/// </summary> /// </summary>
/// <param name="targetScreen"></param> /// <param name="targetScreen"></param>
private void ToggleNavigationButtons(int targetScreen) private void ToggleNavigationButtons(int targetScreen)
{
//Ifthis is using an Infinate Scoll, then don't disable
if (!_isInfinate)
{ {
if (PrevButton) if (PrevButton)
{ {
@ -399,6 +430,7 @@ namespace UnityEngine.UI.Extensions
NextButton.GetComponent<Button>().interactable = targetScreen < _screensContainer.transform.childCount - 1; NextButton.GetComponent<Button>().interactable = targetScreen < _screensContainer.transform.childCount - 1;
} }
} }
}
private void OnValidate() private void OnValidate()
{ {