Is there a way to set the position of a scrollview’s viewport? So when we release on dragging, it snaps the view onto a child.
Used to implement snapping by setting the anchoring position with the old ui system
Is there a way to set the position of a scrollview’s viewport? So when we release on dragging, it snaps the view onto a child.
Used to implement snapping by setting the anchoring position with the old ui system
void Start()
{
_scrollView = _myUIPanel.Query<ScrollView>("ScrollView");
_scrollView.RegisterCallback<MouseUpEvent>( OnMouseUp, TrickleDown.TrickleDown );
}
public void OnMouseUp(MouseUpEvent evt)
{
int segments = _scrollView.childCount - 1;
float width = _scrollView.horizontalScroller.slider.range / segments;
float normal = Mathf.InverseLerp(0f, _scrollView.horizontalScroller.slider.range, _scrollView.scrollOffset.x);
float bValue = Mathf.Lerp(0f, segments, normal);
_scrollView.scrollOffset = new Vector2(Mathf.RoundToInt(bValue) * width, 0);
}
Yep if anyone was wondering, ScrollView’s scrollOffset is the property we can use to achieve snapping
Ok dosent work for mobile.
OnMouseUp gets called when your finger moves to scroll and again when u actually lift your finger off the screen.
OnPointerUp does not even get called when you scroll and release finger
For mobile, you have to handle touch events, since it is multi-touch. Something like
void Update() {
Touch touch = Input.touches[0];
if (touch.phase == TouchPhase.End) {...}