ScrollConflictManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /// Credit srinivas sunil
  2. /// sourced from: https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/pull-requests/21/develop_53/diff
  3. /// Updated by Hiep Eldest : https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/300/scrollconflictmanager-not-working-if
  4. using UnityEngine.EventSystems;
  5. /// <summary>
  6. /// 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
  7. /// Attach the script to the rect scroll and assign other rectscroll in the inspector (one is vertical and other is horizontal) gathered and modified from unity answers(delta snipper)
  8. /// </summary>
  9. namespace UnityEngine.UI.Extensions
  10. {
  11. [RequireComponent(typeof(ScrollRect))]
  12. [AddComponentMenu("UI/Extensions/Scrollrect Conflict Manager")]
  13. public class ScrollConflictManager : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
  14. {
  15. [Tooltip("The parent ScrollRect control hosting this ScrollSnap")]
  16. public ScrollRect ParentScrollRect;
  17. [Tooltip("The parent ScrollSnap control hosting this Scroll Snap.\nIf left empty, it will use the ScrollSnap of the ParentScrollRect")]
  18. private ScrollSnapBase ParentScrollSnap;
  19. private ScrollRect _myScrollRect;
  20. private IBeginDragHandler[] _beginDragHandlers;
  21. private IEndDragHandler[] _endDragHandlers;
  22. private IDragHandler[] _dragHandlers;
  23. //This tracks if the other one should be scrolling instead of the current one.
  24. private bool scrollOther;
  25. //This tracks whether the other one should scroll horizontally or vertically.
  26. private bool scrollOtherHorizontally;
  27. void Awake()
  28. {
  29. if (ParentScrollRect)
  30. {
  31. InitialiseConflictManager();
  32. }
  33. }
  34. private void InitialiseConflictManager()
  35. {
  36. //Get the current scroll rect so we can disable it if the other one is scrolling
  37. _myScrollRect = this.GetComponent<ScrollRect>();
  38. //If the current scroll Rect has the vertical checked then the other one will be scrolling horizontally.
  39. scrollOtherHorizontally = _myScrollRect.vertical;
  40. //Check some attributes to let the user know if this wont work as expected
  41. if (scrollOtherHorizontally)
  42. {
  43. if (_myScrollRect.horizontal)
  44. Debug.LogError("You have added the SecondScrollRect to a scroll view that already has both directions selected");
  45. if (!ParentScrollRect.horizontal)
  46. Debug.LogError("The other scroll rect does not support scrolling horizontally");
  47. }
  48. else if (!ParentScrollRect.vertical)
  49. {
  50. Debug.LogError("The other scroll rect does not support scrolling vertically");
  51. }
  52. if (ParentScrollRect && !ParentScrollSnap)
  53. {
  54. ParentScrollSnap = ParentScrollRect.GetComponent<ScrollSnapBase>();
  55. }
  56. }
  57. void Start()
  58. {
  59. if (ParentScrollRect)
  60. {
  61. AssignScrollRectHandlers();
  62. }
  63. }
  64. private void AssignScrollRectHandlers()
  65. {
  66. _beginDragHandlers = ParentScrollRect.GetComponents<IBeginDragHandler>();
  67. _dragHandlers = ParentScrollRect.GetComponents<IDragHandler>();
  68. _endDragHandlers = ParentScrollRect.GetComponents<IEndDragHandler>();
  69. }
  70. public void SetParentScrollRect(ScrollRect parentScrollRect)
  71. {
  72. ParentScrollRect = parentScrollRect;
  73. InitialiseConflictManager();
  74. AssignScrollRectHandlers();
  75. }
  76. #region DragHandler
  77. public void OnBeginDrag(PointerEventData eventData)
  78. {
  79. //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
  80. float horizontal = Mathf.Abs(eventData.position.x - eventData.pressPosition.x);
  81. float vertical = Mathf.Abs(eventData.position.y - eventData.pressPosition.y);
  82. if (scrollOtherHorizontally)
  83. {
  84. if (horizontal > vertical)
  85. {
  86. scrollOther = true;
  87. //disable the current scroll rect so it does not move.
  88. _myScrollRect.enabled = false;
  89. for (int i = 0, length = _beginDragHandlers.Length; i < length; i++)
  90. {
  91. _beginDragHandlers[i].OnBeginDrag(eventData);
  92. if(ParentScrollSnap) ParentScrollSnap.OnBeginDrag(eventData);
  93. }
  94. }
  95. }
  96. else if (vertical > horizontal)
  97. {
  98. scrollOther = true;
  99. //disable the current scroll rect so it does not move.
  100. _myScrollRect.enabled = false;
  101. for (int i = 0, length = _beginDragHandlers.Length; i < length; i++)
  102. {
  103. _beginDragHandlers[i].OnBeginDrag(eventData);
  104. if (ParentScrollSnap) ParentScrollSnap.OnBeginDrag(eventData);
  105. }
  106. }
  107. }
  108. public void OnEndDrag(PointerEventData eventData)
  109. {
  110. if (scrollOther)
  111. {
  112. _myScrollRect.enabled = true;
  113. scrollOther = false;
  114. for (int i = 0, length = _endDragHandlers.Length; i < length; i++)
  115. {
  116. _endDragHandlers[i].OnEndDrag(eventData);
  117. if (ParentScrollSnap) ParentScrollSnap.OnEndDrag(eventData);
  118. }
  119. }
  120. }
  121. public void OnDrag(PointerEventData eventData)
  122. {
  123. if (scrollOther)
  124. {
  125. for (int i = 0, length = _endDragHandlers.Length; i < length; i++)
  126. {
  127. _dragHandlers[i].OnDrag(eventData);
  128. if (ParentScrollSnap) ParentScrollSnap.OnDrag(eventData);
  129. }
  130. }
  131. }
  132. #endregion DragHandler
  133. }
  134. }