ScrollRectPassThrough.cs 677 B

1234567891011121314151617181920212223242526
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.UI;
  4. /// <summary>
  5. /// Attach this to any interactive UI element (Button, Toggle, etc.) inside a ScrollRect
  6. /// so that mouse wheel scroll events are forwarded to the parent ScrollRect instead of
  7. /// being swallowed by the child element.
  8. /// </summary>
  9. public class ScrollRectPassThrough : MonoBehaviour, IScrollHandler
  10. {
  11. private ScrollRect _parentScrollRect;
  12. private void Start()
  13. {
  14. _parentScrollRect = GetComponentInParent<ScrollRect>();
  15. }
  16. public void OnScroll(PointerEventData eventData)
  17. {
  18. if (_parentScrollRect != null)
  19. {
  20. _parentScrollRect.OnScroll(eventData);
  21. }
  22. }
  23. }