2019-03-08 01:04:10 +08:00
/// Credit srinivas sunil
2020-07-09 03:38:28 +08:00
/// sourced from: https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/pull-requests/21/develop_53/diff
2019-09-25 21:02:40 +08:00
/// Updated by Hiep Eldest : https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/300/scrollconflictmanager-not-working-if
2019-03-08 01:04:10 +08:00
using UnityEngine.EventSystems ;
/// <summary>
/// This is the most efficient way to handle scroll conflicts when there are multiple scroll rects, this is useful when there is a vertical scrollrect in/on a horizontal scrollrect or vice versa
2020-07-09 03:38:28 +08:00
/// Attach the script to the rect scroll and assign other rectscroll in the inspector (one is vertical and other is horizontal) gathered and modified from unity answers(delta snipper)
2019-03-08 01:04:10 +08:00
/// </summary>
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(ScrollRect))]
[AddComponentMenu("UI/Extensions/Scrollrect Conflict Manager")]
public class ScrollConflictManager : MonoBehaviour , IBeginDragHandler , IEndDragHandler , IDragHandler
{
2020-05-22 20:29:03 +08:00
[Tooltip("The parent ScrollRect control hosting this ScrollSnap")]
2019-03-08 01:04:10 +08:00
public ScrollRect ParentScrollRect ;
2020-05-22 20:29:03 +08:00
[Tooltip("The parent ScrollSnap control hosting this Scroll Snap.\nIf left empty, it will use the ScrollSnap of the ParentScrollRect")]
2020-10-22 08:38:49 +08:00
private ScrollSnapBase ParentScrollSnap ;
2020-05-22 20:29:03 +08:00
2019-03-08 01:04:10 +08:00
private ScrollRect _myScrollRect ;
2019-09-25 21:02:40 +08:00
private IBeginDragHandler [ ] _beginDragHandlers ;
private IEndDragHandler [ ] _endDragHandlers ;
private IDragHandler [ ] _dragHandlers ;
2019-03-08 01:04:10 +08:00
//This tracks if the other one should be scrolling instead of the current one.
private bool scrollOther ;
2020-07-09 03:38:28 +08:00
//This tracks whether the other one should scroll horizontally or vertically.
2019-03-08 01:04:10 +08:00
private bool scrollOtherHorizontally ;
void Awake ( )
2020-10-22 08:38:49 +08:00
{
if ( ParentScrollRect )
{
InitialiseConflictManager ( ) ;
}
}
private void InitialiseConflictManager ( )
2019-03-08 01:04:10 +08:00
{
//Get the current scroll rect so we can disable it if the other one is scrolling
_myScrollRect = this . GetComponent < ScrollRect > ( ) ;
//If the current scroll Rect has the vertical checked then the other one will be scrolling horizontally.
scrollOtherHorizontally = _myScrollRect . vertical ;
//Check some attributes to let the user know if this wont work as expected
if ( scrollOtherHorizontally )
{
if ( _myScrollRect . horizontal )
2020-05-22 20:29:03 +08:00
Debug . LogError ( "You have added the SecondScrollRect to a scroll view that already has both directions selected" ) ;
2019-03-08 01:04:10 +08:00
if ( ! ParentScrollRect . horizontal )
2020-07-09 03:38:28 +08:00
Debug . LogError ( "The other scroll rect does not support scrolling horizontally" ) ;
2019-03-08 01:04:10 +08:00
}
else if ( ! ParentScrollRect . vertical )
{
2020-07-09 03:38:28 +08:00
Debug . LogError ( "The other scroll rect does not support scrolling vertically" ) ;
2020-05-22 20:29:03 +08:00
}
if ( ParentScrollRect & & ! ParentScrollSnap )
{
ParentScrollSnap = ParentScrollRect . GetComponent < ScrollSnapBase > ( ) ;
2019-03-08 01:04:10 +08:00
}
}
2019-09-25 21:02:40 +08:00
void Start ( )
2020-10-22 08:38:49 +08:00
{
if ( ParentScrollRect )
{
AssignScrollRectHandlers ( ) ;
}
}
private void AssignScrollRectHandlers ( )
2019-09-25 21:02:40 +08:00
{
_beginDragHandlers = ParentScrollRect . GetComponents < IBeginDragHandler > ( ) ;
_dragHandlers = ParentScrollRect . GetComponents < IDragHandler > ( ) ;
_endDragHandlers = ParentScrollRect . GetComponents < IEndDragHandler > ( ) ;
}
2020-10-22 08:38:49 +08:00
public void SetParentScrollRect ( ScrollRect parentScrollRect )
{
ParentScrollRect = parentScrollRect ;
InitialiseConflictManager ( ) ;
AssignScrollRectHandlers ( ) ;
}
2020-05-22 20:29:03 +08:00
#region DragHandler
2019-03-08 01:04:10 +08:00
public void OnBeginDrag ( PointerEventData eventData )
{
//Get the absolute values of the x and y differences so we can see which one is bigger and scroll the other scroll rect accordingly
float horizontal = Mathf . Abs ( eventData . position . x - eventData . pressPosition . x ) ;
float vertical = Mathf . Abs ( eventData . position . y - eventData . pressPosition . y ) ;
if ( scrollOtherHorizontally )
{
if ( horizontal > vertical )
{
scrollOther = true ;
2020-07-09 03:38:28 +08:00
//disable the current scroll rect so it does not move.
2019-03-08 01:04:10 +08:00
_myScrollRect . enabled = false ;
2019-09-25 21:02:40 +08:00
for ( int i = 0 , length = _beginDragHandlers . Length ; i < length ; i + + )
{
_beginDragHandlers [ i ] . OnBeginDrag ( eventData ) ;
2020-05-22 20:29:03 +08:00
if ( ParentScrollSnap ) ParentScrollSnap . OnBeginDrag ( eventData ) ;
2019-09-25 21:02:40 +08:00
}
2019-03-08 01:04:10 +08:00
}
}
else if ( vertical > horizontal )
{
scrollOther = true ;
2020-07-09 03:38:28 +08:00
//disable the current scroll rect so it does not move.
2019-03-08 01:04:10 +08:00
_myScrollRect . enabled = false ;
2019-09-25 21:02:40 +08:00
for ( int i = 0 , length = _beginDragHandlers . Length ; i < length ; i + + )
{
_beginDragHandlers [ i ] . OnBeginDrag ( eventData ) ;
2020-05-22 20:29:03 +08:00
if ( ParentScrollSnap ) ParentScrollSnap . OnBeginDrag ( eventData ) ;
2019-09-25 21:02:40 +08:00
}
2019-03-08 01:04:10 +08:00
}
}
public void OnEndDrag ( PointerEventData eventData )
{
if ( scrollOther )
{
_myScrollRect . enabled = true ;
2019-09-25 21:02:40 +08:00
scrollOther = false ;
for ( int i = 0 , length = _endDragHandlers . Length ; i < length ; i + + )
{
_endDragHandlers [ i ] . OnEndDrag ( eventData ) ;
2020-05-22 20:29:03 +08:00
if ( ParentScrollSnap ) ParentScrollSnap . OnEndDrag ( eventData ) ;
2019-09-25 21:02:40 +08:00
}
2019-03-08 01:04:10 +08:00
}
}
public void OnDrag ( PointerEventData eventData )
{
if ( scrollOther )
{
2019-09-25 21:02:40 +08:00
for ( int i = 0 , length = _endDragHandlers . Length ; i < length ; i + + )
{
_dragHandlers [ i ] . OnDrag ( eventData ) ;
2020-05-22 20:29:03 +08:00
if ( ParentScrollSnap ) ParentScrollSnap . OnDrag ( eventData ) ;
2019-09-25 21:02:40 +08:00
}
2019-03-08 01:04:10 +08:00
}
}
2020-05-22 20:29:03 +08:00
#endregion DragHandler
2019-03-08 01:04:10 +08:00
}
2016-02-28 00:28:02 +08:00
}