47 lines
980 B
C#
47 lines
980 B
C#
|
|
using UnityEngine;
|
|
[ RequireComponent(typeof(SpriteRenderer))]
|
|
public class Ani : MonoBehaviour
|
|
{
|
|
public SpriteRenderer spriteRenderer { get ; private set;}
|
|
|
|
public Sprite[] Sprites;
|
|
public float animationTime = 0.25f ;
|
|
public int animationFrame{get; private set;}
|
|
public bool loop =true;
|
|
private void Awake()
|
|
{
|
|
this.spriteRenderer = GetComponent<SpriteRenderer>();
|
|
}
|
|
private void Start()
|
|
{
|
|
InvokeRepeating(nameof(Advance), this.animationTime, this.animationTime);
|
|
}
|
|
private void Advance()
|
|
{
|
|
if(!this.spriteRenderer.enabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
this.animationFrame++;
|
|
if (this.animationFrame>= this.Sprites.Length && this.loop)
|
|
{
|
|
this.animationFrame = 0;
|
|
}
|
|
if(this.animationFrame >= 0 && this.animationFrame < this.Sprites.Length)
|
|
{
|
|
this.spriteRenderer.sprite = this.Sprites[this.animationFrame];
|
|
}
|
|
}
|
|
public void Restart()
|
|
{
|
|
this.animationFrame = -1;
|
|
|
|
Advance();
|
|
}
|
|
}
|
|
|