Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

IDragHandler on child blocks Scrollrect scrolling

Discussion in 'UGUI & TextMesh Pro' started by unity_P-dQObz0sCT9rQ, Dec 7, 2019.

  1. unity_P-dQObz0sCT9rQ

    unity_P-dQObz0sCT9rQ

    Joined:
    Aug 28, 2019
    Posts:
    6
    Hello,

    I'm trying to implement a scrollrect where you can press and hold items to drag them, and before you press and hold, the scrollrect behaves normally. (IDragHandler is on the child object)
    But if i Have IDraghandler on the child, you can no longer scroll the Scrollrect by pressing it. How can i fix that?

    basically, i want:
    The scrollrect to work normally, but when you press and hold an item, the scrollrect stops and the item is dragged
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    you would need to extend the
    StandaloneInputModule
    , override
    Process
    and
    ProcessDrag
    there and handle your custom scroll-logic inside these methods...
    It is not a trivial task.
     
    unity_P-dQObz0sCT9rQ likes this.
  3. unity_P-dQObz0sCT9rQ

    unity_P-dQObz0sCT9rQ

    Joined:
    Aug 28, 2019
    Posts:
    6
    Thanks, I ended up putting a draggable script on an image that takes up half the object, and the other half can scroll. I'll keep in mind what you said
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    My approach to this problem would probably be to get rid of the child's IDragHandler and get rid of the ScrollRect, and instead create my own custom component that detects drags and various other events like PointerDown and implements both of the desired functions depending on what the pointer does.
     
  5. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    Something like this should pass control back to the parented ScrollRect

    Code (CSharp):
    1.  
    2. var scrollRect = GetComponentInParent<ScrollRect>();
    3. eventData.pointerDrag = scrollRect.gameObject;
    4. EventSystem.current.SetSelectedGameObject(scrollRect.gameObject);
    5.  
    6. scrollRect.OnInitializePotentialDrag(eventData);    
    7. scrollRect.OnBeginDrag(eventData);
    8.  
     
  6. FardinHaque

    FardinHaque

    Joined:
    Apr 13, 2015
    Posts:
    7
    Awesome! This did the trick! Thanks you.
     
    zhuchun likes this.