Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CreateAssetMenu(menuName = "Sprites/Sprite Animation", fileName = "NewSpriteAnimation")]
- public class SpriteAnimation : ScriptableObject {
- [System.Serializable]
- public struct Frame {
- public Sprite sprite;
- public int milliseconds;
- }
- public bool loop = false;
- public int cancellableAt = 0;
- public float speed = 1f;
- public Frame[] frames;
- }
- public class SpriteAnimator : MonoBehaviour {
- [System.Serializable]
- public struct Request {
- public SpriteAnimation animation;
- public float speed;
- public bool loop;
- public int start;
- public int end;
- }
- public SpriteAnimation defaultAnimation;
- public bool playOnAwake;
- public bool ignoreTimescale;
- public bool pause;
- [ReadOnly] public int frame;
- [ReadOnly] public Request currentRequest;
- [ReadOnly] public Request nextRequest;
- SpriteRenderer spriteRenderer;
- float elapsed = 0f;
- float duration;
- void Awake() {
- spriteRenderer = GetComponent<SpriteRenderer>();
- }
- void OnEnable() {
- if (playOnAwake && defaultAnimation != null) { PlayAnimation(defaultAnimation, start: Random.Range(0, defaultAnimation.frames.Length)); }
- }
- void Update() {
- if (currentRequest.animation == null || pause) { return; }
- if (nextRequest.animation != null && frame >= currentRequest.animation.cancellableAt) {
- currentRequest = nextRequest;
- nextRequest.animation = null;
- ChangeFrame(currentRequest.start);
- }
- elapsed += ignoreTimescale ? Time.unscaledDeltaTime : Time.deltaTime;
- if (elapsed >= duration) {
- frame++;
- if (frame > currentRequest.end) {
- if (currentRequest.loop || currentRequest.animation.loop) {
- frame = currentRequest.start;
- }
- else {
- PlayAnimation(defaultAnimation);
- return;
- }
- }
- ChangeFrame(frame);
- }
- }
- public void ChangeFrame(int value) {
- frame = value
- elapsed = 0f;
- duration = currentRequest.animation.frames[frame].milliseconds * 0.001f * (1f / currentRequest.animation.speed) * (1f / currentRequest.speed);
- spriteRenderer.sprite = currentRequest.animation.frames[frame].sprite;
- }
- public void PlayAnimation(SpriteAnimation animation, float speed = 1f, bool loop = false, int start = -1, int end = -1, bool force = false) {
- if (animation == null) {
- Stop();
- return;
- }
- start = start < 0 ? 0 : start;
- end = end < 0 ? animation.frames.Length - 1 : end;
- if (currentRequest.animation == null || frame >= currentRequest.animation.cancellableAt || force) {
- nextRequest.animation = null;
- currentRequest.animation = animation;
- currentRequest.speed = speed;
- currentRequest.loop = loop;
- currentRequest.start = start;
- currentRequest.end = end;
- ChangeFrame(start);
- }
- else {
- nextRequest.animation = animation;
- nextRequest.speed = speed;
- nextRequest.loop = loop;
- nextRequest.start = start;
- nextRequest.end = end;
- }
- }
- public void Stop() {
- currentRequest.animation = null;
- nextRequest.animation = null;
- spriteRenderer.sprite = null;
- frame = -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement