com.unity.uiextensions/Scripts/Layout/VerticalScrollSnap.cs

167 lines
6.5 KiB
C#
Raw Normal View History

/// Credit BinaryX, SimonDarksideJ
2015-08-30 18:49:19 +08:00
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1945602
/// Updated by SimonDarksideJ - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect.
/// Updated by SimonDarksideJ - major refactoring on updating current position and scroll management
2015-08-30 18:49:19 +08:00
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(ScrollRect))]
[AddComponentMenu("Layout/Extensions/Vertical Scroll Snap")]
public class VerticalScrollSnap : ScrollSnapBase, IEndDragHandler
2015-08-30 18:49:19 +08:00
{
void Start()
2015-08-30 18:49:19 +08:00
{
isVertical = true;
2015-08-30 18:49:19 +08:00
DistributePages();
if(MaskArea) CalculateVisible();
2015-08-30 18:49:19 +08:00
_lerp = false;
_currentPage = StartingScreen - 1;
SetScrollContainerPosition();
2015-08-30 18:49:19 +08:00
}
void Update()
{
if (!_lerp && _scroll_rect.velocity == Vector2.zero)
{
return;
}
else if (_lerp)
2015-08-30 18:49:19 +08:00
{
_screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * Time.deltaTime);
if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f)
2015-08-30 18:49:19 +08:00
{
_lerp = false;
EndScreenChange();
2015-08-30 18:49:19 +08:00
}
}
CurrentPage = GetPageforPosition(_screensContainer.localPosition);
//If the container is moving check if it needs to settle on a page
if (!_pointerDown && (_scroll_rect.velocity.y > 0.01 || _scroll_rect.velocity.y < -0.01))
2015-08-30 18:49:19 +08:00
{
// if the pointer is released and is moving slower than the threshold, then just land on a page
if ((_scroll_rect.velocity.y > 0 && _scroll_rect.velocity.y < SwipeVelocityThreshold) ||
(_scroll_rect.velocity.y < 0 && _scroll_rect.velocity.y > -SwipeVelocityThreshold))
{
ScrollToClosestElement();
}
2015-08-30 18:49:19 +08:00
}
2015-08-30 18:49:19 +08:00
}
//used for changing between screen resolutions
public void DistributePages()
2015-08-30 18:49:19 +08:00
{
_screens = _screensContainer.childCount;
2015-08-30 18:49:19 +08:00
float _offset = 0;
float _dimension = 0;
Rect panelDimensions = gameObject.GetComponent<RectTransform>().rect;
2015-08-30 18:49:19 +08:00
float currentYPosition = 0;
var pageStepValue = _childSize = (int)panelDimensions.height * ((PageStep == 0) ? 3 : PageStep);
2015-08-30 18:49:19 +08:00
for (int i = 0; i < _screensContainer.transform.childCount; i++)
{
RectTransform child = _screensContainer.transform.GetChild(i).gameObject.GetComponent<RectTransform>();
currentYPosition = _offset + i * pageStepValue;
child.sizeDelta = new Vector2(panelDimensions.width, panelDimensions.height);
child.anchoredPosition = new Vector2(0f, currentYPosition);
child.anchorMin = new Vector2(child.anchorMin.x, 0f);
child.anchorMax = new Vector2(child.anchorMax.x, 0f);
child.pivot = new Vector2(child.pivot.x, 0f);
2015-08-30 18:49:19 +08:00
}
_dimension = currentYPosition + _offset * -1;
_screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(0f, _dimension);
}
/// <summary>
/// Add a new child to this Scroll Snap and recalculate it's children
/// </summary>
/// <param name="GO">GameObject to add to the ScrollSnap</param>
public void AddChild(GameObject GO)
{
_scroll_rect.verticalNormalizedPosition = 0;
GO.transform.SetParent(_screensContainer);
InitialiseChildObjectsFromScene();
DistributePages();
if (MaskArea) UpdateVisible();
SetScrollContainerPosition();
}
/// <summary>
/// Remove a new child to this Scroll Snap and recalculate it's children
/// *Note, this is an index address (0-x)
/// </summary>
/// <param name="index"></param>
/// <param name="ChildRemoved"></param>
public void RemoveChild(int index, out GameObject ChildRemoved)
{
ChildRemoved = null;
if (index < 0 || index > _screensContainer.childCount)
{
return;
}
_scroll_rect.verticalNormalizedPosition = 0;
Transform child = _screensContainer.transform.GetChild(index);
child.SetParent(null);
ChildRemoved = child.gameObject;
InitialiseChildObjectsFromScene();
DistributePages();
if (MaskArea) UpdateVisible();
if (_currentPage > _screens - 1)
{
CurrentPage = _screens - 1;
}
SetScrollContainerPosition();
}
private void SetScrollContainerPosition()
{
_scrollStartPosition = _screensContainer.localPosition.y;
_scroll_rect.verticalNormalizedPosition = (float)(_currentPage) / (_screens - 1);
}
2015-08-30 18:49:19 +08:00
#region Interfaces
/// <summary>
/// Release screen to swipe
/// </summary>
/// <param name="eventData"></param>
2015-08-30 18:49:19 +08:00
public void OnEndDrag(PointerEventData eventData)
{
if (_scroll_rect.vertical)
{
if (UseFastSwipe)
{
//If using fastswipe - then a swipe does page next / previous
if ((_scroll_rect.velocity.y > 0 && _scroll_rect.velocity.y > FastSwipeThreshold) ||
_scroll_rect.velocity.y < 0 && _scroll_rect.velocity.y < -FastSwipeThreshold)
2015-08-30 18:49:19 +08:00
{
_scroll_rect.velocity = Vector3.zero;
2015-08-30 18:49:19 +08:00
if (_startPosition.y - _screensContainer.localPosition.y > 0)
{
NextScreen();
2015-08-30 18:49:19 +08:00
}
else
{
PreviousScreen();
2015-08-30 18:49:19 +08:00
}
}
else
{
ScrollToClosestElement();
2015-08-30 18:49:19 +08:00
}
}
}
}
#endregion
}
}