Beispiel #1
0
        public void PlayAnimation(string animationName, AnimationPlayStyle playStyle, bool replayAnimation = false)
        {
            if (CurrentAnimationName == animationName && !replayAnimation)
            {
                return;
            }

            try {
                StopCoroutine(coroutine); //Stop the coroutine.
            } catch { }

            coroutine = StartCoroutine(PlayAnimationCoroutine(animationName, playStyle)); //Start the coroutine.
        }
Beispiel #2
0
        private IEnumerator PlayAnimationCoroutine(string animationName, AnimationPlayStyle playStyle)
        {
            Animation animation;

            try {
                animation = AssetManager.GetAnimation(animationName);
            } catch {
                Debug.Log($"Failed to get animation {animationName}!");
                yield break;
            }

            CurrentAnimation     = animation;
            CurrentAnimationName = animationName;
            CurrentFrame         = 0;

            int currentFrame = 0;
            int increment    = 1;

            while (true)
            {
                try {
                    spriteRenderer.sprite = AssetManager.GetSprite(animation.Frames[currentFrame].ImageName, AssetManager.GetSpritePivot(GetCurrentSprite()), GetCurrentSprite().pixelsPerUnit); //Get the sprite.
                } catch { }
                currentFrame = Mathf.Clamp(currentFrame + increment, 0, animation.NumberOfFrames);

                CurrentFrame = currentFrame;

                if (currentFrame == animation.NumberOfFrames && increment == 1)
                {
                    switch (playStyle)
                    {
                    case AnimationPlayStyle.Loop:
                        currentFrame = 0;
                        break;

                    case AnimationPlayStyle.StayOnLastFrame:
                        yield break;

                    default:
                        increment = -1;     //Go back.
                        break;
                    }
                }
                else if (currentFrame == 0 && increment == -1)
                {
                    switch (playStyle)
                    {
                    case AnimationPlayStyle.Loop:
                        increment = 1;     //Loop the animation.
                        break;

                    case AnimationPlayStyle.PingPongToIdleState:
                        PlayIdleAnimation();     //Play the idle animation.
                        break;

                    case AnimationPlayStyle.GoToIdleState:
                        PlayIdleAnimation();
                        break;
                    }
                }

                yield return(new WaitForSeconds(animation.Interval)); //Wait.
            }

            yield return(null);
        }