Exemple #1
0
    private void Update()
    {
        // We do nothing if FPS <= 0
        if (framesPerSecond <= 0)
        {
            return;
        }

        if (_playing)
        {
            _animationTimer += Time.deltaTime;

            if (1f / framesPerSecond < _animationTimer)
            {
                // Next Frame!
                _frameDurationCounter++;
                _spriteRenderer.sprite = _currentAnimation.GetFrame(_animationIndex);
                _animationTimer        = 0;

                if (_frameDurationCounter >= _currentAnimation.FramesDuration[_animationIndex])
                {
                    // Change frame only if have passed the desired frames
                    _animationIndex      += 1;
                    _frameDurationCounter = 0;
                }

                if (_animationIndex >= _framesInAnimation)
                {
                    // Last frame, reset index and stop if is one shot
                    _animationIndex = 0;
                    onFinish.Invoke();
                    if (_oneShot)
                    {
                        Stop();
                    }
                }
            }
        }
    }