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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Set scrollview scroller default starting position?

Discussion in 'UI Toolkit' started by wethings, Nov 23, 2021.

  1. wethings

    wethings

    Joined:
    Aug 26, 2018
    Posts:
    28
    Hi,

    is there a way to set the horizontal/vertical scrollers to start at say 50%?
     
  2. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    521
    There are two options, but no direct method to achieve this using styling (uss/uxml).

    You can set the scroll offset from c#, but in order to do so, you would need to know the size of the scroll view size.
    In order to do that, you would need to
    • Register a callback on the Geometry Change event of the content container. It will be called when the size of all children will be known.
    • When called, look at the size of the container and set the scroll offset. Unregister the callback so that it run only on the first frame.


    You can also use ScrollTo(VisualElement child) if you know which visual element is in the middle. This will also have to be in the Geometry change event of the content container or on the second frame.
     
    wethings likes this.
  3. wethings

    wethings

    Joined:
    Aug 26, 2018
    Posts:
    28
    ah I see thank you!
     
  4. Kanoh_Agito

    Kanoh_Agito

    Joined:
    Jun 9, 2023
    Posts:
    1
    I'm not sure if there was an update or something, but as of July 2023 you can set the starting position using the Value slider in the Scrollbar GameObject in the Inspector. Setting it to 1 sets the scrollbar to the top of the frame, 0 to the bottom. Setting it to 0.5 would start it in the middle. scrollbar.jpg
     
  5. TeorikDeli

    TeorikDeli

    Joined:
    Apr 6, 2014
    Posts:
    117
    Your example is for UGUI. @SimonDufour 's suggestions are still correct way to do this in UI Toolkit.