Wiki
Clone wikiUnityUIScrollSnaps / HelperClasses / Scroller
Scroller
The Scroller is based on the Android Open Source Project's Scroller. It makes it easy to move something from point A to point B in way that's cleaner and more robust than simple lerping.
Contents
Usage
The Scroller class is not a MonoBehavior, it cannot be attached to a GameObject, it can only be created within another class.
Create it using this structure:
Correct
#!c# using UnityEngine; using UnityEngine.UI.ScrollSnaps; public class TestScript : MonoBehaviour { Scroller m_Scroller; public void Start() { m_Scroller = new Scroller(); } }
Scrollers cannot be created in instance field initializers, such as below, because of how they access the screen's dpi.
Incorrect
#!c# using UnityEngine; using UnityEngine.UI.ScrollSnaps; public class TestScript : MonoBehaviour { Scroller m_Scroller = new Scroller(); }
Available Constructors:
Scroller()
Scroller(float friction)
Scroller(Interpolator interpolator)
Scroller(int minDuration, int maxDuration)
Scroller(float friction, Interpolator interpolator)
Scroller(float friction, int minDuration, int maxDuration)
Scroller(int minDuration, int maxDuration, Interpolator interpolator)
Scroller(float friction, int minDuration, int maxDuration, Interpolator interpolator)
Dependencies:
- None
Properties
Propert | Type | Description |
---|---|---|
interpolator | readonly Interpolator | Returns the scroller's interpolator. The interpolator modifies how scrolling is animated. |
friction | readonly float | Returns the scroller's friction. The friction controls how quickly or slowly fling animations come to a stop. |
minDuration | readonly int | Returns the minimum duration, in miliseconds, for all animations, scroll and fling. |
maxDuraiton | readonly int | Returns the maximum duration, in miliseconds, for all animations, scroll and fling. |
currentPosition | readonly Vector2 | Returns the current position in the scroll. Before calling this it is best to call ComputeScrollOffset() if you are looking for up-to-date data. |
startPosition | readonly Vector2 | Returns the start position of the scroll. |
finalPosition | readonly Vector2 | Returns the final position of the scroll. |
durationOfLatestAnimation | readonly int | Returns the total duration of the latest animation. |
timePassedSinceStartOfAnimation | readonly int | Returns the amount of time since the start of the latest animation. |
currentVelocity | readonly float | Returns the current velocity of the scroll. |
isFinished | readonly bool | Returns if the latest animation is finished. |
isFlinging | readonly bool | Returns if true if the scroller is currently animating a fling. |
isScrolling | readonly bool | Returns true if the scroller is currently animating a scroll. |
Public Functions
Type | Function |
---|---|
void | SetFinalX(int newX) |
Set the final position on the X axis, of the current animation. | |
void | SetFinalY(int newY) |
Set the final position on the Y axis, of the current animation. | |
void | SetFinalPosition(Vector2 newPos) |
Set the final position of the current animation. | |
void | ShiftAnimation(Vector2 offset) |
Shifts the start and end positions of the animation by the offset, does not affect the duration or speed. | |
void | ExtendDuration(int extendMS) |
Extend the scroll animation. This allows the running animation to scroll further and longer in combination with SetFinalPosition(). | |
bool | ComputeScrollOffset() |
Call this when you want to know the new location. Returns true if the animation is not done yet. | |
void | StartScroll(Vector2 startPos, Vector2 endPos) |
Start scrolling by providing a start position and an end position, the duration will be the default value of 250 milliseconds. | |
void | StartScroll(Vector2 startPos, Vector2 endPos, int duration) |
Start scrolling by providing a start position, an end position, and the duration of the scroll in milliseconds. | |
void | StartScroll(int startX, int startY, int dx, int dy, int duration) |
Start scrolling by providing a start position, the movement delta, and the duration of the scroll in milliseconds. | |
void | Fling(Vector2 startPos, Vector2 velocity) |
Start scrolling based on a velocity. The distance traveled will depend on the velocity. | |
void | Fling(Vector2 startPos, Vector2 velocity, Vector2 minPos, Vector2 maxPos) |
Start scrolling based on a velocity. The distance traveled will depend on the velocity. | |
void | Fling(int startX, int startY, int velocityX, int velocityY, int minX, int minY, int maxX, int maxY) |
Start scrolling based on velocity. THe distance traveled will depend on the velocity. | |
void | ForceFinish() |
Stops the animation. Contrary to AbortAnimation() force finishing the animation does not move the scroller to the final position. | |
void | AbortAnimation() |
Stops the animation. Contrary to ForceFinish() aborting the animation moves the scroller to the final position. |
Interpolators
Interpolators are used by the Scroller to modify how scrolling animations animate. The Scroller comes with 9 built-in interpolators, but you can also create your own. Simply impliment the Interpolator interface.
#!c# public interface Interpolator { float GetInterpolation(float input); }
Here is a simple example of an interpolator, this one is provided with the Scroller script.
#!c# public class AccelerateInterpolator : Interpolator { public float GetInterpolation(float input) { return input * input; } }
Built-In Interpolators
Viscous FluidI nterpolator
Accelerate Decelerate Interpolator
Accelerate Interpolator
Anticipate Interpolator
Anticipate Overshoot Interpolator
Bounce Interpolator
Decelerate Interpolator
Linear Interpolator
Overshoot Interpolator
For a visual example of the interpolators see the Directional Scroll Snap Example Project.
Updated