From 654cd416cd27e0dd8649583d97159b6d48edd8d9 Mon Sep 17 00:00:00 2001 From: srinivas sunil Date: Sat, 27 Feb 2016 16:28:02 +0000 Subject: [PATCH 1/3] ScrollConflictManager created online with Bitbucket --HG-- branch : srinivassunil/scrollconflictmanager-created-online-wit-1456590432246 --- Scripts/ReorderableList/ScrollConflictManager | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Scripts/ReorderableList/ScrollConflictManager diff --git a/Scripts/ReorderableList/ScrollConflictManager b/Scripts/ReorderableList/ScrollConflictManager new file mode 100644 index 0000000..e1846ae --- /dev/null +++ b/Scripts/ReorderableList/ScrollConflictManager @@ -0,0 +1,84 @@ +using UnityEngine; +using System.Collections; +using UnityEngine.EventSystems; +using UnityEngine.UI; +using System; + + +//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 a horizontal scrollrect or vice versa +//gathered and modified from unity answers(delta snipper) + + +[RequireComponent(typeof(ScrollRect))] +public class ScrollConflictManager : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler +{ + public ScrollRect OtherScrollRect; + private ScrollRect _myScrollRect; + //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(); + //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 (!OtherScrollRect.horizontal) + Debug.Log("The other scroll rect doesnt support scrolling horizontally"); + } + else if (!OtherScrollRect.vertical) + { + Debug.Log("The other scroll rect doesnt support scrolling vertically"); + } + } + //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; + OtherScrollRect.OnBeginDrag(eventData); + } + } + else if (vertical > horizontal) + { + scrollOther = true; + //disable the current scroll rect so it doesnt move. + _myScrollRect.enabled = false; + OtherScrollRect.OnBeginDrag(eventData); + } + } + //IEndDragHandler + public void OnEndDrag(PointerEventData eventData) + { + if (scrollOther) + { + scrollOther = false; + _myScrollRect.enabled = true; + OtherScrollRect.OnEndDrag(eventData); + } + } + //IDragHandler + public void OnDrag(PointerEventData eventData) + { + if (scrollOther) + { + OtherScrollRect.OnDrag(eventData); + } + } +} \ No newline at end of file From dcf73019e95d6f3216dcebad0c9de2bf016e4898 Mon Sep 17 00:00:00 2001 From: srinivas sunil Date: Sat, 27 Feb 2016 16:31:23 +0000 Subject: [PATCH 2/3] ScrollConflictManager edited online with Bitbucket --HG-- branch : srinivassunil/scrollconflictmanager-created-online-wit-1456590432246 --- Scripts/ReorderableList/ScrollConflictManager | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Scripts/ReorderableList/ScrollConflictManager b/Scripts/ReorderableList/ScrollConflictManager index e1846ae..2430aea 100644 --- a/Scripts/ReorderableList/ScrollConflictManager +++ b/Scripts/ReorderableList/ScrollConflictManager @@ -6,9 +6,12 @@ using System; //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 a horizontal scrollrect or vice versa -//gathered and modified from unity answers(delta snipper) +//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) +namespace UnityEngine.UI.Extensions +{ [RequireComponent(typeof(ScrollRect))] public class ScrollConflictManager : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler @@ -81,4 +84,5 @@ public class ScrollConflictManager : MonoBehaviour, IBeginDragHandler, IEndDragH OtherScrollRect.OnDrag(eventData); } } +} } \ No newline at end of file