Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    Dismiss Notice

ScrollRect with Buttons

Discussion in 'Unity UI & TextMesh Pro' started by sevensails, Oct 15, 2015.

  1. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    442
    Hi!

    I have a ScrollRect with tons of button on it!

    But, when the Mouse Cursor is over a button, instead of over the background, the Dragging of the ScrollRect and the MouseWhell does not works.

    Is there a way to fix this?
     
  2. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    442
    No one? This is a huge problem for me.
     
  3. NuclearFriend

    NuclearFriend

    Joined:
    Aug 13, 2015
    Posts:
    13
    Also currently struggling with this, I have been able to get the Buttons to pass the Drag/Scroll events through to the ScrollRect with this simple component

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections;
    5.  
    6. public class ScrollRectButton : MonoBehaviour, IBeginDragHandler,  IDragHandler, IEndDragHandler, IScrollHandler
    7. {
    8.     // Private variables
    9.     //--------------------------------
    10.  
    11.     ScrollRect m_TargetScrollRect;
    12.    
    13.     // Public methods
    14.     //--------------------------------
    15.    
    16.     public void OnBeginDrag(PointerEventData eventData)
    17.     {
    18.         m_TargetScrollRect.OnBeginDrag(eventData);
    19.     }
    20.    
    21.     public void OnDrag(PointerEventData eventData)
    22.     {
    23.         m_TargetScrollRect.OnDrag(eventData);
    24.     }
    25.    
    26.     public void OnEndDrag(PointerEventData eventData)
    27.     {
    28.         m_TargetScrollRect.OnEndDrag(eventData);
    29.     }
    30.    
    31.     public void OnScroll(PointerEventData data)
    32.     {
    33.         m_TargetScrollRect.OnScroll(data);
    34.     }
    35.  
    36.     // Private methods
    37.     //--------------------------------
    38.  
    39.     void Start()
    40.     {
    41.         m_TargetScrollRect = Util.FindComponentInParent<ScrollRect>(transform);
    42.     }
    43. }
    (and Util.FindComponentInParent if you want it)
    Code (CSharp):
    1.  
    2.  
    3.     public static T FindComponentInParent<T>(Transform transform)
    4.     {
    5.         while (transform.parent != null)
    6.         {
    7.             T component = transform.parent.GetComponent<T>();
    8.             if (component != null)
    9.             {
    10.                 return component;
    11.             }
    12.             transform = transform.parent;
    13.         }
    14.         return default(T);
    15.     }
    But there is still one issue remaining, I'm currently trying to figure out how to make the button deselect when the Drag begins. I've tried calling EventSystem.current.SetSelectedGameObject(null), which does nothing. I've tried calling OnDeselect on the Button component, again nothing. Anyone have any experience with this and able to shed some light?
     
    zge likes this.
  4. NuclearFriend

    NuclearFriend

    Joined:
    Aug 13, 2015
    Posts:
    13
    Argh after all my messing around I discovered it was because I had an Event Trigger component on my button, not sure how it got there, but it seems to really mess with the Scroll Rect event handling. Hopefully this saves someone else spending a while digging around.
     
  5. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    105
    This is still an issue in 2017.
     
  6. zge

    zge

    Joined:
    Sep 18, 2014
    Posts:
    35
    Thanks @NuclearFriend !

    I needed this today for a swipeable button in a ScrollRect.

    Just out of interest, any reason you didn't just use GetComponentInParent?