2019-03-08 01:04:10 +08:00
/// Credit srinivas sunil
/// sourced from: https://bitbucket.org/ddreaper/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
/// Attach the script to the rect scroll and assign other rectscroll in the inspecter (one is verticle and other is horizontal) gathered and modified from unity answers(delta snipper)
/// </summary>
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(ScrollRect))]
[AddComponentMenu("UI/Extensions/Scrollrect Conflict Manager")]
public class ScrollConflictManager : MonoBehaviour , IBeginDragHandler , IEndDragHandler , IDragHandler
{
public ScrollRect ParentScrollRect ;
2020-05-22 18:00:35 +08:00
public HorizontalScrollSnap ParentScrollSnapHorizontal ;
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 ;
//This tracks wether the other one should scroll horizontally or vertically.
private bool scrollOtherHorizontally ;
void Awake ( )
{
//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 )
Debug . Log ( "You have added the SecondScrollRect to a scroll view that already has both directions selected" ) ;
if ( ! ParentScrollRect . horizontal )
Debug . Log ( "The other scroll rect doesnt support scrolling horizontally" ) ;
}
else if ( ! ParentScrollRect . vertical )
{
Debug . Log ( "The other scroll rect doesnt support scrolling vertically" ) ;
}
}
2019-09-25 21:02:40 +08:00
void Start ( )
{
_beginDragHandlers = ParentScrollRect . GetComponents < IBeginDragHandler > ( ) ;
_dragHandlers = ParentScrollRect . GetComponents < IDragHandler > ( ) ;
_endDragHandlers = ParentScrollRect . GetComponents < IEndDragHandler > ( ) ;
}
2019-03-08 01:04:10 +08:00
//IBeginDragHandler
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 ;
//disable the current scroll rect so it doesnt move.
_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 18:00:35 +08:00
ParentScrollSnapHorizontal . OnBeginDrag ( eventData ) ;
2019-09-25 21:02:40 +08:00
}
2019-03-08 01:04:10 +08:00
}
}
else if ( vertical > horizontal )
{
scrollOther = true ;
//disable the current scroll rect so it doesnt move.
_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 18:00:35 +08:00
ParentScrollSnapHorizontal . OnBeginDrag ( eventData ) ;
2019-09-25 21:02:40 +08:00
}
2019-03-08 01:04:10 +08:00
}
}
//IEndDragHandler
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 18:00:35 +08:00
ParentScrollSnapHorizontal . OnEndDrag ( eventData ) ;
2019-09-25 21:02:40 +08:00
}
2019-03-08 01:04:10 +08:00
}
}
//IDragHandler
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 18:00:35 +08:00
ParentScrollSnapHorizontal . OnDrag ( eventData ) ;
2019-09-25 21:02:40 +08:00
}
2019-03-08 01:04:10 +08:00
}
}
}
2016-02-28 00:28:02 +08:00
}