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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Solved: ScrollView scroll to bottom

Discussion in 'UI Toolkit' started by timvb3, May 24, 2022.

  1. timvb3

    timvb3

    Joined:
    Apr 11, 2022
    Posts:
    2
    Hello,

    I am trying to programattically scroll to the bottom of a ScrollView after I add new item. (I am trying to create a log of messages e.g. events that have occurred)

    In UI Builder, I have added a ScrollView. In my script I instantiate and add my new item to the ScrollView. After that i try to change the verticalScroller value as per some old tutorials. This will only ever scroll to the top no matter what value i set it to. I have tried 0.0f, 1.0f and some inbetween values.


    Code (CSharp):
    1. TemplateContainer template = messageTemplate.Instantiate();
    2.  
    3. template.Q<Label>("MessageLabel").text = message;
    4.  
    5. ScrollView messageList = rootElement.Q<ScrollView>("MessageBox");
    6. messageList.Add(template);
    7.  
    8. messageList.verticalScroller.value = 0.0f;
    Can anyone recommend a turorial/manual/reference guide that uses UI Builder / Toolkit?

    Thanks
    Tim
     
  2. bubba01453

    bubba01453

    Joined:
    Aug 30, 2015
    Posts:
    1
    Set the scroller's value to its highValue once it becomes positive.
    Code (CSharp):
    1.  
    2. scroller.value = scroller.highValue > 0 ? scroller.highValue : 0;
    3.  
    The highValue is positive if there are more items than can be viewed and negative if there is empty space in the view. This code has the items fill from the top of the view (value = 0) and then forces the last items to be visible (value = highValue). If you want your items to fill from the bottom, always use highValue.

    This at least happens in 2022.1.1f1. The Scroller class has a lowValue member but I have never seen it be anything but 0.
     
    timvb3 likes this.
  3. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    ScrollView has a ScrollTo() method, it does not help in your case?

    Code (CSharp):
    1. messageList.ScrollTo(template);
     
  4. timvb3

    timvb3

    Joined:
    Apr 11, 2022
    Posts:
    2

    Thanks, you are correct. That did the trick.
     
  5. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I've just had the issue and solved it with is code in my document controller:

    Code (csharp):
    1.  
    2.         private IEnumerator scrollLater(ScrollView list, VisualElement item)
    3.         {
    4.             yield return new WaitForSeconds(0.1f);
    5.             list.ScrollTo(item);
    6.         }
    7.  
    8.         public virtual void DelayedScroll(ScrollView list, VisualElement item)
    9.         {
    10.             StartCoroutine(scrollLater(list, item));
    11.         }
    12.  
    The problem was that after removing the last item the list would not scroll to the bottom because the geometry was not updated. I tried using a geometry change event as well as a remove from panel event but that didn't work. WaitForFixedUpdate() or WaitForEndOfFrame() don't always work.
     
    Last edited: Nov 3, 2022
  6. rezuma

    rezuma

    Joined:
    Jun 23, 2015
    Posts:
    6
    Thank you, this was my problem too. After adding text it wasn't scrolling all the way to the bottom even with the code above. It seems it's because scroller.highValue wasn't updating, adding a coroutine fixed it.
     
  7. ShokWayve

    ShokWayve

    Joined:
    Jan 16, 2013
    Posts:
    126
    Is there a way to make the scrollview go to the selected item?

    Thanks.
     
  8. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Actually it's better to do something like that:

    Code (csharp):
    1.  
    2. list.schedule.Execute(() => list.ScrollTo(item));
    3.  
     
    Songerk likes this.