using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Image))]
public class ImageFrameAni : MonoBehaviour
{
    [SerializeField] Sprite[] mSprFrames;
    [SerializeField] int mFrameRate = 30;
    [SerializeField] bool mIsPlaying = true;

    private Image mImg;

    private int mCurIndex;

    private float mDelta;
    private float mTimer;

    private void Awake()
    {
        mImg = GetComponent<Image>();
    }

    private void Start()
    {
        mDelta = 1.0f / mFrameRate;
        mTimer = mDelta;

        mCurIndex = 0;
        mImg.sprite = mSprFrames[mCurIndex];
    }

    private void Update()
    {
        if (mSprFrames.Length > 0 && mIsPlaying)
        {
            mTimer -= Time.deltaTime;

            if (mTimer <= 0)
            {
                mCurIndex = (mCurIndex + 1) % mSprFrames.Length;
                mImg.sprite = mSprFrames[mCurIndex];

                mTimer = mDelta;
            }
        }
    }
}