- Home /
Question by
Crocsx · Jun 01, 2016 at 07:23 AM ·
unity 5uiscrollview
Make an snapped ScrollView
Hi,
So, I m making a menu for an android device.
I would like to make a scrollview using the same behavior that most of the Mobile's menu have. The snapping to the closest element when I scroll.
basically this : https://youtu.be/cd06PzjtIjM?t=6m27s
So I did the following stuff :
public void OnBeginDrag()
{
_scrollStart = _tContainer.localPosition;
}
public void OnScroll()
{
Vector3 totalScrolled = _scrollStart - _tContainer.localPosition;
if(totalScrolled.y > 80)
{
if ((_currIndex + 1) < levels.Count)
{
_currIndex++;
scrollRect.verticalNormalizedPosition = _currIndex / levels.Count;
}
}
if (totalScrolled.y < -80)
{
if (_currIndex-1 > 0)
{
_currIndex--;
scrollRect.verticalNormalizedPosition = _currIndex / levels.Count;
}
}
}
Basically, if I drag over 80, I want to set the snap to the next element of my levelsButtonArray. It works fine. But the problem is that I can't find something that work in order to change the snapping position.
My question is, how do i change the reference 0,0 of the scrollview in order to snap the selected element position?
I searched on the internet that :
scrollRect.verticalNormalizedPosition = Mathf.Lerp(0, 1, _currIndex / levels.Count
but can't make it work...
Thanks for the help.
Comment