33 lines
No EOL
864 B
C#
33 lines
No EOL
864 B
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace FramedAnimator {
|
|
[RequireComponent(typeof(SpriteRenderer))]
|
|
public class Animator : MonoBehaviour {
|
|
[SerializeField] private Animation animation;
|
|
|
|
private float _currentFrame;
|
|
private int _renderingFrame;
|
|
private int _limit;
|
|
|
|
private SpriteRenderer _renderer;
|
|
|
|
private void Awake() {
|
|
_renderer = GetComponent<SpriteRenderer>();
|
|
}
|
|
|
|
private void Update() {
|
|
if (_renderingFrame >= _limit)
|
|
return;
|
|
_currentFrame += Time.deltaTime * animation.FrameRate;
|
|
|
|
_renderingFrame = Mathf.Clamp(Mathf.FloorToInt(_currentFrame), 0, _limit);
|
|
_renderer.sprite = animation.GetFrame(_renderingFrame);
|
|
}
|
|
|
|
public void PlayUntil(float fraction) {
|
|
_limit = Mathf.RoundToInt(animation.FrameCount * Mathf.Clamp(fraction, 0, 1));
|
|
_limit = Mathf.Clamp(_limit, 0, animation.FrameCount - 1);
|
|
}
|
|
}
|
|
} |