voidUpdateDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) return;
if (m_ContainerRect == null) return;
Vector2 localCursor; if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(m_ContainerRect, eventData.position, eventData.pressEventCamera, out localCursor)) return;
// bugfix (case 802330) clamp01 input in callee before calling this function, this allows inertia from dragging content to go past extremities without being clamped m_Value = input;
// If the stepped value doesn't match the last one, it's time to update if (currentValue == value) return;
UpdateVisuals(); if (sendCallback) { UISystemProfilerApi.AddMarker("Scrollbar.value", this); m_OnValueChanged.Invoke(value); } }
privatevoidUpdateVisuals() { #if UNITY_EDITOR if (!Application.isPlaying) UpdateCachedReferences(); #endif m_Tracker.Clear();
if (m_HorizontalScrollbar) m_HorizontalScrollbar.onValueChanged.AddListener(SetHorizontalNormalizedPosition); if (m_VerticalScrollbar) m_VerticalScrollbar.onValueChanged.AddListener(SetVerticalNormalizedPosition);
protectedvirtualvoidSetNormalizedPosition(floatvalue, int axis) { EnsureLayoutHasRebuilt(); UpdateBounds(); // How much the content is larger than the view. float hiddenLength = m_ContentBounds.size[axis] - m_ViewBounds.size[axis]; // Where the position of the lower left corner of the content bounds should be, in the space of the view. float contentBoundsMinPosition = m_ViewBounds.min[axis] - value * hiddenLength; // The new content localPosition, in the space of the view. float newLocalPosition = m_Content.localPosition[axis] + contentBoundsMinPosition - m_ContentBounds.min[axis];
if (m_HorizontalScrollbar) m_HorizontalScrollbar.onValueChanged.RemoveListener(SetHorizontalNormalizedPosition); if (m_VerticalScrollbar) m_VerticalScrollbar.onValueChanged.RemoveListener(SetVerticalNormalizedPosition);
publicvirtualvoidOnDrag(PointerEventData eventData) { if (!m_Dragging) return;
if (eventData.button != PointerEventData.InputButton.Left) return;
if (!IsActive()) return;
Vector2 localCursor; if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(viewRect, eventData.position, eventData.pressEventCamera, out localCursor)) return;
UpdateBounds();
var pointerDelta = localCursor - m_PointerStartLocalCursor; Vector2 position = m_ContentStartPosition + pointerDelta;
// Offset to get content into place in the view. Vector2 offset = CalculateOffset(position - m_Content.anchoredPosition); position += offset; if (m_MovementType == MovementType.Elastic) { if (offset.x != 0) position.x = position.x - RubberDelta(offset.x, m_ViewBounds.size.x); if (offset.y != 0) position.y = position.y - RubberDelta(offset.y, m_ViewBounds.size.y); }
publicvirtualvoidOnScroll(PointerEventData data) { if (!IsActive()) return;
EnsureLayoutHasRebuilt(); UpdateBounds();
Vector2 delta = data.scrollDelta; // Down is positive for scroll events, while in UI system up is positive. delta.y *= -1; if (vertical && !horizontal) { if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y)) delta.y = delta.x; delta.x = 0; } if (horizontal && !vertical) { if (Mathf.Abs(delta.y) > Mathf.Abs(delta.x)) delta.x = delta.y; delta.y = 0; }
if (data.IsScrolling()) m_Scrolling = true;
Vector2 position = m_Content.anchoredPosition; position += delta * m_ScrollSensitivity; if (m_MovementType == MovementType.Clamped) position += CalculateOffset(position - m_Content.anchoredPosition);