Updated ScrollConflict Manager to handle multiple patent/dependant scrollrects

https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/300/scrollconflictmanager-not-working-if
release
Simon Jackson 2019-09-25 14:02:40 +01:00
parent 15f5ff7886
commit af4cb4428d
1 changed files with 28 additions and 10 deletions

View File

@ -1,5 +1,6 @@
/// 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
/// Updated by Hiep Eldest : https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/300/scrollconflictmanager-not-working-if
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
@ -14,8 +15,10 @@ namespace UnityEngine.UI.Extensions
public class ScrollConflictManager : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler public class ScrollConflictManager : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{ {
public ScrollRect ParentScrollRect; public ScrollRect ParentScrollRect;
public ScrollSnap ParentScrollSnap;
private ScrollRect _myScrollRect; private ScrollRect _myScrollRect;
private IBeginDragHandler[] _beginDragHandlers;
private IEndDragHandler[] _endDragHandlers;
private IDragHandler[] _dragHandlers;
//This tracks if the other one should be scrolling instead of the current one. //This tracks if the other one should be scrolling instead of the current one.
private bool scrollOther; private bool scrollOther;
//This tracks wether the other one should scroll horizontally or vertically. //This tracks wether the other one should scroll horizontally or vertically.
@ -41,6 +44,13 @@ namespace UnityEngine.UI.Extensions
} }
} }
void Start()
{
_beginDragHandlers = ParentScrollRect.GetComponents<IBeginDragHandler>();
_dragHandlers = ParentScrollRect.GetComponents<IDragHandler>();
_endDragHandlers = ParentScrollRect.GetComponents<IEndDragHandler>();
}
//IBeginDragHandler //IBeginDragHandler
public void OnBeginDrag(PointerEventData eventData) public void OnBeginDrag(PointerEventData eventData)
{ {
@ -54,8 +64,10 @@ namespace UnityEngine.UI.Extensions
scrollOther = true; scrollOther = true;
//disable the current scroll rect so it doesnt move. //disable the current scroll rect so it doesnt move.
_myScrollRect.enabled = false; _myScrollRect.enabled = false;
ParentScrollRect.OnBeginDrag(eventData); for (int i = 0, length = _beginDragHandlers.Length; i < length; i++)
ParentScrollSnap.OnBeginDrag(eventData); {
_beginDragHandlers[i].OnBeginDrag(eventData);
}
} }
} }
else if (vertical > horizontal) else if (vertical > horizontal)
@ -63,8 +75,10 @@ namespace UnityEngine.UI.Extensions
scrollOther = true; scrollOther = true;
//disable the current scroll rect so it doesnt move. //disable the current scroll rect so it doesnt move.
_myScrollRect.enabled = false; _myScrollRect.enabled = false;
ParentScrollRect.OnBeginDrag(eventData); for (int i = 0, length = _beginDragHandlers.Length; i < length; i++)
ParentScrollSnap.OnBeginDrag(eventData); {
_beginDragHandlers[i].OnBeginDrag(eventData);
}
} }
} }
@ -73,10 +87,12 @@ namespace UnityEngine.UI.Extensions
{ {
if (scrollOther) if (scrollOther)
{ {
scrollOther = false;
_myScrollRect.enabled = true; _myScrollRect.enabled = true;
ParentScrollRect.OnEndDrag(eventData); scrollOther = false;
ParentScrollSnap.OnEndDrag(eventData); for (int i = 0, length = _endDragHandlers.Length; i < length; i++)
{
_endDragHandlers[i].OnEndDrag(eventData);
}
} }
} }
@ -85,8 +101,10 @@ namespace UnityEngine.UI.Extensions
{ {
if (scrollOther) if (scrollOther)
{ {
ParentScrollRect.OnDrag(eventData); for (int i = 0, length = _endDragHandlers.Length; i < length; i++)
ParentScrollSnap.OnDrag(eventData); {
_dragHandlers[i].OnDrag(eventData);
}
} }
} }
} }