Class ScyllaScrollRect
A scroll rect that keeps the elastic bounce for dragging, clamps wheel and trackpad scrolling to the ends of the content, and scrolls horizontally in the direction the gesture asks for.
Implements
Inherited Members
Namespace: Scylla.Core.Util.UI
Assembly: ScyllaCore.dll
Syntax
[AddComponentMenu("")]
public class ScyllaScrollRect : ScrollRect, IInitializePotentialDragHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IScrollHandler, IEventSystemHandler, ICanvasElement, ILayoutElement, ILayoutGroup, ILayoutController
Remarks
Two corrections, both on the scroll path only. The horizontal direction fix is described on OnScroll(PointerEventData); the clamping is the reason the class was written and is described below.
Unity's own scroll rect applies a scroll event straight to the content position and
only clamps it when the movement type is Clamped. In Elastic mode the
delta goes on unchecked, so the content can be pushed as far past the end as the input
asks for and the spring then has that whole distance to travel back.
A mouse wheel sends a fixed step and never gets far enough for that to show. A macOS trackpad sends a continuous, momentum-carried stream that keeps arriving after the fingers lift, which is enough to throw the whole content off screen and leave it out there for about a second before it springs back.
Clamping only the scroll path is what keeps both behaviours: a drag past the end still resists and bounces, because that overshoot is the user pulling on it directly and is bounded by how far they dragged.
Use it exactly as ScrollRect, on either axis or both. Configure the elasticity,
inertia and sensitivity as usual; only the scroll-wheel path behaves differently.
Methods
OnScroll(PointerEventData)
Applies a wheel or trackpad scroll with the horizontal axis corrected, then pins the content back inside its ends.
Declaration
public override void OnScroll(PointerEventData eventData)
Parameters
| Type | Name | Description |
|---|---|---|
| PointerEventData | eventData | Pointer event carrying the scroll delta. |
Overrides
Remarks
Unity's scroll rect flips the vertical delta, with the comment that down is positive for a scroll event while up is positive in the UI system, and then does not do the same for the horizontal one. The result is that a two-finger swipe left scrolls the content right and a swipe right scrolls it left, while the vertical axis behaves correctly. Flipping the horizontal delta the same way puts both axes back on the platform's convention.
Only when UnityEngine.UI.ScrollRect.horizontal is on. With it off, Unity repurposes a horizontal delta as a vertical one so a sideways wheel still drives a vertical list, and flipping it there would invert that instead of fixing anything.
An axis whose content already fits has its delta dropped. In Elastic mode
Unity still moves content that is shorter than the viewport and then springs it
back, so a trackpad swipe across a table with nothing to scroll made the whole
table drift and bounce. There is nowhere to go on such an axis, so the honest
response to the gesture is to do nothing.
The event's delta is restored before returning. It is a pooled object that the event system may pass on to another handler, and leaving a modified delta on it would make this component change how an unrelated one behaves.