Tanoda
ScrollConflictManager.cs
Go to the documentation of this file.
1
4
6
12{
13 [RequireComponent(typeof(ScrollRect))]
14 [AddComponentMenu("UI/Extensions/Scrollrect Conflict Manager")]
15 public class ScrollConflictManager : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
16 {
17 [Tooltip("The parent ScrollRect control hosting this ScrollSnap")]
18 public ScrollRect ParentScrollRect;
19
20 [Tooltip("The parent ScrollSnap control hosting this Scroll Snap.\nIf left empty, it will use the ScrollSnap of the ParentScrollRect")]
22
23 private ScrollRect _myScrollRect;
24 private IBeginDragHandler[] _beginDragHandlers;
25 private IEndDragHandler[] _endDragHandlers;
26 private IDragHandler[] _dragHandlers;
27 //This tracks if the other one should be scrolling instead of the current one.
28 private bool scrollOther;
29 //This tracks whether the other one should scroll horizontally or vertically.
30 private bool scrollOtherHorizontally;
31
32 void Awake()
33 {
34 //Get the current scroll rect so we can disable it if the other one is scrolling
35 _myScrollRect = this.GetComponent<ScrollRect>();
36 //If the current scroll Rect has the vertical checked then the other one will be scrolling horizontally.
37 scrollOtherHorizontally = _myScrollRect.vertical;
38 //Check some attributes to let the user know if this wont work as expected
39 if (scrollOtherHorizontally)
40 {
41 if (_myScrollRect.horizontal)
42 Debug.LogError("You have added the SecondScrollRect to a scroll view that already has both directions selected");
43 if (!ParentScrollRect.horizontal)
44 Debug.LogError("The other scroll rect does not support scrolling horizontally");
45 }
46 else if (!ParentScrollRect.vertical)
47 {
48 Debug.LogError("The other scroll rect does not support scrolling vertically");
49 }
50
52 {
54 }
55 }
56
57 void Start()
58 {
59 _beginDragHandlers = ParentScrollRect.GetComponents<IBeginDragHandler>();
60 _dragHandlers = ParentScrollRect.GetComponents<IDragHandler>();
61 _endDragHandlers = ParentScrollRect.GetComponents<IEndDragHandler>();
62 }
63
64 #region DragHandler
65
66 public void OnBeginDrag(PointerEventData eventData)
67 {
68 //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
69 float horizontal = Mathf.Abs(eventData.position.x - eventData.pressPosition.x);
70 float vertical = Mathf.Abs(eventData.position.y - eventData.pressPosition.y);
71 if (scrollOtherHorizontally)
72 {
73 if (horizontal > vertical)
74 {
75 scrollOther = true;
76 //disable the current scroll rect so it does not move.
77 _myScrollRect.enabled = false;
78 for (int i = 0, length = _beginDragHandlers.Length; i < length; i++)
79 {
80 _beginDragHandlers[i].OnBeginDrag(eventData);
82 }
83 }
84 }
85 else if (vertical > horizontal)
86 {
87 scrollOther = true;
88 //disable the current scroll rect so it does not move.
89 _myScrollRect.enabled = false;
90 for (int i = 0, length = _beginDragHandlers.Length; i < length; i++)
91 {
92 _beginDragHandlers[i].OnBeginDrag(eventData);
94 }
95 }
96 }
97
98 public void OnEndDrag(PointerEventData eventData)
99 {
100 if (scrollOther)
101 {
102 _myScrollRect.enabled = true;
103 scrollOther = false;
104 for (int i = 0, length = _endDragHandlers.Length; i < length; i++)
105 {
106 _endDragHandlers[i].OnEndDrag(eventData);
108 }
109 }
110 }
111
112 public void OnDrag(PointerEventData eventData)
113 {
114 if (scrollOther)
115 {
116 for (int i = 0, length = _endDragHandlers.Length; i < length; i++)
117 {
118 _dragHandlers[i].OnDrag(eventData);
120 }
121 }
122 }
123
124 #endregion DragHandler
125 }
126}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
virtual void OnEndDrag(PointerEventData eventData)
void OnBeginDrag(PointerEventData eventData)
Touch screen to start swiping
void OnDrag(PointerEventData eventData)
While dragging do
Credit Erdener Gonenc - @PixelEnvision.