Get a value between min and max borders of a scrollbar

Hey Guys,

I have a simple Question related to the Scrollbar GUI Element.
I want to make an Inventory for a Third Person Game.
Now I’m still wondering how to get the correct value of a Scrollbar.

There’s 0 as the minimum Value and 1 as the maximum.

float scrollbarKnobWidth = 0;

int srollbarValue =
GUI.HorizontalScrollbar(new Rect(0, 0, Screen.width, 15),srollbarValue, scrollbarKnobWidth, 0, 1);

Now I want to change the ‘scrollbarKnobWidth’ related to the count of Items that are in the Inventory.
For Example I set the Value to ‘0.6’. In this case I get values from 0 to 0.6 instead of 0 to 1 if I pull the Knob from the left to the right.
Is there a simple way to get always a value between the same minimum and maximum borders independent to the knobwidth?

Directly as a return value from GUI.HorizontalScrollbar? AFAIK, no you can’t. The value returned will always be based on the combination of your left/right/size values and if you change any of those (like size) you change the value range. You can of course use some simple code to force that to return values over the same range. To do that you have to know that your max achievable right value is (right value - size). Am I making sense? :slight_smile:

Thanks for the reply.

Now I got it:

float scrollbarKnobWidth = 0;

int scrollbarPos =
GUI.HorizontalScrollbar(new Rect(0, 0, Screen.width, 15), scrollbarPos, scrollbarKnobWidth, 0, 1);

float valueDifference = (1 - scrollbarPos ) / (1 - scrollButtonWidth) * scrollbarPos;

srollbarValue = scrollbarPos + valueDifference;