Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Scroll Rect disable dragging

Discussion in 'UGUI & TextMesh Pro' started by Hinkle, Feb 26, 2015.

  1. Hinkle

    Hinkle

    Joined:
    Sep 10, 2013
    Posts:
    8
    Is there anyway to disable to click & drag functionality of a scroll rect? I just want it to work with the mouse scroll wheel. I suppose I could just code it myself, but I like the elasticity and scroll bar features.
     
  2. jgold

    jgold

    Joined:
    Feb 26, 2015
    Posts:
    6
    Inherit from ScrollRect then override the following:
    public virtual void OnBeginDrag(PointerEventData eventData);
    public virtual void OnDrag(PointerEventData eventData);
    public virtual void OnEndDrag(PointerEventData eventData);​
     
  3. Hinkle

    Hinkle

    Joined:
    Sep 10, 2013
    Posts:
    8
    Thanks! That wasn't quite right but it helped me find the solution. Which was inheriting from ScrollRect and then:

    public override void OnBeginDrag(PointerEventData eventData) { }
    public override void OnDrag(PointerEventData eventData) { }
    public override void OnEndDrag(PointerEventData eventData) { }
     
  4. JadeTheFlame

    JadeTheFlame

    Joined:
    Jul 27, 2014
    Posts:
    7
    This post was helpful and I don't want to necro, but could one of you send me a link on how to inherit from a component in unity? I can't seem to find a guide anywhere.
    PS: I'm a newb :p
     
  5. Hinkle

    Hinkle

    Joined:
    Sep 10, 2013
    Posts:
    8
    Ok so you know how all your scripts start with :

    public class MyScript : MonoBehaviour {...

    That means it inherits from MonoBehaviour, which is the default in unity (thats why you have access to variables such as "gameObject" or "transform" in your scripts. If you change it to :

    public class MyScript : ScrollRect {...

    It now inherits from ScrollRect, which means it will have all the funtionality and variables that are defined in ScrollRect (which in turn inherits from MonoBehaviour). But now you have your own "version" of that class and can make adjustments on top of it.

    The ScrollRect class has a function "OnDrag" that defines what happens when you drag. If you override this function in your class, the "OnDrag" function in the base ScrollRect class will be ignored, and your function will run instead. In the above case the new function does nothing and thus disables the normal OnDrag behaviour.

    Now you can add your new script "MyScript" as a component on your GameObject instead of the regular ScrollRect component and it will act as a regular ScrollRect, but with your adjustments.

    Hope that clears it up for you ;)
     
    R_ACTS, kcfos, Merman and 2 others like this.
  6. Charles-Van-Norman

    Charles-Van-Norman

    Joined:
    Aug 10, 2010
    Posts:
    86
    Note that you have to add
    using UnityEngine.EventSystems;

    to your script for this to work, otherwise it doesn't know about PointerEventData.

    So your full script would look like:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class PreventClickDrag : ScrollRect {
    7.  
    8.     public override void OnBeginDrag(PointerEventData eventData) { }
    9.     public override void OnDrag(PointerEventData eventData) { }
    10.     public override void OnEndDrag(PointerEventData eventData) { }
    11. }
    12.  
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,860
    To necro this even further.... because it's one of the first search results on this topic... another way to do this, if you have some content object that wants to handle click/drag (which you probably do, else why not allow the user to scroll the content with the mouse?), is to implement IDragHandler in your content class, and be sure to call eventData.Use().
    Code (CSharp):
    1.     public void OnDrag(PointerEventData eventData) {
    2.         // Do whatever drag-handling you need to do, then:
    3.         eventData.Use();
    4.     }
    5.    
     
  8. Svpam111

    Svpam111

    Joined:
    Aug 19, 2015
    Posts:
    4
    You could disable "Horizontal" or "Vertical" in Scroll Rect component and set Scrollbar component "Interactable" to be false
     
  9. trivial_user

    trivial_user

    Joined:
    Jul 22, 2020
    Posts:
    1
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class NoDragScrollRect : MonoBehaviour, IEndDragHandler, IBeginDragHandler  {
    7.  
    8.     public ScrollRect EdgesScroll;
    9.  
    10.     public void OnBeginDrag(PointerEventData data)
    11.     {
    12.         EdgesScroll.StopMovement();
    13.         EdgesScroll.enabled = false;
    14.     }
    15.  
    16.     public void OnEndDrag(PointerEventData data)
    17.     {
    18.         EdgesScroll.enabled = true;
    19.     }
    20.  
    21. }
     
  10. JockChen

    JockChen

    Joined:
    Apr 12, 2022
    Posts:
    1
    panel.GetComponent<ScrollRect>().horizontal = false;
     
    Omid7L, Babster and chuotngocha like this.
  11. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    226
    Thanks, the 2 custom scripts (PreventClickDrag replacing ScrollRect and NoDragScrollRect as an extra component), as well as unchecking Horizontal/Vertical worked to disable dragging, with the following subtleties:

    1. All solutions are still not perfect regarding button handling: if there is a button in your scrollable content, and you press it, then move the cursor more than a given threshold of distance, the drag event will still be detected (also it will do nothing), cancelling button press; releasing mouse or touch will not register button click. The only workaround I found for this so far is https://github.com/ViveSoftware/ViveInputUtility-Unity/issues/32 which registers action on mouse press (button down). I have yet to find a solution that preserves true button behaviour on release, even after moving beyond the threshold.
    2. If you choose to uncheck Horizontal/Vertical, you need to set visibility to "Auto Hide" or "Auto Hide and Expand Viewport". "Permanent" Visibility will make the scrollbar disappear if its corresponding direction is disabled. Which makes sense, but since we're hacking the Horizontal/Vertical feature to disable dragging, this has side effects. Ideally, we'd want a checkbox specifically to disable dragging!
    3. In addition unchecking Horizontal/Vertical will disable mouse scroll in this direction, so if you need it, it's not convenient (you'd have to reimplement it manually).

    So if you must rely on proper button behaviour and need mouse scroll, you may be better off with a custom solution (just using the Scrollbar component and coding the rest of the behaviour). If you don't mind the small inconveniences or not use buttons, then you can just use one of those solutions!
     
  12. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    Thank you!
     
  13. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    461
    The ScrollRect events can be intercepted by adding a script like this

    Code (CSharp):
    1. public class ViewportDragHandler : MonoBehaviour, IBeginDragHandler {
    2.  
    3.     private ScrollRect scrollRect;
    4.     void Awake() {
    5.         this.scrollRect = this.GetComponent<ScrollRect>();
    6.     }
    7.  
    8.     public void OnBeginDrag(PointerEventData data) {
    9.         // do your stuff here
    10.         this.scrollRect.OnBeginDrag(data); // pass the event to the ScrollRect, if needed
    11.     }
    12.  
    13. }
     
  14. gotiobg

    gotiobg

    Joined:
    May 10, 2017
    Posts:
    19
    What worked for me to disable dragging in a Scroll View is to disable the Horizontal and Vertical checkboxes in the Scroll Rect component and leave the Scrollbar components interactable (in Scrollbar Horizontal and Scrollbar Vertical).
     
  15. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    I found disabling the Horizontal and Vertical checkboxes disabled the scroll bars too (they were visible & moved but didn't have any effect).

    I disabled dragging of the scrollview by disabling 'Raycast Target' on the image component on the scrollview game object.