Resolved issue with HSS/VSS not resizing when Rect/Scene changes

Updated HSS/VSS Visibility logic so it's more refined.

--HG--
branch : develop_5.3
release
Simon Jackson 2016-12-31 15:14:39 +00:00
parent 909589e544
commit e0c3864fb7
3 changed files with 129 additions and 43 deletions

View File

@ -13,10 +13,9 @@ namespace UnityEngine.UI.Extensions
{ {
void Start() void Start()
{ {
isVertical = false; _isVertical = false;
childAnchorPoint = new Vector2(0, 0.5f); _childAnchorPoint = new Vector2(0, 0.5f);
DistributePages(); DistributePages();
if(MaskArea) CalculateVisible();
_lerp = false; _lerp = false;
_currentPage = StartingScreen; _currentPage = StartingScreen;
SetScrollContainerPosition(); SetScrollContainerPosition();
@ -26,6 +25,13 @@ namespace UnityEngine.UI.Extensions
{ {
if (!_lerp && _scroll_rect.velocity == Vector2.zero) if (!_lerp && _scroll_rect.velocity == Vector2.zero)
{ {
if (!_settled && !_pointerDown)
{
if (!IsRectSettledOnaPage(_screensContainer.localPosition))
{
ScrollToClosestElement();
}
}
return; return;
} }
else if (_lerp) else if (_lerp)
@ -44,14 +50,19 @@ namespace UnityEngine.UI.Extensions
if (!_pointerDown && (_scroll_rect.velocity.x > 0.01 || _scroll_rect.velocity.x < 0.01)) if (!_pointerDown && (_scroll_rect.velocity.x > 0.01 || _scroll_rect.velocity.x < 0.01))
{ {
// if the pointer is released and is moving slower than the threshold, then just land on a page // if the pointer is released and is moving slower than the threshold, then just land on a page
if ((_scroll_rect.velocity.x > 0 && _scroll_rect.velocity.x < SwipeVelocityThreshold) || if (IsRectMovingFasterThanThreshold(0))
(_scroll_rect.velocity.x < 0 && _scroll_rect.velocity.x > -SwipeVelocityThreshold))
{ {
ScrollToClosestElement(); ScrollToClosestElement();
} }
} }
} }
private bool IsRectMovingFasterThanThreshold(float startingSpeed)
{
return (_scroll_rect.velocity.x > startingSpeed && _scroll_rect.velocity.x < SwipeVelocityThreshold) ||
(_scroll_rect.velocity.x < startingSpeed && _scroll_rect.velocity.x > -SwipeVelocityThreshold);
}
private void DistributePages() private void DistributePages()
{ {
_screens = _screensContainer.childCount; _screens = _screensContainer.childCount;
@ -70,7 +81,7 @@ namespace UnityEngine.UI.Extensions
currentXPosition = _offset + (int)(i * pageStepValue); currentXPosition = _offset + (int)(i * pageStepValue);
child.sizeDelta = new Vector2(panelDimensions.width, panelDimensions.height); child.sizeDelta = new Vector2(panelDimensions.width, panelDimensions.height);
child.anchoredPosition = new Vector2(currentXPosition, 0f); child.anchoredPosition = new Vector2(currentXPosition, 0f);
child.anchorMin = child.anchorMax = child.pivot = childAnchorPoint; child.anchorMin = child.anchorMax = child.pivot = _childAnchorPoint;
} }
_dimension = currentXPosition + _offset * -1; _dimension = currentXPosition + _offset * -1;
@ -135,9 +146,18 @@ namespace UnityEngine.UI.Extensions
{ {
_lerp = false; _lerp = false;
DistributePages(); DistributePages();
if (MaskArea) UpdateVisible();
SetScrollContainerPosition(); SetScrollContainerPosition();
} }
private void OnRectTransformDimensionsChange()
{
if (_childAnchorPoint != Vector2.zero)
{
UpdateLayout();
}
}
#region Interfaces #region Interfaces
/// <summary> /// <summary>
/// Release screen to swipe /// Release screen to swipe

View File

@ -7,23 +7,25 @@ namespace UnityEngine.UI.Extensions
public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IPointerDownHandler, IPointerUpHandler public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IPointerDownHandler, IPointerUpHandler
{ {
internal RectTransform _screensContainer; internal RectTransform _screensContainer;
internal bool isVertical; internal bool _isVertical;
internal int _screens = 1; internal int _screens = 1;
internal float _scrollStartPosition; internal float _scrollStartPosition;
internal float _childSize; internal float _childSize;
private float _childPos; private float _childPos, _maskSize;
internal Vector2 childAnchorPoint; internal Vector2 _childAnchorPoint;
internal ScrollRect _scroll_rect; internal ScrollRect _scroll_rect;
internal Vector3 _lerp_target; internal Vector3 _lerp_target;
internal bool _lerp; internal bool _lerp;
internal bool _pointerDown = false; internal bool _pointerDown = false;
internal bool _settled = true;
internal Vector3 _startPosition = new Vector3(); internal Vector3 _startPosition = new Vector3();
[Tooltip("The currently active page")] [Tooltip("The currently active page")]
internal int _currentPage; internal int _currentPage;
internal int _previousPage; internal int _previousPage;
internal int HalfNoVisibleItems; internal int _halfNoVisibleItems;
private int _bottomItem, _topItem;
[Serializable] [Serializable]
public class SelectionChangeStartEvent : UnityEvent { } public class SelectionChangeStartEvent : UnityEvent { }
@ -76,7 +78,7 @@ namespace UnityEngine.UI.Extensions
} }
internal set internal set
{ {
if (value != _currentPage) if (value != _currentPage && value >= 0 && value < _screensContainer.childCount)
{ {
_previousPage = _currentPage; _previousPage = _currentPage;
_currentPage = value; _currentPage = value;
@ -94,14 +96,17 @@ namespace UnityEngine.UI.Extensions
public GameObject[] ChildObjects; public GameObject[] ChildObjects;
[SerializeField] [SerializeField]
[Tooltip("Event fires when a user starts to change the selection")]
private SelectionChangeStartEvent m_OnSelectionChangeStartEvent = new SelectionChangeStartEvent(); private SelectionChangeStartEvent m_OnSelectionChangeStartEvent = new SelectionChangeStartEvent();
public SelectionChangeStartEvent OnSelectionChangeStartEvent { get { return m_OnSelectionChangeStartEvent; } set { m_OnSelectionChangeStartEvent = value; } } public SelectionChangeStartEvent OnSelectionChangeStartEvent { get { return m_OnSelectionChangeStartEvent; } set { m_OnSelectionChangeStartEvent = value; } }
[SerializeField] [SerializeField]
[Tooltip("Event fires as the page changes, while dragging or jumping")]
private SelectionPageChangedEvent m_OnSelectionPageChangedEvent = new SelectionPageChangedEvent(); private SelectionPageChangedEvent m_OnSelectionPageChangedEvent = new SelectionPageChangedEvent();
public SelectionPageChangedEvent OnSelectionPageChangedEvent { get { return m_OnSelectionPageChangedEvent; } set { m_OnSelectionPageChangedEvent = value; } } public SelectionPageChangedEvent OnSelectionPageChangedEvent { get { return m_OnSelectionPageChangedEvent; } set { m_OnSelectionPageChangedEvent = value; } }
[SerializeField] [SerializeField]
[Tooltip("Event fires when the page settles after a user has dragged")]
private SelectionChangeEndEvent m_OnSelectionChangeEndEvent = new SelectionChangeEndEvent(); private SelectionChangeEndEvent m_OnSelectionChangeEndEvent = new SelectionChangeEndEvent();
public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } }
@ -182,33 +187,43 @@ namespace UnityEngine.UI.Extensions
} }
} }
internal void CalculateVisible()
{
float MaskSize = isVertical ? MaskArea.rect.height : MaskArea.rect.width;
HalfNoVisibleItems = (int)Math.Round(MaskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2 + 2;
int StartingItemsBefore = StartingScreen - HalfNoVisibleItems < 0 ? 0 : HalfNoVisibleItems;
int StartingItemsAfter = _screensContainer.childCount - StartingScreen < HalfNoVisibleItems ? _screensContainer.childCount - StartingScreen : HalfNoVisibleItems;
for (int i = StartingScreen - StartingItemsBefore; i < StartingScreen + StartingItemsAfter - 1; i++)
{
ChildObjects[i].SetActive(true);
}
}
internal void UpdateVisible() internal void UpdateVisible()
{ {
//If there are no objects in the scene, exit //If there are no objects in the scene or a mask, exit
if (ChildObjects == null || ChildObjects.Length < 1 || _screensContainer.childCount < 1) return; if (!MaskArea && (ChildObjects == null || ChildObjects.Length < 1 || _screensContainer.childCount < 1)) return;
int BottomItem = _currentPage - HalfNoVisibleItems < 0 ? 0 : HalfNoVisibleItems; _maskSize = _isVertical ? MaskArea.rect.height : MaskArea.rect.width;
int TopItem = _screensContainer.childCount - _currentPage < HalfNoVisibleItems ? _screensContainer.childCount - _currentPage : HalfNoVisibleItems; _halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2;
_bottomItem = _topItem = 0;
for (int i = CurrentPage - BottomItem; i < CurrentPage + TopItem; i++) //work out how many items below the current page can be visible
for (int i = _halfNoVisibleItems + 1; i > 0 ; i--)
{
_bottomItem = _currentPage - i < 0 ? 0 : i;
if (_bottomItem > 0) break;
}
//work out how many items above the current page can be visible
for (int i = _halfNoVisibleItems + 1; i > 0; i--)
{
_topItem = _screensContainer.childCount - _currentPage - i < 0 ? 0 : i;
if (_topItem > 0) break;
}
//Set the active items active
for (int i = CurrentPage - _bottomItem; i < CurrentPage + _topItem; i++)
{
try
{ {
ChildObjects[i].SetActive(true); ChildObjects[i].SetActive(true);
} }
catch
{
Debug.Log("Failed to setactive child [" + i + "]");
}
}
if (_screensContainer.childCount - _currentPage > HalfNoVisibleItems + 1) ChildObjects[CurrentPage + TopItem + 1].SetActive(false); //Deactivate items out of visibility at the bottom of the ScrollRect Mask (only on scroll)
if(_currentPage - HalfNoVisibleItems > 0) ChildObjects[CurrentPage - BottomItem - 1].SetActive(false); if (_currentPage > _halfNoVisibleItems) ChildObjects[CurrentPage - _bottomItem].SetActive(false);
//Deactivate items out of visibility at the top of the ScrollRect Mask (only on scroll)
if (_screensContainer.childCount - _currentPage > _topItem) ChildObjects[CurrentPage + _topItem].SetActive(false);
} }
@ -256,17 +271,39 @@ namespace UnityEngine.UI.Extensions
} }
} }
/// <summary>
/// Gets the closest page for the current Scroll Rect container position
/// </summary>
/// <param name="pos">Position to test, normally the Scroll Rect container Local position</param>
/// <returns>Closest Page number (zero indexed array value)</returns>
internal int GetPageforPosition(Vector3 pos) internal int GetPageforPosition(Vector3 pos)
{ {
return isVertical ? return _isVertical ?
-(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) : -(int)Math.Round((pos.y - _scrollStartPosition) / _childSize) :
-(int)Math.Round((pos.x - _scrollStartPosition) / _childSize); -(int)Math.Round((pos.x - _scrollStartPosition) / _childSize);
} }
/// <summary>
/// Validates if the current Scroll Rect container position is within the bounds for a page
/// </summary>
/// <param name="pos">Position to test, normally the Scroll Rect container Local position</param>
/// <returns>True / False, is the position in the bounds of a page</returns>
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);
}
/// <summary>
/// Returns the local position for a child page based on the required page number
/// </summary>
/// <param name="page">Page that the position is required for (Zero indexed array value)</param>
/// <param name="target">Outputs the local position for the selected page</param>
internal void GetPositionforPage(int page, ref Vector3 target) internal void GetPositionforPage(int page, ref Vector3 target)
{ {
_childPos = -_childSize * page; _childPos = -_childSize * page;
if (isVertical) if (_isVertical)
{ {
target.y = _childPos + _scrollStartPosition; target.y = _childPos + _scrollStartPosition;
} }
@ -276,6 +313,9 @@ namespace UnityEngine.UI.Extensions
} }
} }
/// <summary>
/// Updates the _Lerp target to the closest page and updates the pagination bullets. Each control's update loop will then handle the move.
/// </summary>
internal void ScrollToClosestElement() internal void ScrollToClosestElement()
{ {
_lerp = true; _lerp = true;
@ -283,8 +323,10 @@ namespace UnityEngine.UI.Extensions
GetPositionforPage(_currentPage, ref _lerp_target); GetPositionforPage(_currentPage, ref _lerp_target);
ChangeBulletsInfo(_currentPage); ChangeBulletsInfo(_currentPage);
} }
/// <summary>
//changes the bullets on the bottom of the page - pagination /// changes the bullets on the bottom of the page - pagination
/// </summary>
/// <param name="targetScreen"></param>
internal void ChangeBulletsInfo(int targetScreen) internal void ChangeBulletsInfo(int targetScreen)
{ {
if (Pagination) if (Pagination)
@ -314,6 +356,14 @@ namespace UnityEngine.UI.Extensions
{ {
MaskBuffer = 1; MaskBuffer = 1;
} }
if (PageStep < 0)
{
PageStep = 0;
}
if (PageStep > 8)
{
PageStep = 9;
}
} }
/// <summary> /// <summary>
@ -338,6 +388,7 @@ namespace UnityEngine.UI.Extensions
internal void EndScreenChange() internal void EndScreenChange()
{ {
OnSelectionChangeEndEvent.Invoke(_currentPage); OnSelectionChangeEndEvent.Invoke(_currentPage);
_settled = true;
} }
#region Interfaces #region Interfaces
@ -347,6 +398,7 @@ namespace UnityEngine.UI.Extensions
/// <param name="eventData"></param> /// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData) public void OnBeginDrag(PointerEventData eventData)
{ {
_settled = false;
StartScreenChange(); StartScreenChange();
_startPosition = _screensContainer.localPosition; _startPosition = _screensContainer.localPosition;
} }

View File

@ -13,19 +13,23 @@ namespace UnityEngine.UI.Extensions
{ {
void Start() void Start()
{ {
isVertical = true; _isVertical = true;
childAnchorPoint = new Vector2(0.5f,0); _childAnchorPoint = new Vector2(0.5f,0);
DistributePages();
if(MaskArea) CalculateVisible();
_lerp = false;
_currentPage = StartingScreen; _currentPage = StartingScreen;
SetScrollContainerPosition(); UpdateLayout();
} }
void Update() void Update()
{ {
if (!_lerp && _scroll_rect.velocity == Vector2.zero) if (!_lerp && _scroll_rect.velocity == Vector2.zero)
{ {
if (!_settled && !_pointerDown)
{
if (!IsRectSettledOnaPage(_screensContainer.localPosition))
{
ScrollToClosestElement();
}
}
return; return;
} }
else if (_lerp) else if (_lerp)
@ -70,7 +74,7 @@ namespace UnityEngine.UI.Extensions
currentYPosition = _offset + i * pageStepValue; currentYPosition = _offset + i * pageStepValue;
child.sizeDelta = new Vector2(panelDimensions.width, panelDimensions.height); child.sizeDelta = new Vector2(panelDimensions.width, panelDimensions.height);
child.anchoredPosition = new Vector2(0f, currentYPosition); child.anchoredPosition = new Vector2(0f, currentYPosition);
child.anchorMin = child.anchorMax = child.pivot = childAnchorPoint; child.anchorMin = child.anchorMax = child.pivot = _childAnchorPoint;
} }
_dimension = currentYPosition + _offset * -1; _dimension = currentYPosition + _offset * -1;
@ -136,8 +140,18 @@ namespace UnityEngine.UI.Extensions
{ {
_lerp = false; _lerp = false;
DistributePages(); DistributePages();
if (MaskArea) UpdateVisible();
SetScrollContainerPosition(); SetScrollContainerPosition();
} }
private void OnRectTransformDimensionsChange()
{
if (_childAnchorPoint != Vector2.zero)
{
UpdateLayout();
}
}
#region Interfaces #region Interfaces
/// <summary> /// <summary>
/// Release screen to swipe /// Release screen to swipe