Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

ScrollRect doesn't scroll when clicking/dragging between items

Discussion in 'UGUI & TextMesh Pro' started by pateras, Jan 27, 2015.

  1. pateras

    pateras

    Joined:
    Jan 12, 2013
    Posts:
    50
    I have a ScrollRect whose Content property is set to a HorizontalLayout container that is the parent to the list of items the user can scroll between. So it looks like this:

    Scroller (ScrollRect)
    Container (HorizontalLayout, Scroller's Content)
    Item
    Item
    Item​

    This works great, as long as the user clicks directly on an item to scroll. If the user clicks in between items, then no scrolling takes place.

    How can I make the ScrollRect scroll anywhere on Container, including in between the items?

    Thank you.
     
  2. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    314
    Do you have a background image element in the Scroller or Container? I encountered a similar problem when I disabled the image, had to enable it and put the alpha to 0.
     
    mbasharahil, Jarazz and GTDGames like this.
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,677
    Right, no there isn't a function or method you can call to move the content within a ScrollRect, all the necessary bits are private / protected in the ScrollRect component.
    However, the way the ScrollRect works is that it moves the transform position of the Content GameObject.

    So to move / step up the Content GameObject you will need a script to alter the Content GO position, which is very simple.
    However I'd also look at the ScrollRect source (In the UI Bitbucket repo) and use a similar method that it uses to ensure the Content GO stays within the bounds of the ScrollRect area.
    Once you have your script, call the method in your script from the Button on click event (either in the editor or in code) and hay presto.
    I did a little sample and tested it, if you need it let me know.
     
  4. pateras

    pateras

    Joined:
    Jan 12, 2013
    Posts:
    50
    That was it. Once I added an Image (with no sprite) and set the alpha to 0, I got the behavior that I wanted.

    Thank you.
     
    Jarazz likes this.
  5. Jaakk0S

    Jaakk0S

    Joined:
    Apr 19, 2020
    Posts:
    63
    Necroing here but I got into a place where the ScrollRect viewport won't scroll if I mouse-drag it. The solution was to flag the Viewport a Raycast Target. This also activated the mouse wheel scroll.