High-performance, allocation-free tween library for Unity. Create animations, delays, and sequences in one line of code.
High-performance, allocation-free tween library for Unity. Create animations, delays, and sequences in one line of code.
PrimeTween is a high-performance, allocation-free animation library for Unity. Animate anything with just one line of code, tweak all animation properties directly from the Inspector, and create complex animation sequences. No runtime memory allocations, ever.
Get PrimeTween PRO to animate anything without code.
Performance comparison with other tween libraries.
Asset Store | Forum | FAQ | YouTube
Import PrimeTween from Asset Store. This is a preferable option because it comes with a Demo scene.
Optional: install via Unity Package Manager (UPM).
Without further ado, let's jump straight to the code!
using PrimeTween;
// Animate 'transform.position.y' from the current value to 10 in 1 second using the Ease.InOutSine
Tween.PositionY(transform, endValue: 10, duration: 1, ease: Ease.InOutSine);
// Rotate 'transform' from the current rotation to (0, 90, 0) in 1 second
Tween.Rotation(transform, endValue: Quaternion.Euler(0, 90, 0), duration: 1);
// Rotate 'transform' around the y-axis by 360 degrees in 1 second
Tween.EulerAngles(transform, startValue: Vector3.zero, endValue: new Vector3(0, 360), duration: 1);That's it!
Simply type Tween. and let your IDE suggest all supported animations. Out of the box, PrimeTween can animate almost everything: UI, UI Toolkit, Materials, Camera properties, Transforms, AudioSource, and whatnot.
Didn't find what you're looking for? No problem, use Tween.Custom() to animate anything.
To view PrimeTween's XML documentation in your IDE, enable this setting: 'Unity Preferences/External Tools/Generate .csproj files for/Local tarball' and press the 'Regenerate project files' button.
…Use .OnComplete() to execute custom code on tween's completion.
// Call SomeMethod() when the animation completes
Tween.Position(transform, endValue: new Vector3(10, 0), duration: 1)
.OnComplete(() => SomeMethod());
// After the animation completes, wait for 0.5 seconds, then destroy the GameObject
Tween.Scale(transform, endValue: 0, duration: 1, endDelay: 0.5f)
.OnComplete(() => Destroy(gameObject));"But wait! There is a memory allocation in the example above" you would say. And you would be right: calling
SomeMethod()orDestroy()capturesthisreference in a closure and allocates heap memory. See how to address this in the zero allocations section.
Creating delays is by far the most useful feature in game development. Delays in PrimeTween behave like normal tweens and can be used with sequences, coroutines, and async/await methods. All while being completely allocation-free.
Tween.Delay(duration: 1f, () => Debug.Log("Delay completed"));Animations can be repeated with the help of cycles. To apply cycles to an animation, pass the int cycles and CycleMode cycleMode parameters to a Tween. method. Setting cycles to -1 will repeat the tween indefinitely.
Tween.PositionY(transform, endValue: 10, duration: 0.5f, cycles: 2, cycleMode: CycleMode.Yoyo);To apply cycles to a Sequence, use the Sequence.Create(cycles: numCycles, cycleMode: CycleMode.Yoyo) method.
Sequence.Create(cycles: 2, CycleMode.Yoyo)
.Chain(Tween.PositionX(transform, 10, duration))
.Chain(Tween.PositionY(transform, 20, duration));endValue by the difference between startValue and endValue. For example, if a tween moves position.x from 0 to 1, then after the first cycle, the tween will move the position.x from 1 to 2, and so on.Sets the number of remaining cycles to an animation. This method modifies the cyclesTotal so that the animation will complete after the number of cycles.
Stops an animation when it reaches 'startValue' or 'endValue' for the next time.
For example, if you have an infinite animation (cycles == -1) with CycleMode.Yoyo/Rewind, and you wish to stop it when it reaches the 'endValue', then set stopAtEndValue to true. To stop the animation at the 'startValue' set stopAtEndValue to false.
There are several sequencing methods in PrimeTween. Let's start with the most common one: grouping tweens in Sequences.
Sequence is a group of tweens, callbacks, and other sequences. Animations in a sequence can overlap, run sequentially or in parallel, or any combination of those. You can control a Sequence the same way as individual tweens, see controlling tweens section.
…Places animation inside this sequence at the specified time, overlapping with other animations. The total sequence duration is increased if the inserted animation doesn't fit inside the current sequence duration.
Places animation after all previously added animations in this sequence. Chained animations run sequentially after one another.
Groups animation with the 'previous' animation in this Sequence. The 'previous' animation is the animation used in the preceding Group/Chain/Insert() method call. Grouped animations start at the same time and run in parallel.
Another sequencing method is waiting for animations in coroutines by calling .ToYieldInstruction().
IEnumerator Coroutine() {
Tween.PositionX(transform, endValue: 10f, duration: 1.5f);
yield return Tween.Scale(transform, 2f, 0.5f, startDelay: 1).ToYieldInstruction();
yield return Tween.Rotation(transform, new Vector3(0f, 0f, 45f), 1f).ToYieldInstruction();
// Non-allocating alternative to 'yield return new WaitForSeconds(1f)'
yield return Tween.Delay(1).ToYieldInstruction();
Debug.Log("Sequence completed");
}And the last method is awaiting animations using the async/await pattern. Async/await is a great tool to prevent the callback hell in your code. PrimeTween doesn't use threads, so tweens can be awaited on all platforms, even on WebGL.
async void AsyncMethod() {
Tween.PositionX(transform, endValue: 10f, duration: 1.5f);
await Tween.Scale(transform, endValue: 2f, duration: 0.5f, startDelay: 1);
await Tween.Rotation(transform, endValue: new Vector3(0f, 0f, 45f), duration: 1f);
// Non-allocating alternative to 'await Task.Delay(1000)' that doesn't use 'System.Threading'
// Animations can be awaited on all platforms, even on WebGL
await Tween.Delay(1);
Debug.Log("Sequence completed");
}While PrimeTween never allocates memory at runtime, the async/await feature in C# is allocating: awaiting an async method allocates a small amount of garbage. Likewise, StartCoroutine() also allocates GC.
For performance-intensive code paths, use Sequence instead of async methods or Coroutines.
Use .SetCancellationToken() to stop an animation when a CancellationToken is canceled. Cancellation stops the animation as if Stop() was called and happens on the next PrimeTween update; if the token is already canceled, the animation is stopped immediately. If the animation is awaited with 'await', the 'await' throws an OperationCanceledException when the token is canceled.
To make a Sequence cancellable, call SetCancellationToken() on the Sequence itself instead of its children animations.
var cancellationTokenSource = new CancellationTokenSource();
Sequence.Create()
.Chain(Tween.PositionX(transform, endValue: 10f, duration: 1.5f))
.Chain(Tween.Scale(transform, endValue: 2f, duration: 0.5f))
.SetCancellationToken(cancellationTokenSource.Token);
// ...
cancellationTokenSource.Cancel(); // stops the animationAll static Tween. methods return a Tween struct. While the tween.isAlive you can control it and access its properties such as duration, elapsedTime, progress, interpolationFactor, etc.
After completion, the tween becomes 'dead' and can't be reused. This ensures that completed tweens don't eat computing resources and prevents the common performance pitfalls encountered in other tween libraries.
…As you can see, there is no way to change the direction of the currently running tween, it can only be stopped and completed. But how to play an animation forward and backward, for example, to show or hide a window? Easy! Just start a new Tween in the desired direction.
[SerializeField] RectTransform window;
public void SetWindowOpened(bool isOpened) {
Tween.UIAnchoredPositionY(window, endValue: isOpened ? 0 : -500, duration: 0.5f);
}In this example, the SetWindowOpened() can be called again while the previous animation is still running. Generally, there is no need to stop the previously running tween in such cases. The new tween will seamlessly start from the current position and overwrite all previously running tweens on the window. Several duplicated tweens are fine, but if your code can potentially create duplicated tweens every frame, then consider stopping the previous tween with the help of tween.Stop() or Tween.StopAll(onTarget: target).
Inspector integration is the cornerstone of PrimeTween's design. It lets you tweak all animation properties from the Inspector without changing the code. It saves time and gives creative freedom. All animation setting
No open issues yet, or sync has not completed.