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.
  2. Dismiss Notice

Unity UI ScrollRect how to autoscroll on button navigation

Discussion in 'UGUI & TextMesh Pro' started by Verdemis, Feb 21, 2018.

  1. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Hello everyone,

    I have a horizontally scrolling ScrollRect in my UI, which contains some buttons. The buttons get selected via keyboard or controller. The problem is when a button is selected which isn't completly in the visible area (or isn't in it at all), the ScrollRect doesn't scroll.

    I didn't really work with ScrollRects before, so my question is... is there a simple setting I can make to get it work or do I have to code some script? If I have to write a script, some helpful hints would be appreciated. :)

    Best,
    Verdemis
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm pretty sure you have to script that yourself.

    As for some hints, consider how it fits with the content rect's current local position and size..

    I can't say if this will work with all layouts, but I tried this and compared the currently selected object's local x position + its size delta x to the -transform.local position of the content and the scroll rect's size delta x
    - that was for "past the right side"

    for the left, i checked if the selection's local position x was < -transform.local position x of the content.

    I had mine aligned to the left, so it may require some adjustment if there are other , um , pivots? :)

    In the end, I got it so they were always completely in view.. nothing more, nothing less. :) Tried a few different sizes to be sure (buttons of diff widths).

    I used an event trigger with OnSelect and a method in a script (that was on the content portion). That part's probably not that important. I don't usually use event triggers, and prefer the interfaces directly.

    (I am horrible with those UI setups, and always use top-left lol)

    If no one offers a better solution, feel free to ask for some follow up. I might keep this code I wrote around for a while, if you want to compare notes.. if you write something up. :)
     
    kcfos and Verdemis like this.
  3. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Thank you for your reply... I got it working after a while trying things out :) Maybe my sultion isn't the best, but it is a working one, which is enough for me, for now. :)
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, glad you got something that works :)
     
    Verdemis likes this.
  5. WILEz1975

    WILEz1975

    Joined:
    Mar 23, 2013
    Posts:
    368
    I resume this old post so as not to write another one. I would need that when the selected item is out of the viewport, the UI transform automatically scrolls to display it.


    Screenshot_48.png

    I wrote this code but it still doesn't work as it should. I only care about the Y.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class AutoScrollWinElement : MonoBehaviour
    4. {
    5.     public ElementsGroupNavigation elementsGroupNavigation;
    6.     public float minAutoScrollAt = -20;
    7.     public float maxAutoScrollAt = -285;
    8.     float autoScrollValue = 50;
    9.     public float defaultScrollValue = 50;
    10.  
    11.     public RectTransform viewPort;
    12.     public float selectedPosition;
    13.     public void UpdateSelection()
    14.     {
    15.         if (!elementsGroupNavigation.CheckkSelected()) return;
    16.  
    17.         selectedPosition = viewPort.transform.position.y- elementsGroupNavigation.CheckkSelected().transform.position.y;
    18.  
    19.  
    20.         if (selectedPosition < minAutoScrollAt){
    21.             autoScrollValue =  minAutoScrollAt - selectedPosition - defaultScrollValue;
    22.           transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + autoScrollValue, transform.localPosition.z);
    23.         }
    24.         else
    25.             if (selectedPosition > maxAutoScrollAt)
    26.         {
    27.             autoScrollValue = maxAutoScrollAt - selectedPosition+ defaultScrollValue;
    28.             transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y - autoScrollValue, transform.localPosition.z);
    29.         }
    30.  
    31.  
    32.  
    33. }
    34.  
    35. }
    36.