Class Tween<T>
A fully generic tween that interpolates values of type T using
a caller-supplied getter, setter, and lerp function.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Tween
Assembly: ScyllaCore.dll
Syntax
public class Tween<T> : TweenBase, ITween
Type Parameters
| Name | Description |
|---|---|
| T | The type of value to animate. Any value type or reference type is supported; the caller is responsible for providing a compatible lerp delegate via Initialize(Func<T>, Action<T>, T, float, Func<T, T, float, T>). For the most common Unity types (float, Vector2, Vector3, Vector4, Color, Quaternion) consider using the dedicated concrete classes (TweenFloat, TweenVector3, etc.) which avoid the delegate-dispatch overhead of the generic lerp parameter. |
Remarks
To vs. From tweens: By default this class operates as a "To" tween - the
start value is captured from the getter at play time (Play()
calls CaptureStartValue). To create a "From" tween, call
SetFrom(T) after construction; the provided value is used as the start
and the current property value (read at construction time via the getter) should be
passed as the end value.
Incremental looping: For Incremental loops, call
SetChangeValue(T) with the per-iteration delta. On each loop iteration
PrepareIncrementalLoop() updates _startValue by re-reading the
getter, so the accumulation is based on the actual live value rather than a
mathematically predicted one.
Pool contract: Reset() clears the getter, setter, lerp function,
start value, end value, change value, and the _usesFrom flag, in addition to
the base-class fields. This ensures the instance can be safely returned to
TweenPool and reused without leaking references.
Constructors
Tween()
Creates a new, unconfigured Tween<T> instance.
Declaration
public Tween()
Remarks
This default constructor is used by TweenPool when pre-allocating instances. Call Initialize(Func<T>, Action<T>, T, float, Func<T, T, float, T>) before playing the tween.
Tween(Func<T>, Action<T>, T, float, Func<T, T, float, T>)
Creates a fully configured Tween<T> instance and immediately calls Initialize(Func<T>, Action<T>, T, float, Func<T, T, float, T>).
Declaration
public Tween(Func<T> getter, Action<T> setter, T endValue, float duration, Func<T, T, float, T> lerpFunction)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<T> | getter | A delegate that reads the current value of the target property.
Must not be |
| Action<T> | setter | A delegate that writes the interpolated value back to the target property each frame.
Must not be |
| T | endValue | The value that the tween animates toward. Captured by value at construction time. |
| float | duration | The total playback time in seconds. Negative values are clamped to zero. |
| Func<T, T, float, T> | lerpFunction | A delegate implementing the interpolation logic with signature
|
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
Properties
EndValue
Gets the target value toward which the tween interpolates.
Declaration
public T EndValue { get; }
Property Value
| Type | Description |
|---|---|
| T | The end value supplied to Initialize(Func<T>, Action<T>, T, float, Func<T, T, float, T>). |
StartValue
Gets the value from which the tween interpolates.
Declaration
public T StartValue { get; }
Property Value
| Type | Description |
|---|---|
| T | The start value pinned at play time (or earlier if SetFrom(T) was called).
Returns the default value of |
Methods
ApplyValue(float)
Writes the interpolated value to the target property at the given eased progress. Must be implemented by derived classes to perform type-specific interpolation.
Declaration
protected override void ApplyValue(float easedProgress)
Parameters
| Type | Name | Description |
|---|---|---|
| float | easedProgress | The eased (and Yoyo-corrected) progress value computed by EasedProgress.
This value is intentionally unclamped; elastic and back easing can produce values below
|
Overrides
Remarks
On the very first call when Progress is at or below zero and no explicit
start value has been set, the getter is invoked to capture the live property value
as the start point. This lazy capture is a fallback for cases where
CaptureStartValue() did not run (e.g., when ApplyValue is
called directly during a Kill(bool) with
complete: true). After capture, the lerp function is invoked with the
resolved start and end values and the result is written via the setter.
CaptureStartValue()
Called once during Play() (for new or restarted tweens) to snapshot the current value of the target property as the tween's start value, without applying any change.
Declaration
protected override void CaptureStartValue()
Overrides
Remarks
The base implementation is empty. Derived classes override this to read their getter
delegate and store the result in their start-value field, but only when a start value
has not already been pinned via a SetFrom call. This approach ensures that
the start value reflects the live property value at the moment play begins, not at
the moment the tween was created.
Initialize(Func<T>, Action<T>, T, float, Func<T, T, float, T>)
Configures this tween instance with all required parameters for interpolation.
Resets the _usesFrom flag so the start value will be captured from the
getter at play time (standard "To" tween behavior) unless SetFrom(T)
is called afterward.
Declaration
public Tween<T> Initialize(Func<T> getter, Action<T> setter, T endValue, float duration, Func<T, T, float, T> lerpFunction)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<T> | getter | A delegate that reads the current value of the target property each frame.
Must not be |
| Action<T> | setter | A delegate that writes the interpolated value to the target property each frame.
Must not be |
| T | endValue | The target value to animate toward. Captured by value; later changes to the source variable do not affect the tween unless this method is called again. |
| float | duration | Playback duration in seconds. Clamped to zero; negative values produce an instant-complete tween on the first update. |
| Func<T, T, float, T> | lerpFunction | The interpolation delegate with signature |
Returns
| Type | Description |
|---|---|
| Tween<T> | This tween instance for method chaining. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
PrepareIncrementalLoop()
Called at the end of each Incremental loop iteration to allow derived classes to update internal state before the next iteration begins.
Declaration
protected override void PrepareIncrementalLoop()
Overrides
Remarks
The base implementation is empty. Derived classes that support incremental looping (e.g., TweenFloat, TweenVector3) override this to capture the current property value as the new start point, so each loop iteration adds the change amount on top of the previous iteration's result.
Reset()
Resets all tween state back to initial defaults, preparing the instance for reuse from a pool.
Declaration
public override void Reset()
Overrides
Remarks
Clears all callbacks, resets timing values, generates a new TweenID,
and sets State back to Created. This is
called automatically by the tween pool (TweenPool) before returning an
instance to a caller. Derived classes that override this method must call the base
implementation.
SetChangeValue(T)
Sets the per-iteration change value used during Incremental loops.
Declaration
public Tween<T> SetChangeValue(T changeValue)
Parameters
| Type | Name | Description |
|---|---|---|
| T | changeValue | The amount added to the start value on each loop iteration. The exact interpretation
depends on the supplied |
Returns
| Type | Description |
|---|---|
| Tween<T> | This tween instance for method chaining. |
Remarks
This value is only consulted when LoopType is Incremental. For the built-in concrete tween types such as TweenFloat, this field is computed automatically from the difference between start and end values; the generic Tween<T> class cannot compute this automatically and relies on the caller to supply it here.
SetFrom(T)
Pins an explicit start value for the tween, converting it from a "To" tween (start captured at play time) into a "From" tween (start supplied by the caller).
Declaration
public Tween<T> SetFrom(T fromValue)
Parameters
| Type | Name | Description |
|---|---|---|
| T | fromValue | The value from which the tween begins animating. This overrides automatic start-value capture via the getter. |
Returns
| Type | Description |
|---|---|
| Tween<T> | This tween instance for method chaining. |
Remarks
Once called, the getter is no longer invoked to determine the start value for this play session. The getter is still used by PrepareIncrementalLoop() to capture the live value at the start of each incremental loop iteration.