Class TweenColor
A performance-optimized, sealed tween implementation that interpolates UnityEngine.Color values. Supports two modes: full RGBA color interpolation and alpha-only fade transitions. Suitable for animating material colors, UI element colors, sprite colors, and any other property expressed as a UnityEngine.Color.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Tween
Assembly: ScyllaCore.dll
Syntax
public sealed class TweenColor : TweenBase, ITween
Remarks
Full-color mode: Initialized via Initialize(Func<Color>, Action<Color>, Color, float). All four channels (R, G, B, A) are independently interpolated each frame using Color(Color, Color, float).
Alpha-only mode: Initialized via InitializeAlpha(Func<Color>, Action<Color>, float, float). Only the alpha channel is animated; R, G, and B are preserved from the start color (read via the getter at play time). This is the correct mode for fading objects in and out without touching their tint. Internally uses ColorAlpha(Color, float, float).
From tweens: Call SetFrom(Color) (full color) or SetFromAlpha(float) (alpha-only) before Play() to pin an explicit starting value instead of capturing it from the getter at play time.
Typical full-color usage via the ScyllaTween factory:
<pre><code class="lang-csharp">var tween = ScyllaTween.To(
() => renderer.material.color,
c => renderer.material.color = c,
Color.red,
0.4f)
.SetEase(EaseType.Linear) .SetManaged(true) .Play();
Pool contract: Reset() resets both color fields to
UnityEngine.Color.white and clears the getter, setter, _usesFrom,
and _alphaOnly flags so the instance can be safely returned to
TweenPool.
Constructors
TweenColor()
Creates a new, unconfigured TweenColor instance.
Declaration
public TweenColor()
Remarks
Used by TweenPool when pre-allocating instances. Call Initialize(Func<Color>, Action<Color>, Color, float) or InitializeAlpha(Func<Color>, Action<Color>, float, float) before playing.
TweenColor(Func<Color>, Action<Color>, Color, float)
Creates a fully configured TweenColor in full-color mode and immediately calls Initialize(Func<Color>, Action<Color>, Color, float).
Declaration
public TweenColor(Func<Color> getter, Action<Color> setter, Color endValue, float duration)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Color> | getter | A delegate that reads the current UnityEngine.Color of the target property.
Must not be |
| Action<Color> | setter | A delegate that writes the interpolated UnityEngine.Color to the target property
each frame. Must not be |
| Color | endValue | The target UnityEngine.Color to animate toward. |
| float | duration | Total playback time in seconds. Negative values are clamped to zero. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
Properties
EndValue
Gets the UnityEngine.Color target value toward which the tween interpolates.
Declaration
public Color EndValue { get; }
Property Value
| Type | Description |
|---|---|
| Color | The full end color in full-color mode, or a color whose alpha channel holds the
target alpha in alpha-only mode (the RGB channels are |
StartValue
Gets the UnityEngine.Color value from which the tween interpolates.
Declaration
public Color StartValue { get; }
Property Value
| Type | Description |
|---|---|
| Color | The start color pinned at play time, or the value passed to SetFrom(Color) or SetFromAlpha(float). Returns UnityEngine.Color.white before the tween has started and no explicit start value has been set. |
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 first call when Progress is at or below zero and no start value has
been pinned, the getter is invoked to capture the live color. In alpha-only mode
(_alphaOnly == true), ColorAlpha(Color, float, float)
is then used to interpolate only the alpha channel while preserving RGB from the
captured start color. In full-color mode, Color(Color, Color, float)
interpolates all four channels independently.
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<Color>, Action<Color>, Color, float)
Configures this tween for full RGBA color interpolation.
Resets _usesFrom and _alphaOnly so the tween begins with standard
"To" behavior (start captured from getter at play time, all four channels animated).
Declaration
public TweenColor Initialize(Func<Color> getter, Action<Color> setter, Color endValue, float duration)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Color> | getter | A delegate that reads the current UnityEngine.Color of the target property each
frame. Must not be |
| Action<Color> | setter | A delegate that writes the interpolated UnityEngine.Color to the target property
each frame. Must not be |
| Color | endValue | The UnityEngine.Color to animate toward (all four RGBA channels). |
| float | duration | Playback duration in seconds. Clamped to zero; a value of zero produces an instant-complete tween on the first update frame. |
Returns
| Type | Description |
|---|---|
| TweenColor | This tween instance for method chaining. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
InitializeAlpha(Func<Color>, Action<Color>, float, float)
Configures this tween for alpha-only fade transitions. Only the alpha channel is animated; RGB channels are preserved from the color captured by the getter at play time.
Declaration
public TweenColor InitializeAlpha(Func<Color> getter, Action<Color> setter, float endAlpha, float duration)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Color> | getter | A delegate that reads the current UnityEngine.Color of the target property.
The returned color's RGB channels are preserved throughout the tween.
Must not be |
| Action<Color> | setter | A delegate that writes the interpolated UnityEngine.Color (with updated alpha)
to the target property each frame. Must not be |
| float | endAlpha | The target alpha value in the range |
| float | duration | Playback duration in seconds. Clamped to zero; a value of zero produces an instant-complete tween on the first update frame. |
Returns
| Type | Description |
|---|---|
| TweenColor | 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.
SetEndValue(Color)
Updates the UnityEngine.Color end value that the tween animates toward.
Declaration
public TweenColor SetEndValue(Color endValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | endValue | The new target UnityEngine.Color value. |
Returns
| Type | Description |
|---|---|
| TweenColor | This tween instance for method chaining. |
Remarks
In alpha-only mode only endValue.a is used; the R, G, B components are
ignored. Safe to call while the tween is playing; the change takes effect on the
next Update(float) call.
SetFrom(Color)
Pins an explicit full-color start value, converting this into a "From" tween that
begins at fromValue and animates toward the configured end color.
Declaration
public TweenColor SetFrom(Color fromValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | fromValue | The UnityEngine.Color from which interpolation begins. In alpha-only mode, only
the alpha component ( |
Returns
| Type | Description |
|---|---|
| TweenColor | This tween instance for method chaining. |
SetFromAlpha(float)
Pins an explicit starting alpha value for alpha-only fade tweens, converting this into a "From" tween. The RGB channels of the internal start color are set to zero; at play time the actual RGB will be supplied by the getter when the start color is captured.
Declaration
public TweenColor SetFromAlpha(float fromAlpha)
Parameters
| Type | Name | Description |
|---|---|---|
| float | fromAlpha | The alpha value from which the fade begins. Not clamped; values outside |
Returns
| Type | Description |
|---|---|
| TweenColor | This tween instance for method chaining. |
Remarks
This method is intended for use in conjunction with InitializeAlpha(Func<Color>, Action<Color>, float, float). In full-color mode the RGB channels of the start value would be lost when ColorAlpha(Color, float, float) preserves them from the runtime getter; use SetFrom(Color) instead for full-color tweens.