2
\$\begingroup\$

So I have done some research on the matter but I can't find anything.

I created a UI scrollbar and asigned 10 steps to it.Then I noticed that the values of the scrollbar does not change in reference of the handle's steps but in reference of the pointer's position(like a slider).That seriously slows down the performance since it has a lot more to process "onValueChanged" and it is unnecessary for what I am trying to achieve.

The values of the steps should go step 1: 0.1(1),step 2: 0.2(2) etc.I need to get these values and nothing in between or the step that the handle is currently on.

How can I do that ?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

As you noticed the value, when the scrollbar is dragged, changes in accordance to pointer's position (this does not happen if you are using keys). From Unity docs:

The Scrollbar has a single event called On Value Changed that responds as the user drags the handle.

Scrollbar OnValueChanged is called at most once per frame. This allows you to react to scrollbar movement in every frame.

If you need to fix your update operations to a given step you need to account for that in your code. Store the last scrollbar Value, and in OnValueChanged check if the new position corresponds to a different step than in the previous frame, otherwise return (this will have little impact on performance). Example code (incomplete):

public OnValueChanged(float value) {
  currentStep = Mathf.round(scrollbar.Value * 10.0f);
  if (currentStep == lastStep) return;

  // Your code here (depending on your needs you may need to iterate
  // from last to current step)...

  lastStep = currentStep;
}

One thing that is calling my attention is that a scrollbar will automatically set its handle size according to the target scroll area, thus making the numberOfSteps unuseful. Perhaps you shall consider using a Slider instead.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .