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

Is there any way to easily disable button clicks in a ScrollRect that's currently scrolling?

Discussion in 'UGUI & TextMesh Pro' started by yolkfolk, Jul 29, 2019.

  1. yolkfolk

    yolkfolk

    Joined:
    Jul 13, 2014
    Posts:
    1
    Basically when the ScrollRect is scrolling and I press my pointer or finger on the screen to make it stop, if where I press my finger happens to be on top of a button, that button's click event will also be triggered.

    I basically want the ScrollRect to just stop on the first click, and for none of the buttons to be triggered (unless the ScrollRect is not scrolling). I was hoping there was a more robust and efficient manner of doing this besides checking if the ScrollRect's velocity is equal to 0. Any tips?
     
  2. sindrijo

    sindrijo

    Joined:
    Mar 11, 2014
    Posts:
    13
    Place an invisible click-blocker over the content in the scroll view, write a script that monitors the scroll velocity of the the ScrollRect and toggle the click-blocker to when the velocity is crosses some threshold value. This script can also be a button and set the ScrollRect's velocity to zero when it detects a click.
     
    BobberooniTooni likes this.
  3. BobberooniTooni

    BobberooniTooni

    Joined:
    Apr 9, 2021
    Posts:
    46
    I did pretty much exactly what sindrijo described, it worked great! If you use the unity built-in scroll view and click on the content, it automatically removes the scroll rect's velocity, so you don't need to do that manually!

    Enjoy :)

    Code (CSharp):
    1.     public ScrollRect scrollRect;
    2.  
    3.     public GameObject scrollClickBlocker;
    4.  
    5.     bool blockerActive;
    6.  
    7.     void Update()
    8.     {
    9.         if (blockerActive == false && Mathf.Abs(scrollRect.velocity.y) > 40f)
    10.         {
    11.             blockerActive = true;
    12.             scrollClickBlocker.SetActive(true);
    13.         }
    14.         else if (
    15.             blockerActive == true && Mathf.Abs(scrollRect.velocity.y) <= 40f
    16.         )
    17.         {
    18.             blockerActive = false;
    19.             scrollClickBlocker.SetActive(false);
    20.         }
    21.     }