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
2020-07-09 03:38:28 +08:00
/// Updated by SimonDarksideJ - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect.
2015-02-03 03:25:41 +08:00
2019-07-11 00:36:36 +08:00
using System ;
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")]
2020-05-22 19:15:58 +08:00
public class HorizontalScrollSnap : ScrollSnapBase
2015-02-03 07:07:31 +08:00
{
2020-09-28 21:00:09 +08:00
private bool updated = true ;
2016-12-19 06:51:16 +08:00
void Start ( )
2015-02-03 07:07:31 +08:00
{
2016-12-31 23:14:39 +08:00
_isVertical = false ;
_childAnchorPoint = new Vector2 ( 0 , 0.5f ) ;
2016-12-30 06:03:04 +08:00
_currentPage = StartingScreen ;
2017-07-27 13:44:18 +08:00
panelDimensions = gameObject . GetComponent < RectTransform > ( ) . rect ;
2017-01-01 01:01:49 +08:00
UpdateLayout ( ) ;
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
{
2020-09-28 21:00:09 +08:00
updated = false ;
2020-09-29 06:59:47 +08:00
if ( ! _lerp & & ( _scroll_rect . velocity = = Vector2 . zero & & _scroll_rect . inertia ) )
2016-12-05 04:06:03 +08:00
{
2016-12-31 23:14:39 +08:00
if ( ! _settled & & ! _pointerDown )
{
2020-06-22 22:48:16 +08:00
if ( ! IsRectSettledOnaPage ( _screensContainer . anchoredPosition ) )
2016-12-31 23:14:39 +08:00
{
ScrollToClosestElement ( ) ;
}
}
2016-12-05 04:06:03 +08:00
return ;
}
else if ( _lerp )
2015-02-03 03:25:41 +08:00
{
2020-06-22 22:48:16 +08:00
_screensContainer . anchoredPosition = Vector3 . Lerp ( _screensContainer . anchoredPosition , _lerp_target , transitionSpeed * ( UseTimeScale ? Time . deltaTime : Time . unscaledDeltaTime ) ) ;
2020-09-29 06:43:03 +08:00
if ( Vector3 . Distance ( _screensContainer . anchoredPosition , _lerp_target ) < 0.2f )
2015-02-03 07:07:31 +08:00
{
2020-06-22 22:48:16 +08:00
_screensContainer . anchoredPosition = _lerp_target ;
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
}
2016-12-22 00:52:19 +08:00
2020-09-25 21:40:59 +08:00
if ( UseHardSwipe ) return ;
2020-06-22 22:48:16 +08:00
CurrentPage = GetPageforPosition ( _screensContainer . anchoredPosition ) ;
2017-01-04 21:18:28 +08:00
2016-12-22 00:52:19 +08:00
//If the container is moving check if it needs to settle on a page
2017-01-04 21:18:28 +08:00
if ( ! _pointerDown )
2015-02-03 03:25:41 +08:00
{
2018-02-19 23:23:38 +08:00
if ( _scroll_rect . velocity . x > 0.01 | | _scroll_rect . velocity . x < - 0.01 )
2016-12-22 00:52:19 +08:00
{
2017-01-04 21:18:28 +08:00
//if the pointer is released and is moving slower than the threshold, then just land on a page
if ( IsRectMovingSlowerThanThreshold ( 0 ) )
{
ScrollToClosestElement ( ) ;
}
2016-12-22 00:52:19 +08:00
}
2015-02-03 03:25:41 +08:00
}
}
2017-01-04 21:18:28 +08:00
private bool IsRectMovingSlowerThanThreshold ( float startingSpeed )
2016-12-31 23:14:39 +08:00
{
return ( _scroll_rect . velocity . x > startingSpeed & & _scroll_rect . velocity . x < SwipeVelocityThreshold ) | |
( _scroll_rect . velocity . x < startingSpeed & & _scroll_rect . velocity . x > - SwipeVelocityThreshold ) ;
}
2017-08-12 17:24:25 +08:00
public void DistributePages ( )
2015-02-03 03:25:41 +08:00
{
2016-12-10 21:36:28 +08:00
_screens = _screensContainer . childCount ;
2016-12-30 06:03:04 +08:00
_scroll_rect . horizontalNormalizedPosition = 0 ;
2016-12-10 21:36:28 +08:00
2017-08-12 17:24:25 +08:00
float _offset = 0 ;
2016-11-15 22:52:34 +08:00
float _dimension = 0 ;
2017-08-12 17:24:25 +08:00
Rect panelDimensions = gameObject . GetComponent < RectTransform > ( ) . rect ;
2016-11-15 22:52:34 +08:00
float currentXPosition = 0 ;
2016-12-10 21:36:28 +08:00
var pageStepValue = _childSize = ( 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 > ( ) ;
2017-08-12 17:24:25 +08:00
currentXPosition = _offset + 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-12-31 23:14:39 +08:00
child . anchorMin = child . anchorMax = child . pivot = _childAnchorPoint ;
2015-02-03 07:07:31 +08:00
}
_dimension = currentXPosition + _offset * - 1 ;
2017-08-12 17:24:25 +08:00
_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-12-10 21:36:28 +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 )
2017-01-04 20:16:11 +08:00
{
AddChild ( GO , false ) ;
}
/// <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>
/// <param name="WorldPositionStays">Should the world position be updated to it's parent transform?</param>
public void AddChild ( GameObject GO , bool WorldPositionStays )
2016-05-24 03:57:56 +08:00
{
2023-02-07 22:35:43 +08:00
try
{
// Rare instances of Unity bug cause error, adding try to manage it.
_scroll_rect . horizontalNormalizedPosition = 0 ;
}
catch { }
2017-01-04 20:16:11 +08:00
GO . transform . SetParent ( _screensContainer , WorldPositionStays ) ;
2017-08-07 18:53:57 +08:00
InitialiseChildObjectsFromScene ( ) ;
2016-05-24 03:57:56 +08:00
DistributePages ( ) ;
2019-02-21 00:51:22 +08:00
if ( MaskArea )
2023-02-07 22:35:43 +08:00
{
2019-02-21 00:51:22 +08:00
UpdateVisible ( ) ;
2023-02-07 22:35:43 +08:00
}
2016-05-24 03:57:56 +08:00
2016-12-22 01:51:38 +08:00
SetScrollContainerPosition ( ) ;
2016-05-24 03:57:56 +08:00
}
/// <summary>
/// Remove a new child to this Scroll Snap and recalculate it's children
/// *Note, this is an index address (0-x)
/// </summary>
2017-07-02 21:33:57 +08:00
/// <param name="index">Index element of child to remove</param>
/// <param name="ChildRemoved">Resulting removed GO</param>
2016-05-24 03:57:56 +08:00
public void RemoveChild ( int index , out GameObject ChildRemoved )
2017-07-02 21:33:57 +08:00
{
RemoveChild ( index , false , out ChildRemoved ) ;
}
/// <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">Index element of child to remove</param>
/// <param name="WorldPositionStays">If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before</param>
/// <param name="ChildRemoved">Resulting removed GO</param>
public void RemoveChild ( int index , bool WorldPositionStays , out GameObject ChildRemoved )
2016-05-24 03:57:56 +08:00
{
ChildRemoved = null ;
if ( index < 0 | | index > _screensContainer . childCount )
{
return ;
}
2023-02-07 22:35:43 +08:00
try
{
// Rare instances of Unity bug cause error, adding try to manage it.
_scroll_rect . horizontalNormalizedPosition = 0 ;
}
catch { }
2016-12-22 01:51:38 +08:00
Transform child = _screensContainer . transform . GetChild ( index ) ;
2017-07-02 21:33:57 +08:00
child . SetParent ( null , WorldPositionStays ) ;
2016-12-22 01:51:38 +08:00
ChildRemoved = child . gameObject ;
2017-08-07 18:56:41 +08:00
InitialiseChildObjectsFromScene ( ) ;
2016-05-24 03:57:56 +08:00
DistributePages ( ) ;
2019-02-21 00:51:22 +08:00
if ( MaskArea )
UpdateVisible ( ) ;
2016-12-05 18:15:48 +08:00
2016-12-22 00:52:19 +08:00
if ( _currentPage > _screens - 1 )
2016-05-24 03:57:56 +08:00
{
2016-12-19 06:51:16 +08:00
CurrentPage = _screens - 1 ;
2016-05-24 03:57:56 +08:00
}
2016-12-22 01:51:38 +08:00
SetScrollContainerPosition ( ) ;
}
2017-01-04 20:16:11 +08:00
/// <summary>
/// Remove all children from this ScrollSnap
/// </summary>
2017-07-02 21:33:57 +08:00
/// <param name="ChildrenRemoved">Array of child GO's removed</param>
2017-01-04 20:16:11 +08:00
public void RemoveAllChildren ( out GameObject [ ] ChildrenRemoved )
2017-07-02 21:33:57 +08:00
{
RemoveAllChildren ( false , out ChildrenRemoved ) ;
}
/// <summary>
/// Remove all children from this ScrollSnap
/// </summary>
/// <param name="WorldPositionStays">If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before</param>
/// <param name="ChildrenRemoved">Array of child GO's removed</param>
public void RemoveAllChildren ( bool WorldPositionStays , out GameObject [ ] ChildrenRemoved )
2017-01-04 20:16:11 +08:00
{
var _screenCount = _screensContainer . childCount ;
ChildrenRemoved = new GameObject [ _screenCount ] ;
for ( int i = _screenCount - 1 ; i > = 0 ; i - - )
{
ChildrenRemoved [ i ] = _screensContainer . GetChild ( i ) . gameObject ;
2017-07-02 21:33:57 +08:00
ChildrenRemoved [ i ] . transform . SetParent ( null , WorldPositionStays ) ;
2017-01-04 20:16:11 +08:00
}
_scroll_rect . horizontalNormalizedPosition = 0 ;
CurrentPage = 0 ;
InitialiseChildObjectsFromScene ( ) ;
DistributePages ( ) ;
2019-02-21 00:51:22 +08:00
if ( MaskArea )
UpdateVisible ( ) ;
2017-01-04 20:16:11 +08:00
}
2016-12-22 01:51:38 +08:00
private void SetScrollContainerPosition ( )
{
2020-06-22 22:48:16 +08:00
_scrollStartPosition = _screensContainer . anchoredPosition . x ;
2016-12-22 00:52:19 +08:00
_scroll_rect . horizontalNormalizedPosition = ( float ) ( _currentPage ) / ( _screens - 1 ) ;
2017-08-02 17:21:45 +08:00
OnCurrentScreenChange ( _currentPage ) ;
2016-05-24 03:57:56 +08:00
}
2016-12-30 06:03:04 +08:00
/// <summary>
/// used for changing / updating between screen resolutions
/// </summary>
public void UpdateLayout ( )
{
_lerp = false ;
DistributePages ( ) ;
2019-02-21 00:51:22 +08:00
if ( MaskArea )
UpdateVisible ( ) ;
2016-12-30 06:03:04 +08:00
SetScrollContainerPosition ( ) ;
2017-02-07 18:55:38 +08:00
OnCurrentScreenChange ( _currentPage ) ;
2016-12-30 06:03:04 +08:00
}
2016-12-31 23:14:39 +08:00
private void OnRectTransformDimensionsChange ( )
{
if ( _childAnchorPoint ! = Vector2 . zero )
{
UpdateLayout ( ) ;
}
}
2017-03-12 23:18:36 +08:00
private void OnEnable ( )
{
2017-03-12 23:29:59 +08:00
InitialiseChildObjectsFromScene ( ) ;
2017-03-12 23:18:36 +08:00
DistributePages ( ) ;
2019-02-21 00:51:22 +08:00
if ( MaskArea )
UpdateVisible ( ) ;
2017-03-12 23:18:36 +08:00
2019-02-21 00:51:22 +08:00
if ( JumpOnEnable | | ! RestartOnEnable )
SetScrollContainerPosition ( ) ;
if ( RestartOnEnable )
2020-09-29 06:43:03 +08:00
GoToScreen ( StartingScreen ) ;
2017-03-12 23:18:36 +08:00
}
2016-12-05 04:06:03 +08:00
/// <summary>
/// Release screen to swipe
/// </summary>
/// <param name="eventData"></param>
2020-05-22 19:15:58 +08:00
public override void OnEndDrag ( PointerEventData eventData )
2015-02-03 03:25:41 +08:00
{
2020-09-28 21:00:09 +08:00
if ( updated )
{
return ;
}
// to prevent double dragging, only act on EndDrag once per frame
updated = true ;
2017-01-04 21:18:28 +08:00
_pointerDown = false ;
2015-02-03 07:07:31 +08:00
if ( _scroll_rect . horizontal )
2015-02-03 03:25:41 +08:00
{
2019-07-11 00:36:36 +08:00
if ( UseSwipeDeltaThreshold & & Math . Abs ( eventData . delta . x ) < SwipeDeltaThreshold )
2019-02-21 00:51:22 +08:00
{
2019-07-11 00:36:36 +08:00
ScrollToClosestElement ( ) ;
2018-11-13 09:32:14 +08:00
}
else
{
2020-06-22 22:48:16 +08:00
var distance = Vector3 . Distance ( _startPosition , _screensContainer . anchoredPosition ) ;
2019-07-11 00:36:36 +08:00
if ( UseHardSwipe )
2018-11-13 09:32:14 +08:00
{
_scroll_rect . velocity = Vector3 . zero ;
2019-07-11 00:36:36 +08:00
if ( distance > FastSwipeThreshold )
2018-03-24 23:36:38 +08:00
{
2020-06-22 22:48:16 +08:00
if ( _startPosition . x - _screensContainer . anchoredPosition . x > 0 )
2018-11-13 09:32:14 +08:00
{
2019-07-11 00:36:36 +08:00
NextScreen ( ) ;
2018-11-13 09:32:14 +08:00
}
else
{
2019-07-11 00:36:36 +08:00
PreviousScreen ( ) ;
2018-11-13 09:32:14 +08:00
}
2018-03-24 23:36:38 +08:00
}
else
{
2019-07-11 00:36:36 +08:00
ScrollToClosestElement ( ) ;
}
}
else
{
if ( UseFastSwipe & & distance < panelDimensions . width & & distance > = FastSwipeThreshold )
{
_scroll_rect . velocity = Vector3 . zero ;
2020-06-22 22:48:16 +08:00
if ( _startPosition . x - _screensContainer . anchoredPosition . x > 0 )
2018-11-13 09:32:14 +08:00
{
2020-06-22 22:48:16 +08:00
if ( _startPosition . x - _screensContainer . anchoredPosition . x > _childSize / 3 )
2019-07-11 00:36:36 +08:00
{
ScrollToClosestElement ( ) ;
}
else
{
NextScreen ( ) ;
}
2018-11-13 09:32:14 +08:00
}
else
{
2020-06-22 22:48:16 +08:00
if ( _startPosition . x - _screensContainer . anchoredPosition . x < - _childSize / 3 )
2019-07-11 00:36:36 +08:00
{
ScrollToClosestElement ( ) ;
}
else
{
PreviousScreen ( ) ;
}
2018-11-13 09:32:14 +08:00
}
2018-03-24 23:36:38 +08:00
}
2023-06-18 00:16:31 +08:00
else if ( distance = = 0 )
{
EndScreenChange ( ) ;
}
2015-02-03 03:25:41 +08:00
}
}
}
}
}
2019-01-15 18:25:12 +08:00
}