Search Unity

[SOLVED] ScrollRect not working on background ?

Discussion in 'UGUI & TextMesh Pro' started by Eldoir, Aug 25, 2016.

  1. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    Hello,
    I create a new thread for this problem even if I'm still surprised no one asked this question before (or I couldn't find it) : I have a ScrollRect, and I would want it to work even when I click on its background. For a better explanation, this is an example in thumbnail : the red zone is where I click and nothing happens, because ScrollRect seems to work only on its children elements. Still, I want to be able to scroll even if I don't click on any of those elements.
    I would want to define an entire zone where scrolling works. I couldn't find any solution on the web, but maybe I wasn't searching with the right keywords. I don't know if I explained my problem well !
    Thanks in advance fro your replies :)
     

    Attached Files:

  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    The solution is add a Panel (call it Viewport) as a child of the ScrollRect Object. Place this panel in the ScrollRect's ViewPort Field. You add a mask for the ScrollRect as a component to ViewPort panel Then add another Panel (we will call it Content) as a child of Viewport. Content is where add Layout component you want (Grid/Horizontal/Vertical Layout group) and then all the buttons. You can also attach a script to Content to dynamically create all the buttons as children of it. This will cause Scroll rect to scroll the ViewPort which will include all the empty spaces. You can also create Scrollbars as children of the original ScrollRectObject and put them in the ScrollRects's HOrizontal and Vertical ScrollBar fields.

    It later versions of Unity this implementation is already done for you in the UI->ScrollView object.
     
  3. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    Nevermind, I answered my own question :
    You just have to create an empty child in your ScrollRect, make it fit the size of your ScrollRect, and then add an Image component with no sprite and set its alpha to 0. Make sure RaycastTarget is checked, and put this new GameObject on top of the children of the ScrollRect, so it does not block the raycasts of any other child of the Scrollect.
    And that's it ! :)

    Edit : Oh, I just saw your answer, thanks @takatok :) It works too !
     
  4. dragonclaw007

    dragonclaw007

    Joined:
    Jul 21, 2020
    Posts:
    2
    hey im having a similar issue....my scroll rect is not scrolling anymore.....ive tried everything including the mentioned answer above....can anyone help me understand the problem
     
    Rostcraft likes this.