Repository navigation
Scroll Snap
Helpers for ScrollRect.
Moves content so a child is centered in the viewport, shifted by an optional offset (positive moves the child right / up). Unless the ScrollRect movement type is Unrestricted, the content stops at its edges, so the first and last items get as close to the center as the content allows. The result does not depend on the anchors or pivots of the content and the items. Calls Canvas.ForceUpdateCanvases() first and stops the inertia on the snapped axes.
using Mane.Unity.UI;
using UnityEngine.UI;
scroll.SnapTo(row.transform);
scroll.SnapTo(row.transform, new Vector2(0f, 12f));
scroll.SnapXTo(row.transform, 8f);
scroll.SnapYTo(row.transform);SnapTo keeps the current position on axes that are not enabled (horizontal / vertical). SnapXTo / SnapYTo only write that axis, and do nothing when it is not enabled.
ScrollTo, ScrollXTo and ScrollYTo move to the same position as the snap methods, over time:
using Mane.DotNet;
using Mane.Unity;
using Mane.Unity.UI;
scroll.ScrollTo(row.transform, 500f, Easing.CubicOut);
scroll.ScrollYTo(row.transform, 500f, Easing.BackOut, 12f);
scroll.ScrollTo(row.transform, 500f, Easing.SinInOut, durationLimits: new MinMaxFloat(.2f, .8f));
// Wait for the end inside a coroutine.
yield return scroll.ScrollTo(row.transform, 500f, Easing.QuadInOut);-
speedis the average speed, in units per second of the content's parent. The duration is the distance divided by it, so a far child takes longer than a near one. -
durationLimits(aMinMaxFloat, optional) keeps that duration in a corridor, so a near child does not jump and a far one does not crawl. The default is 0.25–0.8 seconds. -
easingis anAnimationCurvewith time and value on 0–1, such as the Easing presets from Mane Tools Unity. Null moves linearly. Overshooting curves (Back, Elastic, Spring) pass the target where there is room, but stop at the content edges. - It runs as a coroutine on the
ScrollRect, on unscaled time like theScrollRectitself, and returns that coroutine. Outside Play mode, on an inactive object, or with a speed that is not positive, it snaps at once and returns null. - Another scroll or snap on the same
ScrollRect, or a drag, stops it where it is.StopScrolling()stops it from code.
openupm add com.manefunction.tools-unity-ui