2015-02-03 03:25:41 +08:00
|
|
|
/// Credit BinaryX
|
|
|
|
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1945602
|
|
|
|
/// Updated by ddreaper - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect.
|
|
|
|
|
|
|
|
using System;
|
2016-11-16 02:09:13 +08:00
|
|
|
using UnityEngine.Events;
|
2015-02-03 03:25:41 +08:00
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
namespace UnityEngine.UI.Extensions
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2016-05-24 03:57:56 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
[RequireComponent(typeof(ScrollRect))]
|
2015-09-20 07:24:17 +08:00
|
|
|
[AddComponentMenu("Layout/Extensions/Horizontal Scroll Snap")]
|
2016-12-05 18:15:48 +08:00
|
|
|
public class HorizontalScrollSnap : ScrollSnapBase, IEndDragHandler
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
2016-05-18 05:37:01 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
// Use this for initialization
|
2016-11-16 07:48:44 +08:00
|
|
|
void Awake()
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
|
|
|
_scroll_rect = gameObject.GetComponent<ScrollRect>();
|
2016-05-28 07:04:08 +08:00
|
|
|
|
|
|
|
if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar)
|
|
|
|
{
|
2016-11-16 07:48:44 +08:00
|
|
|
Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results");
|
2016-05-28 07:04:08 +08:00
|
|
|
}
|
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
_screensContainer = _scroll_rect.content;
|
2016-11-15 22:52:34 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
DistributePages();
|
2015-02-03 03:25:41 +08:00
|
|
|
|
2016-11-16 07:48:44 +08:00
|
|
|
if (NextButton)
|
|
|
|
NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); });
|
|
|
|
|
|
|
|
if (PrevButton)
|
|
|
|
PrevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousScreen(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
UpdateChildPositions();
|
2015-02-03 07:07:31 +08:00
|
|
|
_lerp = false;
|
2016-11-15 21:36:50 +08:00
|
|
|
_currentScreen = StartingScreen - 1;
|
2015-02-03 03:25:41 +08:00
|
|
|
|
2016-11-15 21:36:50 +08:00
|
|
|
_scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1);
|
2015-02-03 03:25:41 +08:00
|
|
|
|
2016-05-18 05:37:01 +08:00
|
|
|
ChangeBulletsInfo(_currentScreen);
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
2015-02-03 03:25:41 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
void Update()
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
//Three Use cases:
|
|
|
|
//1: Swipe Next - FastSwipeNextPrev
|
|
|
|
//2: Swipe next while in motion - FastSwipeNextPrev
|
|
|
|
//3: Swipe to end - default
|
|
|
|
|
|
|
|
//If lerping, NOT swiping and (!fastswipenextprev & velocity < 200)
|
|
|
|
//Aim is to settle on target "page"
|
|
|
|
if (!_lerp && _scroll_rect.velocity == Vector2.zero)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (_lerp)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2016-05-17 20:26:50 +08:00
|
|
|
_screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * Time.deltaTime);
|
2016-11-16 07:48:44 +08:00
|
|
|
if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f)
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
|
|
|
_lerp = false;
|
2016-11-16 02:09:13 +08:00
|
|
|
EndScreenChange();
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
2016-12-05 04:06:03 +08:00
|
|
|
}
|
|
|
|
//If the container is moving faster than the threshold, then just update the pages as they pass
|
|
|
|
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));
|
|
|
|
if (_currentScreen != _previousScreen)
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
_previousScreen = _currentScreen;
|
|
|
|
ChangeBulletsInfo(_currentScreen);
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
2016-12-05 04:06:03 +08:00
|
|
|
else if (!_pointerDown)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
ScrollToClosestElement();
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:15:48 +08:00
|
|
|
//used for changing between screen resolutions
|
2015-02-03 07:07:31 +08:00
|
|
|
private void DistributePages()
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
int _offset = 0;
|
2016-11-15 22:52:34 +08:00
|
|
|
float _dimension = 0;
|
2016-09-03 05:38:06 +08:00
|
|
|
Rect panelDimensions = gameObject.GetComponent<RectTransform>().rect;
|
2016-11-15 22:52:34 +08:00
|
|
|
float currentXPosition = 0;
|
|
|
|
var pageStepValue = (int)panelDimensions.width * ((PageStep == 0) ? 3 : PageStep);
|
2016-11-16 02:09:13 +08:00
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < _screensContainer.transform.childCount; i++)
|
|
|
|
{
|
|
|
|
RectTransform child = _screensContainer.transform.GetChild(i).gameObject.GetComponent<RectTransform>();
|
2016-11-15 22:52:34 +08:00
|
|
|
currentXPosition = _offset + (int)(i * pageStepValue);
|
2016-09-03 05:38:06 +08:00
|
|
|
child.sizeDelta = new Vector2(panelDimensions.width, panelDimensions.height);
|
2015-02-03 07:07:31 +08:00
|
|
|
child.anchoredPosition = new Vector2(currentXPosition, 0f);
|
2016-09-03 05:38:06 +08:00
|
|
|
child.anchorMin = new Vector2(0f, child.anchorMin.y);
|
|
|
|
child.anchorMax = new Vector2(0f, child.anchorMax.y);
|
|
|
|
child.pivot = new Vector2(0f, child.pivot.y);
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_dimension = currentXPosition + _offset * -1;
|
|
|
|
|
|
|
|
_screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(_dimension, 0f);
|
2016-11-16 07:48:44 +08:00
|
|
|
}
|
2016-05-24 03:57:56 +08:00
|
|
|
|
2016-11-16 07:48:44 +08:00
|
|
|
void UpdateChildPositions()
|
|
|
|
{
|
2016-05-24 03:57:56 +08:00
|
|
|
_screens = _screensContainer.childCount;
|
|
|
|
|
2016-12-05 04:06:03 +08:00
|
|
|
_positions = new Vector3[_screens];
|
2016-05-24 03:57:56 +08:00
|
|
|
|
|
|
|
if (_screens > 0)
|
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
for (int i = 0; i < _screens; ++i)
|
2016-05-24 03:57:56 +08:00
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
_scroll_rect.horizontalNormalizedPosition = (float)i / (float)(_screens - 1);
|
|
|
|
_positions[i] = _screensContainer.localPosition;
|
2016-05-24 03:57:56 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 04:06:03 +08:00
|
|
|
|
|
|
|
//debug visible
|
|
|
|
_visiblePositions = _positions;
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
|
|
|
|
2016-12-05 18:15:48 +08:00
|
|
|
/// <summary>
|
2016-05-24 03:57:56 +08:00
|
|
|
/// 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.horizontalNormalizedPosition = 0;
|
|
|
|
GO.transform.SetParent(_screensContainer);
|
|
|
|
DistributePages();
|
2016-12-05 18:15:48 +08:00
|
|
|
UpdateChildPositions();
|
2016-05-24 03:57:56 +08:00
|
|
|
|
|
|
|
_scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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.horizontalNormalizedPosition = 0;
|
|
|
|
var children = _screensContainer.transform;
|
|
|
|
int i = 0;
|
|
|
|
foreach (Transform child in children)
|
|
|
|
{
|
|
|
|
if (i == index)
|
|
|
|
{
|
|
|
|
child.SetParent(null);
|
|
|
|
ChildRemoved = child.gameObject;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2016-12-05 18:15:48 +08:00
|
|
|
|
2016-05-24 03:57:56 +08:00
|
|
|
DistributePages();
|
2016-12-05 18:15:48 +08:00
|
|
|
UpdateChildPositions();
|
|
|
|
|
2016-05-24 03:57:56 +08:00
|
|
|
if (_currentScreen > _screens - 1)
|
|
|
|
{
|
|
|
|
_currentScreen = _screens - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen) / (_screens - 1);
|
|
|
|
}
|
|
|
|
|
2015-02-03 07:07:31 +08:00
|
|
|
#region Interfaces
|
2016-12-05 04:06:03 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Release screen to swipe
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="eventData"></param>
|
2015-02-03 07:07:31 +08:00
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
if (_scroll_rect.horizontal)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
if (UseFastSwipe)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
//If using fastswipe - then a swipe does page next / previous
|
|
|
|
if (_scroll_rect.velocity.x > SwipeVelocityThreshold)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
_scroll_rect.velocity = Vector3.zero;
|
2015-02-03 07:07:31 +08:00
|
|
|
if (_startPosition.x - _screensContainer.localPosition.x > 0)
|
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
NextScreen();
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
PreviousScreen();
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-05 04:06:03 +08:00
|
|
|
ScrollToClosestElement();
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:15:48 +08:00
|
|
|
#endregion
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
|
|
|
}
|