54 lines
No EOL
1.3 KiB
C#
54 lines
No EOL
1.3 KiB
C#
using System;
|
|
using FramedAnimator.Domain;
|
|
using UnityEngine;
|
|
|
|
namespace FramedAnimator {
|
|
[RequireComponent(typeof(SpriteRenderer))]
|
|
public class Animator : MonoBehaviour {
|
|
[SerializeField] private new Animation animation;
|
|
public string CurrentAnimation => animation.name;
|
|
private SpriteRenderer _renderer;
|
|
|
|
public event Action<string> OnAnimationEnd;
|
|
private AnimatorModel _model;
|
|
|
|
private void Awake() {
|
|
_renderer = GetComponent<SpriteRenderer>();
|
|
if(animation)
|
|
_model = new AnimatorModel(animation.FrameRate, animation.FrameCount);
|
|
}
|
|
|
|
private void Update() {
|
|
if (!animation)
|
|
return;
|
|
|
|
if (_model.RenderingFrame >= _model.FrameCount - 1)
|
|
TryCallAnimationEnd();
|
|
else
|
|
UpdateAnimationFrame();
|
|
}
|
|
|
|
private void UpdateAnimationFrame() {
|
|
_model.UpdateAnimationFrame(Time.deltaTime);
|
|
_renderer.sprite = animation.GetFrame(_model.RenderingFrame);
|
|
}
|
|
|
|
private void TryCallAnimationEnd() {
|
|
if(_model.AnimationEnded(Time.deltaTime))
|
|
OnAnimationEnd?.Invoke(animation.name);
|
|
}
|
|
|
|
public void ChangeAnimation(Animation anim) {
|
|
_model = new AnimatorModel(anim.FrameRate, anim.FrameCount);
|
|
animation = anim;
|
|
_renderer.sprite = animation.GetFrame(0);
|
|
}
|
|
|
|
public void PlayUntil(float fraction) {
|
|
if (fraction >= 1f)
|
|
_model.PlayToEnd = true;
|
|
else
|
|
_model.PlayUntil(fraction);
|
|
}
|
|
}
|
|
} |