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 How to Change Scrollview Scrolling Speed?

Discussion in 'UI Toolkit' started by sharkbitgamesdev, Dec 2, 2021.

  1. sharkbitgamesdev

    sharkbitgamesdev

    Joined:
    Dec 2, 2019
    Posts:
    18
    Apparently it's the page size variable for whatever scrollbar you are using, but changing that hasn't made any effect? It always scrolls at a painfully glacial pace. Anyone know how to do this? Many thanks in advance.
     
  2. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    759
  3. sharkbitgamesdev

    sharkbitgamesdev

    Joined:
    Dec 2, 2019
    Posts:
    18
    Thanks a bunch! Saw that thread, and dismissed it, thinking it wouldn't be relevant. Good thing you came around.

    Kind of annoying to have to use a workaround for now, since the solution given by the Unity team didn't work. But, hey, it's working now, so I won't complain too much.
     
    MousePods likes this.
  4. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    366
    How did you do it?
    The workaround in that thread did not do it for me. Maybe I did something wrong?
     
  5. sharkbitgamesdev

    sharkbitgamesdev

    Joined:
    Dec 2, 2019
    Posts:
    18
    This is the solution I am using:
    Code (CSharp):
    1.  
    2. public class BetterScrollViews
    3. {
    4.     public ScrollView scrollView;
    5.     public float scrollVelocity;
    6.     public float scrollDamping = 0.1f;
    7.     public Scroller verticalScroller;
    8.  
    9.     public BetterScrollViews(ScrollView scroll)
    10.     {
    11.         scrollView = scroll;
    12.         verticalScroller = scroll.Q("unity-content-and-vertical-scroll-container").
    13.             Q<Scroller>();
    14.  
    15.         verticalScroller.valueChanged += Change;
    16.  
    17.         scrollView.RegisterCallback<WheelEvent>(ScrollCallback);
    18.     }
    19.  
    20.     public void ScrollCallback(WheelEvent evt)
    21.     {
    22.         scrollView.UnregisterCallback<WheelEvent>(ScrollCallback);
    23.         scrollVelocity += evt.delta.y * 100;
    24.         // Stop the event here so the builtin scroll functionality of
    25.         //the list doesn't activate
    26.         evt.StopPropagation();
    27.         scrollView.RegisterCallback<WheelEvent>(ScrollCallback);
    28.     }
    29.  
    30.     public void Change(float num)
    31.     {
    32.         verticalScroller.valueChanged -= Change;
    33.         verticalScroller.value += scrollVelocity;
    34.         scrollVelocity -= scrollVelocity * scrollDamping;
    35.         verticalScroller.valueChanged += Change;
    36.     }
    37. }
    This works. The way I use it is essentially to just instantiate this instead of Scrollview.
     
    manuelgoellnitz likes this.
  6. OkFlyQiang

    OkFlyQiang

    Joined:
    Jun 14, 2021
    Posts:
    3
    Have u solved this problem?
    I got a method to solve it and here post it in case of someone struggling to fix it.

    Code (CSharp):
    1.  yourScrollView.RegisterCallback<WheelEvent>(evt =>
    2.             {
    3.                 ToolMethods.rSetField(typeof(ScrollView), yourScrollView, "m_SingleLineHeight", 300, BindingFlags.NonPublic | BindingFlags.Instance);
    4.             });
    the rSetField method is
    Code (CSharp):
    1. public static void rSetField(Type type, object instance, string fieldName, object val, BindingFlags bindingFlags)
    2.         {
    3.             PrintReflectionInfo(type, instance, fieldName);
    4.  
    5.             type.GetField(fieldName, bindingFlags).SetValue(instance, val);
    6.         }
    hope this works for you.
     
  7. OkFlyQiang

    OkFlyQiang

    Joined:
    Jun 14, 2021
    Posts:
    3
    e,just ignore this this line
     
  8. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    366
    Both workaround work fine.
    I am a little concerned about that constant unregister and register of the callback in the first one, so I used the second one.
    But I feel a bit dirty having to use reflection for it! :D
     
    magnetic_scho likes this.