PrimeTween for basic 2D sprite animations? #72
-
Hi, is PrimeTween a good choice for animating 2D sprites (keyframing different sprites)? I'm currently using a Sprite Renderer + Animator + Animancer Component, which seems like overkill for a basic sprite animation. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey! Although there is no built-in support for this, it's absolutely possible to use PrimeTween for simple sprite animations. Here is a simple script: using System;
using PrimeTween;
using UnityEngine;
using UnityEngine.Assertions;
[Serializable]
public class SpriteAnimation {
[SerializeField] SpriteRenderer spriteRenderer;
[SerializeField] Sprite[] sprites;
[SerializeField] TweenSettings settings = new TweenSettings(1f, Ease.Linear, cycles: -1);
int curIndex = -1;
public Tween Play() {
Assert.IsNotNull(spriteRenderer, nameof(spriteRenderer) + " reference is not populated in Inspector.");
Assert.IsTrue(sprites != null && sprites.Length > 0, "Sprites array is empty.");
curIndex = -1;
tween.Stop();
tween = Tween.Custom(this, 0f, sprites.Length, settings, static (_this, val) => {
int newIndex = (int)val;
var _sprites = _this.sprites;
if (newIndex < _sprites.Length && _this.curIndex != newIndex) {
_this.curIndex = newIndex;
_this.spriteRenderer.sprite = _sprites[newIndex];
}
});
return tween;
}
public Tween tween { get; private set; }
public bool isAlive => tween.isAlive;
public void Stop() => tween.Stop();
} Usage example: using UnityEngine;
public class SpriteAnimationExample : MonoBehaviour {
[SerializeField] SpriteAnimation idleAnimation;
[SerializeField] SpriteAnimation walkAnimation;
[SerializeField] SpriteAnimation jumpAnimation;
SpriteAnimation currentAnimation;
void Update() {
if (jumpAnimation.isAlive) {
return;
}
if (Input.GetKeyDown(KeyCode.Space)) {
SetCurrentAnimation(jumpAnimation);
jumpAnimation.tween.OnComplete(() => print("jump completed"));
return;
}
if (Input.GetKey(KeyCode.RightArrow)) {
SetCurrentAnimation(walkAnimation);
} else {
SetCurrentAnimation(idleAnimation);
}
}
void SetCurrentAnimation(SpriteAnimation animation) {
if (currentAnimation == animation) {
return;
}
currentAnimation?.Stop();
animation.Play();
currentAnimation = animation;
}
} I used this free 2D Character Sprite Animation - Penguin asset to test this and here is the result: |
Beta Was this translation helpful? Give feedback.
-
Thank you for the example code. I modified it to use ScriptableObjects to hold each group of sprites per animation, and the Component Names asset to organize the different sprite animations. https://i.gyazo.com/1ac7a0fde5e816c0c586a3557bc5cc02.png
I have not tested the performance yet to compare it against the Unity Animator. But with the Unity Animator, I was seeing ~2 ms spent each frame on animation (https://gyazo.com/042d3858cf90eb93937ce7f2232c53ce), and I think PrimeTween would perform much better. I have not yet converted my existing animations to using PrimeTween yet, because I am unsure what will be my workflow for lining up / syncing Collider2D shape animations to sprite animations, but it is on my list of TODOs. |
Beta Was this translation helpful? Give feedback.
Hey!
Although there is no built-in support for this, it's absolutely possible to use PrimeTween for simple sprite animations. Here is a simple script: