Search Unity

Question Immediate ScrollTo on ScrollView not working for Runtime

Discussion in 'Unity Hub' started by SuperMops, Jul 14, 2022.

  1. SuperMops

    SuperMops

    Joined:
    Feb 7, 2018
    Posts:
    28
    Hi,

    I need a ScrollView to scroll to a certain element immediately after it was created
    and added to the panel.

    When I try this in OnEnable or in a callback for the AttachToPanelEvent this doesn't work.

    Code (CSharp):
    1. private void OnEnable()
    2. {
    3.     UIDocument ui = GetComponent<UIDocument>();
    4.     VisualElement root = ui.rootVisualElement;
    5.  
    6.     ScrollView myScrollView = new ScrollView();
    7.  
    8.     // ... imagine some added Elements to the ScrollView
    9.  
    10.     root.Add(myScrollView);
    11.     myScrollView.ScrollTo(someElem);
    12.  
    13.     // nothing happens
    14. }
    When I try this later (for example on some Input.GetKeyDown test) the scrolling works as expected.

    So why is this not happening immediately and how can I detect that my ScrollView is 'ready' to
    use the ScrollTo() function?
     
  2. SuperMops

    SuperMops

    Joined:
    Feb 7, 2018
    Posts:
    28
    sorry, wrong category. I reposted this in the UIToolkit forum but apparently, I can not delete it here.