Search code examples
unity-game-enginescrollview

UNITY In scroll view rect , how do I drag when I click on the childs?


Consider the following set up, I have a 2D like array using scrollviewrect vertical and vertial layout group. And then in side with child of horizontal layout group.

EDIT: It happens when I run the game, not in the editor.

EDIT2: The map icons are Buttons, I have tried removing the Button script but it is still the same

enter image description here

My issue is I can drag it up and down only if I click between the icons, but when I click on the icons it wont drag.

Is it because my icon have click event implemented that blocks the drag event? How do I fix it?

enter image description hereenter image description here


Solution

  • The solution is close to what TheQuaX has, but I find a better way which is to pass the event to the scroll.

    Here is the code I found on unity forum.

    https://forum.unity.com/threads/child-objects-blocking-scrollrect-from-scrolling.311555/

    public class FixScrollRect: MonoBehaviour, IBeginDragHandler,  IDragHandler, IEndDragHandler, IScrollHandler
    {
        public ScrollRect MainScroll;
     
     
        public void OnBeginDrag(PointerEventData eventData)
        {
            MainScroll.OnBeginDrag(eventData);
        }
     
     
        public void OnDrag(PointerEventData eventData)
        {
            MainScroll.OnDrag(eventData);
        }
     
        public void OnEndDrag(PointerEventData eventData)
        {
            MainScroll.OnEndDrag(eventData);
        }
     
     
        public void OnScroll(PointerEventData data)
        {
            MainScroll.OnScroll(data);
        }
     
     
    }