Posting here because it's the top google serach.
I was able to get UIScrollBar working with UIWrapContent. Do not reference the scroll bar in your scroll view script.
The only assumption in the posted code is that your vertical scrollbars go top to bottom and your horizontal scroll bars go left to right. I'm sure you can easily modify if this assumption isn't true to you.
do this code when the scrollview moves. EX: scrollView.onMomentumMove += UpdateScrollbar;
_svStartLocalPos = _scrollView.transform.localPosition;
UpdateScrollbar()
{
float pct = 0.0f;
float endPos = 0.0f;
if(_scrollView.movement == UIScrollView.Movement.Vertical)
{
endPos = (_wrapContent.itemSize * (NUM_ITEMS / (float)NUM_COLUMNS) - _uiPanel.GetViewSize().y) + _svStartLocalPos.y;
pct = Mathf.Clamp((_scrollView.transform.localPosition.y - _svStartLocalPos.y) / (endPos - _svStartLocalPos.y), 0.0f, 100.0f);
}
else if(_scrollView.movement == UIScrollView.Movement.Horizontal)
{
endPos = (_wrapContent.itemSize * (NUM_ITEMS / (float)NUM_ROWS) - _uiPanel.GetViewSize().x) - Mathf.Abs(_svStartLocalPos.x);
pct = Mathf.Clamp((_scrollView.transform.localPosition.x - _svStartLocalPos.x) / (_svStartLocalPos.x - endPos), 0.0f, 100.0f);
}
_scrollBar.value = Mathf.Lerp(_scrollBar.value, pct, 0.1f);
}
If you want to drag the scroll bar or click on the background to jump
if(_scrollBar.backgroundWidget != null)
{
UIEventListener bgl = UIEventListener.Get(_scrollBar.backgroundWidget.gameObject);
if(bgl != null)
{
bgl.onPress += OnScrollBarPressed;
bgl.onDrag += OnScrollBarDragged;
}
}
void OnScrollBarPressed(GameObject go, bool isPressed)
{
if(!isPressed)
{
OnScrollBarChanged();
}
}
void OnScrollBarDragged(GameObject go, Vector2 delta)
{
OnScrollBarChanged();
}
void OnScrollBarChanged()
{
Vector3 newLocalPos = _scrollView.gameObject.transform.localPosition;
if(_scrollView.movement == UIScrollView.Movement.Vertical)
{
newLocalPos.y = _scrollBar.value * (_wrapContent.itemSize * (NUM_ITEMS / (float)NUM_COLUMNS) - _uiPanel.GetViewSize().y) + _svStartLocalPos.y;
}
else if(_scrollView.movement == UIScrollView.Movement.Horizontal)
{
newLocalPos.x = -_scrollBar.value * (_wrapContent.itemSize * (NUM_ITEMS / (float)NUM_ROWS) - _uiPanel.GetViewSize().x) + _svStartLocalPos.x;
}
SpringPanel.Begin(_scrollView.panel.cachedGameObject, newLocalPos, 8).onFinished = OnSpringPanelFinished;
}
void OnSpringPanelFinished()
{
_scrollView.RestrictWithinBounds(false);
}