Scripting
Create tweens in code and control them with TweenHandle
Create and Bind
Add using Evo.Tween;, then call EvoTween.Create(from, to, duration). This creates a value-type builder. Playback starts when you call Bind(...) or a property binding such as BindToScale(...).
Supported value types are float, int, bool, Vector2, Vector3, Vector4, Color, and Quaternion. Integers are rounded, booleans switch at eased progress 0.5, and quaternions use spherical interpolation.
using UnityEngine;
using Evo.Tween;
public class MenuFade : MonoBehaviour
{
[SerializeField] private CanvasGroup group;
private TweenHandle fade;
public void Show()
{
fade.Kill();
fade = EvoTween.Create(group.alpha, 1f, 0.2f)
.WithEase(Ease.OutCubic)
.WithTimeMode(TimeMode.Unscaled)
.BindToAlpha(group);
}
public void Hide()
{
fade.Kill();
fade = EvoTween.Create(group.alpha, 0f, 0.15f)
.WithTimeMode(TimeMode.Unscaled)
.BindToAlpha(group);
}
private void OnDisable() => fade.Kill();
}The code API uses the explicit start value passed to Create. Read the property at the time you create the tween if you want to start from its current value.
Configuration
Builder methods return an updated copy. Chain them as shown above, or assign the returned builder back to your variable.
| Method | Description |
|---|---|
WithDelay(float seconds) | Waits before interpolation starts. |
WithEase(Ease ease, AnimationCurve customCurve = null) | Chooses built-in or custom easing. |
WithLoops(int iterations, LoopMode loop = LoopMode.Restart) | Sets total iterations. 0 means infinite when the loop mode is Restart or Yoyo. |
WithSpeed(float speed) | Sets the playback speed multiplier. |
WithTimeMode(TimeMode mode) | Chooses Scaled or Unscaled time. |
WithUpdatePhase(UpdatePhase phase) | Chooses Update, LateUpdate, or FixedUpdate. |
OnComplete(Action callback) | Sets the callback for successful completion. |
The code builder defaults to Linear, one iteration, no delay, speed 1, scaled time, and Update. Serialized clip settings default to OutCubic. These configuration methods are also available on the returned TweenHandle.
Yoyo counts each direction as one iteration. Use WithLoops(2, LoopMode.Yoyo) for one trip out and back, or WithLoops(0, LoopMode.Yoyo) to keep repeating.
For Rigidbody bindings, explicitly select UpdatePhase.FixedUpdate. Choosing a Rigidbody target doesn't change the update phase automatically.
Control Playback
Keep the returned TweenHandle when you need control over a running tween.
| Member | Description |
|---|---|
IsActive | Whether the tween is still alive, including while paused. |
IsPaused | Whether playback is paused. |
Pause() / Resume() | Holds or resumes the current time. |
Complete() | Jumps to the value a normal finish leaves, then invokes the completion callback. |
Kill() | Cancels without invoking the completion callback or restoring the old value. |
Handles are structs with a version check, so a handle from an old run won't control a later tween that reuses the same pooled instance. A default handle is safe to call. Request a new tween to replay an animation after its previous run has ended.
Lifetime and Property Conflicts
Target-bound code tweens stop when their Unity target is destroyed. Disabling a GameObject doesn't automatically cancel its code tweens, so use OnDisable if they should stop with your component.
Starting another target-bound code tween with the same target and property key cancels the previous one. Built-in bindings supply their own keys; for example, the anchored-position and slide bindings share a key because both write anchoredPosition.
This conflict handling doesn't coordinate every clip inside a Tween Animator or a sequence. Avoid overlapping writers to the same property across sequences, code tweens, Animator controllers, and layout scripts.
Animate a Custom Value
Use Bind(Action<T>) for a simple setter. For a Unity target, the target-and-setter overload lets you avoid a capturing lambda and participate in property conflict handling.
using UnityEngine;
using Evo.Tween;
public class MeterFill : MonoBehaviour
{
// Keep this key stable and unique for this property on this target.
private const int FillProperty = 10001;
private TweenHandle tween;
public float Fill { get; private set; }
public void SetFill(float value)
{
tween = EvoTween.Create(Fill, Mathf.Clamp01(value), 0.3f)
.Bind(this, static (meter, current) => meter.Fill = current, FillProperty);
}
private void OnDisable() => tween.Kill();
}A plain setter binding has no target lifetime or property key to track. Cancel it explicitly if its callback accesses an object that might be destroyed.
For a serialized, reusable version of a custom property, see For Developers. For ready-made bindings, see Modules.