Add to your Unity project as a git package using the package manager.
https://github.com/Less3Design/L3-tween.git
L3 Tween is based around exclusively supporting the way I like to write tweens:
private Tween moveTween;
void DoAnimation()
{
Vector3 startPos = transform.position;
Vector3 endPos = Random.insideUnitSphere;
//cancel previous tween, start a new one
moveTween = moveTween.Cancel().New(1f).SetOnUpdate((t) => {
transform.position = Vector3.Lerp(startPos,endPos,t);
});
}Tween is a readonly struct handle, so always assign the result of Cancel() / New(). A handle goes dead the moment its tween completes or is cancelled; tween.IsActive tells you which. Setting a callback on a dead handle logs a warning and does nothing.
Supporting just a single use case lets us keep the business logic to only a couple hundred lines. Here is all the state we mainatin:
public class TweenObject
{
public ushort id;
public ushort version;
public bool active;
public int activeIndex;
public float fromValue;
public float toValue;
public float duration;
public float elapsedTime;
public Ease easeType;
public TweenUpdateType deltaTimeType;
public Action<float> onUpdate;
public Action onComplete;
public Action onCancel;
}The pool starts at 512 tweens and doubles whenever it runs out, up to a max of 4096 (hard limit 65536, ids are ushorts).
Configure it from Project Settings > Less3 > Tween or from code, before the first tween:
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ConfigureTweens() => TweenManager.Configure(initialPoolSize: 1024, maxPoolSize: 8192);It's super good enough. If you need to animate many thousands of objects at once I reccomend treating that like a particle system, don't use tweens.
- TweenObjects are pooled; allocation is O(1) off a free list
- Only active tweens are iterated. A frame with no tweens running costs a single int compare
- The biggest cost will be whatever you allocate and execute inside tweens
- No async job burst etc... because the goal is to keep code as simple as possible
Video showing the system allocating tweens through its pool