Updated ScrollConflictManager with fix from Jared Turner #85

pull/413/head
Simon (Darkside) Jackson 2019-03-07 17:04:10 +00:00
parent e813cd70a9
commit 22b2ec2ec2
1 changed files with 92 additions and 87 deletions

View File

@ -1,88 +1,93 @@
/// Credit srinivas sunil /// Credit srinivas sunil
/// sourced from: https://bitbucket.org/ddreaper/unity-ui-extensions/pull-requests/21/develop_53/diff /// sourced from: https://bitbucket.org/ddreaper/unity-ui-extensions/pull-requests/21/develop_53/diff
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
/// <summary> /// <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 /// 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) /// 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> /// </summary>
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[RequireComponent(typeof(ScrollRect))] [RequireComponent(typeof(ScrollRect))]
[AddComponentMenu("UI/Extensions/Scrollrect Conflict Manager")] [AddComponentMenu("UI/Extensions/Scrollrect Conflict Manager")]
public class ScrollConflictManager : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler public class ScrollConflictManager : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{ {
public ScrollRect ParentScrollRect; public ScrollRect ParentScrollRect;
private ScrollRect _myScrollRect; public ScrollSnap ParentScrollSnap;
//This tracks if the other one should be scrolling instead of the current one. private ScrollRect _myScrollRect;
private bool scrollOther; //This tracks if the other one should be scrolling instead of the current one.
//This tracks wether the other one should scroll horizontally or vertically. private bool scrollOther;
private bool scrollOtherHorizontally; //This tracks wether the other one should scroll horizontally or vertically.
private bool scrollOtherHorizontally;
void Awake()
{ void Awake()
//Get the current scroll rect so we can disable it if the other one is scrolling {
_myScrollRect = this.GetComponent<ScrollRect>(); //Get the current scroll rect so we can disable it if the other one is scrolling
//If the current scroll Rect has the vertical checked then the other one will be scrolling horizontally. _myScrollRect = this.GetComponent<ScrollRect>();
scrollOtherHorizontally = _myScrollRect.vertical; //If the current scroll Rect has the vertical checked then the other one will be scrolling horizontally.
//Check some attributes to let the user know if this wont work as expected scrollOtherHorizontally = _myScrollRect.vertical;
if (scrollOtherHorizontally) //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 (_myScrollRect.horizontal)
if (!ParentScrollRect.horizontal) Debug.Log("You have added the SecondScrollRect to a scroll view that already has both directions selected");
Debug.Log("The other scroll rect doesnt support scrolling horizontally"); if (!ParentScrollRect.horizontal)
} Debug.Log("The other scroll rect doesnt support scrolling horizontally");
else if (!ParentScrollRect.vertical) }
{ else if (!ParentScrollRect.vertical)
Debug.Log("The other scroll rect doesnt support scrolling vertically"); {
} Debug.Log("The other scroll rect doesnt support scrolling vertically");
} }
}
//IBeginDragHandler
public void OnBeginDrag(PointerEventData eventData) //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); //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 vertical = Mathf.Abs(eventData.position.y - eventData.pressPosition.y); float horizontal = Mathf.Abs(eventData.position.x - eventData.pressPosition.x);
if (scrollOtherHorizontally) float vertical = Mathf.Abs(eventData.position.y - eventData.pressPosition.y);
{ if (scrollOtherHorizontally)
if (horizontal > vertical) {
{ if (horizontal > vertical)
scrollOther = true; {
//disable the current scroll rect so it doesnt move. scrollOther = true;
_myScrollRect.enabled = false; //disable the current scroll rect so it doesnt move.
ParentScrollRect.OnBeginDrag(eventData); _myScrollRect.enabled = false;
} ParentScrollRect.OnBeginDrag(eventData);
} ParentScrollSnap.OnBeginDrag(eventData);
else if (vertical > horizontal) }
{ }
scrollOther = true; else if (vertical > horizontal)
//disable the current scroll rect so it doesnt move. {
_myScrollRect.enabled = false; scrollOther = true;
ParentScrollRect.OnBeginDrag(eventData); //disable the current scroll rect so it doesnt move.
} _myScrollRect.enabled = false;
} ParentScrollRect.OnBeginDrag(eventData);
ParentScrollSnap.OnBeginDrag(eventData);
//IEndDragHandler }
public void OnEndDrag(PointerEventData eventData) }
{
if (scrollOther) //IEndDragHandler
{ public void OnEndDrag(PointerEventData eventData)
scrollOther = false; {
_myScrollRect.enabled = true; if (scrollOther)
ParentScrollRect.OnEndDrag(eventData); {
} scrollOther = false;
} _myScrollRect.enabled = true;
ParentScrollRect.OnEndDrag(eventData);
//IDragHandler ParentScrollSnap.OnEndDrag(eventData);
public void OnDrag(PointerEventData eventData) }
{ }
if (scrollOther)
{ //IDragHandler
ParentScrollRect.OnDrag(eventData); public void OnDrag(PointerEventData eventData)
} {
} if (scrollOther)
} {
ParentScrollRect.OnDrag(eventData);
ParentScrollSnap.OnDrag(eventData);
}
}
}
} }